GUI交互界面简单应用(聊天窗口)
package test2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ChatFrame extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel panelTool;
private JButton btnSend;
private JTextField txtInput;
private JTextArea txtaShow;
//下拉菜单
private JComboBox<String> cmbUsers;
//滚动条
private JScrollPane srlp;
public ChatFrame(){
super("聊天窗口");
txtaShow = new JTextArea();
srlp = new JScrollPane(txtaShow);
this.add(srlp,BorderLayout.CENTER);
panelTool = new JPanel();
String [] names = new String[]{"全部","蒋元伟","陈杰","廖婷","刘超"};
cmbUsers = new JComboBox<String>(names);
panelTool.add(cmbUsers);
txtInput = new JTextField(10);
panelTool.add(txtInput);
btnSend = new JButton("发送");
panelTool.add(btnSend);
this.add(panelTool,BorderLayout.SOUTH);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtaShow.append(cmbUsers.getSelectedItem()+"说:"+
txtInput.getText()+"\r\n");
}
});
this.setVisible(true);
setLocation(350, 150);
setBackground(Color.CYAN);
setSize(300, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
ChatFrame cf = new ChatFrame();
}
}