聊天窗口
布局:
默认布局,文本域取center,面板取south
组件:
1、文本域+滚动条
2、面板
- 下拉列表ComboBox
- 文本框
- 按钮
package com;
import javax.swing.*;
import java.awt.*;
public class Chat extends JFrame {
JTextArea area;
JPanel p;
JScrollPane scroll;
JComboBox box;
JTextField content;
JButton send;
public static void main(String[] args) {
Chat chat=new Chat();
}
Chat(){
area=new JTextArea();
scroll=new JScrollPane(area);
String[] name={"孙悟空","八戒","唐僧","沙师弟"};
box=new JComboBox(name);
content=new JTextField(10);
send=new JButton("发送");
p=new JPanel();
p.add(box);p.add(content);p.add(send);
this.add(scroll);
this.add(p, BorderLayout.SOUTH);
this.setTitle("聊天窗口");
this.setSize(300,300);
this.setLocation(300,300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
结果: