GUI编程
1. AWT的组件和容器
1.1 Frame窗口
案例一
package com.GUI.lesson01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("图像界面窗口");
frame.setVisible(true);
frame.setSize(400,400);
frame.setBackground(new Color(229, 214, 118));
frame.setLocation(200,200);
frame.setResizable(false);
}
}
案例二
package com.GUI.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.CYAN);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.magenta);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.YELLOW);
}
}
class MyFrame extends Frame{
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame+"+(++id));
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
setResizable(false);
}
}
1.2 Panel面板
解决了关闭事件:适配器监听!
package com.GUI.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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(84, 124, 238));
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(200, 84, 238));
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
1.3 布局管理器
(1)流式布局
package com.GUI.lesson01;
import java.awt.*;
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());
frame.setBounds(200,200,200,200);
frame.setVisible(true);
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
}
frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
(2)东西南北中布局
package com.GUI.lesson01;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayoout");
Button east = new Button("East");
Button wast = new Button("Wast");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(wast,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setBounds(400,400,400,400);
frame.setVisible(true);
}
}
(3)表格布局
package com.GUI.lesson01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayoout");
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();
frame.setBounds(400,400,400,400);
frame.setVisible(true);
}
}
1.4 作业
package com.GUI.lesson01;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(400,400,400,300);
frame.setBackground(Color.white);
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("Wast-1"),BorderLayout.WEST);
p2.add(new Button("p2-1"));
p2.add(new Button("p2-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("East-1"),BorderLayout.EAST);
p3.add(new Button("Wast-1"),BorderLayout.WEST);
p4.add(new Button("p4-1"));
p4.add(new Button("p4-2"));
p4.add(new Button("p4-3"));
p4.add(new Button("p4-4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1,BorderLayout.NORTH);
frame.add(p3,BorderLayout.SOUTH);
}
}