一、运行截图
二、程序代码
package java实训;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SJ7_4 {
JLabel q1;
JFrame kj;
//JPanel mb1;
JButton an1,an2,an3,an4;
JMenuBar cd1;
JMenu cd,cd2;
JTextArea dhwb;
JTextField dhwb2;
JComboBox<Object> xlk;
public class sjcl1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
String neir=dhwb2.getText();
if(neir!=null&&!neir.trim().equals("")) {
dhwb.append("我:"+neir+"\n");
}
else {
dhwb.append("你输入的聊天信息不能为空,请重新输入!"+"\n");
}
dhwb2.setText(null);
}
}
public class sjcl2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
dhwb.setText(null);
}
}
public class sjcl3 implements ActionListener{
public void actionPerformed(ActionEvent e) {
JButton ks=(JButton) e.getSource();
dhwb.setEditable(true);
dhwb2.setEditable(true);
an1.setEnabled(true);
an2.setEnabled(true);
an3.setEnabled(true);
ks.setEnabled(false);
}
}
public class sjcl4 implements ActionListener{
public void actionPerformed(ActionEvent e) {
an4.setEnabled(true);
an3.setEnabled(false);
dhwb.setEditable(false);
dhwb2.setEditable(false);
an1.setEnabled(false);
an2.setEnabled(false);
}
}
public void sun() {
JFrame kj=new JFrame("聊天窗口界面");
kj.setSize(550, 600);
kj.setLocation(400, 300);
kj.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
JLabel xm = new JLabel("请选择你联系人:");
dhwb=new JTextArea(12,45);
dhwb.setFont( new Font("宋体",Font.PLAIN,20));
dhwb2=new JTextField(25);
dhwb.setEditable(false);
dhwb2.setEditable(false);
JScrollPane sun=new JScrollPane (dhwb);
sun.setSize(80, 180);
sun.setLocation(420, 260);
an4=new JButton("开始聊天");
an3=new JButton("结束聊天");
an1=new JButton("发送");
an2=new JButton("清空");
an1.setEnabled(false);
an2.setEnabled(false);
an3.setEnabled(false);
kj.add(xm);
String [] ss={"孙既来","卢新敏","李阳","但传致","张熙"};
xlk=new JComboBox<Object>(ss);
kj.add(xlk);
kj.add(an4);
kj.add(an3);
kj.add(sun);
kj.add(dhwb2);
kj.add(an1);
kj.add(an2);
an1.addActionListener(new sjcl1());
an2.addActionListener(new sjcl2());
an4.addActionListener(new sjcl3());
an3.addActionListener(new sjcl4());
kj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
kj.setResizable(false);
kj.setVisible(true);
}
public static void main(String[] args) {
SJ7_4 xx=new SJ7_4();
xx.sun();
}
}