?? clientframenew.java
字號:
package com.chat.client;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
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 com.chat.constants.ClientConstants;
import com.chat.dto.MsgDTO;
import com.chat.io.TxtManager;
import com.chat.util.FontDialog;
@SuppressWarnings("serial")
public class ClientFramenew extends JFrame implements Runnable {
//private DefaultComboBoxModel cbxModel =new DefaultComboBoxModel();
// private InputStream in;
// private OutputStream out;
private JTextPane txtDownMsg;
private JTextPane txtUpMsg;
private MsgDTO tempMsgDTO=new MsgDTO();
private StyledDocument doc;
private MainFramtest Main;
public static void main(String args[]) {
try {
//ClientFramenew frame = new ClientFramenew(args[0],args[1]);
//frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public ClientFramenew() {
super();
}
public ClientFramenew(String myuserId,String towhomid,MainFramtest mainframe) {
super();
this.tempMsgDTO.setUserId(myuserId);
this.tempMsgDTO.setToWhere(towhomid);
Main=mainframe;
System.out.println(myuserId+"與"+towhomid+"對話");
getContentPane().setLayout(null);
setTitle("用戶 "+this.tempMsgDTO.getUserId()+" 與用戶: "+this.tempMsgDTO.getToWhere()+" 聊天中");
setBounds(100, 100, 479, 471);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JMenu menu = new JMenu();
menu.setText("文件(F)");
menuBar.add(menu);
final JMenuItem menuItem = new JMenuItem();
menuItem.setText("Menu Item");
menu.add(menuItem);
final JMenu menu_1 = new JMenu();
menu_1.setText("編輯(E)");
menuBar.add(menu_1);
final JMenuItem menuItem_1 = new JMenuItem();
menuItem_1.setText("Menu Item");
menu_1.add(menuItem_1);
final JMenu menu_2 = new JMenu();
menu_2.setText("動作(V)");
menuBar.add(menu_2);
final JMenuItem menuItem_2 = new JMenuItem();
menuItem_2.setText("Menu Item");
menu_2.add(menuItem_2);
final JMenu menu_3 = new JMenu();
menu_3.setText("設置(T)");
menuBar.add(menu_3);
final JMenuItem menuItem_3 = new JMenuItem();
menuItem_3.setText("Menu Item");
menu_3.add(menuItem_3);
final JMenu menu_4 = new JMenu();
menu_4.setText("幫助(H)");
menuBar.add(menu_4);
final JMenuItem btnAbout = new JMenuItem();
btnAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//About.main(new String[0]);
}
});
btnAbout.setText("關于...");
menu_4.add(btnAbout);
final JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBounds(0, 0, 512, 27);
getContentPane().add(desktopPane);
final JToolBar toolBar = new JToolBar();
toolBar.setBounds(0, 0, 502, 27);
desktopPane.add(toolBar);
final JButton button = new JButton();
button.setText("功能1");
toolBar.add(button);
toolBar.addSeparator();
final JButton button_1 = new JButton();
button_1.setText("功能2");
toolBar.add(button_1);
toolBar.addSeparator();
final JButton button_2 = new JButton();
toolBar.add(button_2);
button_2.setText("功能3");
toolBar.addSeparator();
final JButton button_3 = new JButton();
button_3.setText("功能4");
toolBar.add(button_3);
toolBar.addSeparator();
final JButton button_5 = new JButton();
button_5.setText("功能5");
toolBar.add(button_5);
/* final JButton button_4 = new JButton();
button_4.setText("New JButton");
button_4.setBounds(283, 0, 99, 23);
desktopPane.add(button_4);*/
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 43, 442, 191);
getContentPane().add(scrollPane);
txtUpMsg = new JTextPane();
txtUpMsg.setEditable(false);
scrollPane.setViewportView(txtUpMsg);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 273, 442, 98);
getContentPane().add(scrollPane_1);
txtDownMsg = new JTextPane();
scrollPane_1.setViewportView(txtDownMsg);
final JButton btnFont = new JButton();
btnFont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
getSelectedFont();
}
});
btnFont.setText("字體");
btnFont.setBounds(20, 240, 73, 23);
getContentPane().add(btnFont);
final JButton btnColor = new JButton();
btnColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
getSelectedColor();
}
});
btnColor.setText("顏色");
btnColor.setBounds(360, 240, 81, 23);
getContentPane().add(btnColor);
final JButton button_8 = new JButton();
button_8.setText("交談記錄");
button_8.setBounds(10, 382, 109, 23);
getContentPane().add(button_8);
final JButton button_9 = new JButton();
button_9.setText("消息模式");
button_9.setBounds(125, 382, 109, 23);
getContentPane().add(button_9);
final JButton btnClose = new JButton();
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Main.send(new String(ClientConstants.LOGOUT+ClientConstants.LOGIN_SPLIT+tempMsgDTO.getUserId()));
dispose();
//System.exit(0);
}
});
btnClose.setText("關閉(C)");
btnClose.setBounds(281, 382, 81, 23);
getContentPane().add(btnClose);
final JButton btnSend = new JButton();
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String content = txtDownMsg.getText();
if (content.trim().equals(""))
return;
tempMsgDTO.setContent(content);
//點擊發送時把數據名發送到服務器
if(Main.send(transferMsgDTO())){
txtDownMsg.setText("");
setMessageText(tempMsgDTO);
}
}
});
btnSend.setText("發送(S)");
btnSend.setBounds(368, 382, 84, 23);
getContentPane().add(btnSend);
final JLabel label = new JLabel();
label.setText("與 "+towhomid+" 對話");
label.setBounds(178, 240, 109, 27);
getContentPane().add(label);
/*cbxUserId = new JComboBox();
cbxUserId.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) { //這里控制為只處理一次
tempMsgDTO.setToWhere(cbxModel.getSelectedItem().toString());
}
}
});
cbxUserId.setBounds(46, 244, 142, 23);
getContentPane().add(cbxUserId);*/
// my methods
new Thread(this).start();
this.setVisible(true);
}
/*private boolean cbxContains(String item){//!!
int count=this.cbxModel.getSize();
for(int i=0;i<count;i++){
if(this.cbxModel.getElementAt(i).equals(item)){
return true;
}
}
return false;
}*/
@SuppressWarnings("unchecked")
//要重寫
public void run() {
while(true){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e.getMessage());}
for(int i=0;i<MainFramtest.ms.size();i++){
if(MainFramtest.ms.get(i).getUserId().equals(this.tempMsgDTO.getToWhere())){
setMessageText(MainFramtest.ms.get(i));
MainFramtest.ms.remove(i);
}
System.out.println("當前鏈表大小"+MainFramtest.ms.size());
System.out.println("本對話面板的towhere為"+this.tempMsgDTO.getToWhere());
//System.out.println("當前鏈表項 towhere:"+MainFramtest.ms.get(i).getToWhere()+
//";\nid:"+MainFramtest.ms.get(i).getUserId());
System.out.println("本對話面板的towhere yu 當前鏈表項 id");
}
}
}
private void getSelectedFont() {
FontDialog dialog = new FontDialog(this, "請選擇一種字體", this.tempMsgDTO
.getFont(), true);
dialog.showDialog();
Font tempFont = null;
if (FontDialog.DialogResult.Ok == dialog.getDialogResult()) {
tempFont = dialog.getSelectedFont();
// System.out.println("選擇的字體是:"+tempFont.toString());
}
dialog.Dispose();
if (tempFont != null) {
this.tempMsgDTO.setFont(tempFont);
}
}
private void getSelectedColor() {
Color color = JColorChooser.showDialog(this, "請選擇一種顏色", this.tempMsgDTO
.getColor());
if (color != null) {
this.tempMsgDTO.setColor(color);
}
}
private MsgDTO transferMsgDTO(){
MsgDTO msgDTO=new MsgDTO();
msgDTO.setUserId(this.tempMsgDTO.getUserId());
msgDTO.setContent(this.tempMsgDTO.getContent());
msgDTO.setToWhere(this.tempMsgDTO.getToWhere());
msgDTO.setFont(this.tempMsgDTO.getFont());
msgDTO.setColor(this.tempMsgDTO.getColor());
return msgDTO;
}
private void setMessageText(MsgDTO tempMsgDTO) {
String msg=tempMsgDTO.getUserId()
+ " 對 "
+ tempMsgDTO.getToWhere() + " 說: "
+ tempMsgDTO.getContent() + '\n';
// StyledDocument doc = txtMsg.getStyledDocument();
try {
addStylesToDocument(tempMsgDTO);
doc.insertString(doc.getLength(),msg, doc.getStyle("style1"));
} catch (BadLocationException e) {
e.printStackTrace();
}
// 讓文本框的焦點始終在最下面
txtUpMsg.setCaretPosition(doc.getLength());
}// 初始化10種顏色的字體
private void addStylesToDocument(MsgDTO tempMsgDTO) {
doc = txtUpMsg.getStyledDocument();
StyledDocument doc = txtUpMsg.getStyledDocument();
// 試試簡單方法
Style def = StyleContext.getDefaultStyleContext().getStyle(
StyleContext.DEFAULT_STYLE);
Font tempFont=tempMsgDTO.getFont();
Style tempStyle=doc.addStyle("style1", def);
StyleConstants.setBold(tempStyle,tempFont.isBold());
StyleConstants.setFontSize(tempStyle, tempFont.getSize());
StyleConstants.setItalic(tempStyle,tempFont.isItalic());
StyleConstants.setForeground(tempStyle,tempMsgDTO.getColor());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -