AWT
1.Frame
封装实现多个Framedd
package com.qiang.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个Frame
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.red);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.gray);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);
}
}
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);
}
}
2. Panel
解决关闭事件
package com.qiang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* @author ma
*/
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(35, 205, 78, 255));
//面板
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(0xE01141));
//frame.add
frame.add(panel);
frame.setVisible(true);
//监听事件 System.exit()
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
3. 布局管理器
-
流式布局
package com.qiang.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件 Button button1 = new B utton("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.add(button1); frame.add(button2); frame.add(button3); frame.setSize(400,400); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { //窗口点击关闭 @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } }
-
东西南北中
package com.qiang.lesson01; import java.awt.*; public class TestBoderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button north = new Button("North"); Button south = new Button("South"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.setSize(200,200); frame.setVisible(true); } }
-
表格布局
package com.qiang.lesson01; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new Gr