利用JAVA GUI制作用户聊天界面
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class Example18 extends JFrame{
JButton sendBt;
JTextField inputFiled;
JTextArea chatContent;
private boolean flase;
public Example18(){
this.setLayout(new BorderLayout());
chatContent=new JTextArea(12,34);
JScrollPane showPanel=new JScrollPane(chatContent);
chatContent.setEditable(flase);
JPanel inputPanel=new JPanel();
inputFiled=new JTextField(20);**
**sendBt=new JButton(“发送”);
sendBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String content=inputFiled.getText();
if(content !=null && !content.trim().equals("")){
chatContent.append(“本人:”+content+"\n");
}
else{
chatContent.append("聊天信息不能为空"+"\n");
}
inputFiled.setText("");
}
});
Label label=new Label("聊天信息");
inputPanel.add(label);
inputPanel.add(inputFiled);
inputPanel.add(sendBt);
this.add(showPanel,BorderLayout.CENTER);
this.add(inputPanel,BorderLayout.SOUTH);
this.setTitle("聊天窗口");
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main**(String[] args){
new Example18();
}
}
这个可能没有标注注释,下次会有的。