(1)单个页面的创建
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameDemo1 {
public static void main(String[] args) {
//图形界面
Frame frame = new Frame("图形界面");
//设置界面可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(500, 500);
//设置背景颜色
frame.setBackground(Color.MAGENTA);
//设置界面位置
frame.setLocation(200, 200);
//设置不可缩放
frame.setResizable(false);
//设置界面可以关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
(2)多个页面的创建
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//多个界面的创建
public class FrameDemo2 {
public static void main(String[] args) {
new MyFrame(100, 100, 200, 200, Color.red);
new MyFrame(300, 100, 200, 200, Color.green);
new MyFrame(100, 300, 200, 200, Color.blue);
new MyFrame(300, 300, 200, 200, Color.MAGENTA);
}
}
class MyFrame extends Frame {
static int counter = 1;
public MyFrame(int x, int y, int a, int b, Color color) {
super("界面" + (counter++));
setVisible(true);
setBounds(x, y, a, b);
setBackground(color);
//设置界面可以关闭
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelDemo {
public static void main(String[] args) {
//创建窗体
Frame frame = new Frame("图形界面");
//创建面板
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//Frame设置坐标
frame.setBounds(300, 300, 500, 500);
frame.setBackground(Color.red);
//Panel设置坐标,Frame的相对位置
panel.setBounds(50, 50, 400, 400);
panel.setBackground(Color.green);
frame.add(panel);
frame.setVisible(true);
//适配器模式设置窗口关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FlowLayoutDemo {
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.LEFT));
frame.setSize(300, 300);
frame.setVisible(true);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayoutDemo {
public static void main(String[] args) {
Frame frame = new Frame();
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.setSize(300, 300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
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);
}
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameTestDemo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(300, 300);
frame.setLocation(400,400);
frame.setVisible(true);
frame.setLayout(new GridLayout(2, 1));
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2, 1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2, 2));
panel1.add(new Button("1"), BorderLayout.WEST);
panel1.add(new Button("4"), BorderLayout.EAST);
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel1.add(panel2, BorderLayout.CENTER);
panel3.add(new Button("5"), BorderLayout.WEST);
panel3.add(new Button("0"), BorderLayout.EAST);
for (int i = 6; i < 10; i++) {
panel4.add(new Button("" + i));
}
panel3.add(panel4, BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
}
}