2、面板Panel
解决了关闭窗口事件
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标背景
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(117, 195, 202));
//panel设置坐标 背景
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(202, 89, 161));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器:
frame.addWindowListener(new WindowAdapter() {
//窗口点击需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
#### 3、布局管理器
- 流式布局
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setSize(200,200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(200,200);
frame.setVisible(true);
}
}
- 表格布局 Grid
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("GridLayout");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//java函数,自动填充
frame.setVisible(true);
}
}
练习:
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
Panel p2 = new Panel();
p2.setLayout(new GridLayout(2,1));
p2.add(new Button("top-nor"));
p2.add(new Button("top-sou"));
p1.add(p2,BorderLayout.CENTER);
Panel p3 = new Panel(new BorderLayout());
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
Panel p4 = new Panel();
p4.setLayout(new GridLayout(2,2));
p4.add(new Button("base-1"));
p4.add(new Button("base-2"));
p4.add(new Button("base-3"));
p4.add(new Button("base-4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.setSize(400,300);
frame.setLocation(300,300);
frame.setBackground(Color.blue);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
1. frame是一个顶级窗口!
2. Panel是无法单独显示,必须放在一个容器中。
3. 布局管理器:流式 东西南北中 表格
4. 大小、定位、可见性、背景颜色、监听
4.监听事件
事件监听:当某个事件发生的时候做什么。
public class TestActionEven {
//按下按键触发一些事件
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
MyAction myAction = new MyAction();
//因为addActionListener()需要一个ActionListener的参数,所以我们构建一个ActionListener的实现
button.addActionListener(myAction);
frame.add(button);
frame.pack();
frame.setVisible(true);
WindowClosing(frame);
}
//关闭窗口事件
public static void WindowClosing(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮共享一个事件
public class TestActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
MyMonitor myMonitor = new MyMonitor();
button2.setActionCommand("button2:stop");
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
WindowClosing(frame);
}
//关闭窗口事件
public static void WindowClosing(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand(); 获取信息
System.out.println("按钮被点击了:msg=>" + e.getActionCommand());
}
}
5.输入框TextFiled
如何获取输入框内容通过监听器
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener myActionListener = new MyActionListener();
//按下enter就会触发这个输入框的事件
textField.addActionListener(myActionListener);
//设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();//获得一些资源,返回一个对象
System.out.println(textField.getText());//获得输入框的文本
textField.setText("");//清空命令行
}
}