TestFrame
package GUI.lesson01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("first GUI windows");
frame.setVisible(true);
frame.setSize(400,400);
frame.setBackground(new Color(255,255,255));
frame.setLocation(0,0);
frame.setResizable(false);
}
}
TestFrame2
package 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.yellow);
MyFrame myFrame3=new MyFrame(100,300,200,200,Color.red);
MyFrame myFrame4=new MyFrame(300,300,200,200,Color.magenta);
}
}
class MyFrame extends Frame {
static int id =0;
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setResizable(false);
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
面板TestPanel
package 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(Color.green);
panel.setBounds(50,50,400,400);
panel.setBackground(Color.red);
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
}
}
布局管理器
流式布局
package GUI.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("按钮测试");
frame.setSize(400,200);
frame.setLocation(200,200);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
Button button01 = new Button("button01");
Button button02 = new Button("button02");
Button button03 = new Button("button03");
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.add(button01);
frame.add(button02);
frame.add(button03);
frame.setVisible(true);
}
}
东西南北中布局
package GUI.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame=new Frame("TestBorderLayout");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
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(400,400);
frame.setVisible(true);
}
}
表格布局
package GUI.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame=new Frame("TestGridLayout");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
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 GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();
frame.setVisible(true);
}
}