首页 > 代码库 > 简易聊天界面

简易聊天界面

 

 1 package Layout;
 2 
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 import java.util.HashMap;
 7 import java.util.Map;
 8 
 9 import javax.swing.*;
10 
11 public class ChatInterface extends JFrame implements ActionListener {
12     private JTextArea jta = new JTextArea();
13     private JPanel jp = new JPanel();
14     private Map<String, String> map = new HashMap();
15     private String str[] = { "one", "two", "three", "four" };
16     @SuppressWarnings("unused")
17     private String str1[] = { str + "five" };
18     private JComboBox jcb = new JComboBox(str);
19     private JTextField jtf = new JTextField(10);
20     private JButton b = new JButton("发送");
21     private JScrollPane jsp = new JScrollPane(jta);//滚动条
22 
23     public ChatInterface() {
24         map.put("one", "one");
25         map.put("two", "two");
26         map.put("three", "three");
27         jp.add(jcb);
28         jp.add(jtf);
29         jp.add(b);
30         // this.setLayout( new GridLayout(2,1));
31 
32         this.add(jp, BorderLayout.SOUTH);
33         this.add(jsp, BorderLayout.CENTER);
34         this.setTitle("聊天窗口");
35         this.setIconImage((new ImageIcon("photo/psb.jpg")).getImage());
36         this.setSize(500, 400);
37         this.setLocation(100, 100);
38         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39         this.setVisible(true);
40 
41         b.addActionListener(this);
42         ActionEvent e = null;
43 
44         actionPerformed(e);
45 
46         // jta.append();
47         // jta.append(getName());
48         // System.out.println(str1);
49         // if(b!=null){str.}
50     }
51 
52     public static void main(String[] args) {
53         ChatInterface l = new ChatInterface();
54 
55     }
56 
57     @Override
58     public void actionPerformed(ActionEvent e) {
59      try{
60         String cmd = e.getActionCommand();
61         if ("发送" == cmd) {
62             jta.append(jtf.getText()+‘\n‘);}
63         }catch( java.lang.NullPointerException a){}
64         }
65     }

 

简易聊天界面