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 Ex`在这里插入代码片`
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)
javaGUI创建用户聊天窗口
最新推荐文章于 2023-06-23 17:02:47 发布