?? serverframe.java
字號(hào):
package com.chat.server;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
//import server.MessageSendingControl;
@SuppressWarnings("serial")
public class ServerFrame extends JFrame {
// 顏色的名稱
public static final String initStyles[] = {
"regular_0"
};
// 限定最多顯示幾行(目前不考慮)
// private static final int maxRowCount = 20;
// 限定是否顯示 (默認(rèn)為true)
private static boolean isDisplay = true;
// 當(dāng)顯示數(shù)據(jù)時(shí),每一行的末尾需要用的字符(默認(rèn)是回車符)
private static final String lastChar = "\n";
// 以下是窗體布局中所需要的成員
private JButton btnStart;
private JButton btnPause;
private JButton btnExit;
private JScrollPane scrollPane;
private JTextPane txtMsg;
private StyledDocument doc = null;
public ServerFrame() {
super();
setTitle("JavaQQ 演示版");
scrollPane = new JScrollPane();
scrollPane.setBounds(0, 10, 892, 425);
getContentPane().add(scrollPane);
txtMsg = new JTextPane();
txtMsg.setEditable(false);
txtMsg.setFont(new Font("", Font.PLAIN, 5));
scrollPane.setViewportView(txtMsg);
btnPause = new JButton();
btnPause.setText("暫停顯示");
btnPause.setBounds(403, 453, 106, 23);
getContentPane().add(btnPause);
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isDisplay = false;
changeState();
}
});
btnStart = new JButton();
btnStart.setText("開始顯示");
btnStart.setEnabled(false);
btnStart.setBounds(236, 453, 106, 23);
getContentPane().add(btnStart);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isDisplay = true;
changeState();
}
});
btnExit = new JButton();
btnExit.setText("退出監(jiān)控");
btnExit.setBounds(568, 453, 106, 23);
getContentPane().add(btnExit);
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int choice=JOptionPane.showConfirmDialog(null,"是否確認(rèn)退出?","確定?",JOptionPane.YES_NO_OPTION);
if(choice!=JOptionPane.YES_OPTION)return;
dispose();
System.exit(0);
// MessageSendingControl.changeMessageSendingState(false);
// System.exit(0);
}
});
addStylesToDocument();
doc = txtMsg.getStyledDocument();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
// MessageSendingControl.changeMessageSendingState(false);
// System.exit(0);
}
});
getContentPane().setLayout(null);
setSize(898, 532);
// this.setUndecorated(true);//去掉窗體修飾,包括最大化按鈕
this.setResizable(false); // 禁止改變窗體大小
this.setLocationRelativeTo(null);
this.setVisible(true);
try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(getContentPane());
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
public void setMessageText(String msg) {
if (!isDisplay)return;
try {
// StyledDocument doc = txtMsg.getStyledDocument();
doc.insertString(doc.getLength(), msg + lastChar, doc
.getStyle(initStyles[0]));
// 讓文本框的焦點(diǎn)始終在最下面
txtMsg.setCaretPosition(doc.getLength());
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}
}
// 初始化10種顏色的字體
private void addStylesToDocument() {
StyledDocument doc = txtMsg.getStyledDocument();
// 試試簡(jiǎn)單方法
Style def = StyleContext.getDefaultStyleContext().getStyle(
StyleContext.DEFAULT_STYLE);
for (int i = 0; i < initStyles.length; i++) {
//font setting
Style temp=doc.addStyle(initStyles[i], def);
Font font=new Font("新宋體",Font.PLAIN,14);
Color color=new Color(0,0,0);
StyleConstants.setBold(temp, font.isBold());
StyleConstants.setFontSize(temp, font.getSize());
StyleConstants.setItalic(temp, font.isItalic());
StyleConstants.setForeground(temp,color);
}
}
// 更改開始顯示和停止顯示按紐的可用狀態(tài)
private void changeState() {
this.btnStart.setEnabled(!isDisplay);
this.btnPause.setEnabled(isDisplay);
}
public static void main(String args[]) {
ServerFrame sc = new ServerFrame();
sc.setMessageText("開始測(cè)試");
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -