gui01
-
frame是一个顶级窗口
-
panel无法单独显示,必须添加到某个容器中
-
大小、定位、颜色、可见性、监听! (以下例子大部分未写监听,无法关闭)

frame
package kaka;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//frame jdk 看源码
Frame frame=new Frame("我的第一个java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,200);
//设置背景颜色
Color color=new Color(76, 220, 216);
frame.setBackground(color);
//弹出的初始位置
frame.setLocation(500,300);
//设置大小固定
frame.setResizable(false);
}
}
多个frame
package kaka;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
MyFrame frame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame frame2 = new MyFrame(200, 100, 200, 200, Color.red);
MyFrame frame3 = new MyFrame(300, 100, 200, 200, Color.green);
MyFrame frame4 = new MyFrame(400, 100, 200, 200, Color.pink);
MyFrame frame5 = new MyFrame(500, 100, 200, 200, Color.black);
}
}
class MyFrame extends Frame {
static int id = 0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(int x, int y, int w, int h, Color color) {
super("MyFrame" + (++id));
setBackground(color);
setBounds(x, y, w, h);
setVisible(true);
}
}
panel
package kaka;
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.setBackground(Color.pink);
frame.setBounds(300,300,500,500);
panel.setBackground(Color.blue);
panel.setBounds(50,50,400,400);
frame.add(panel);
frame.setVisible(true);
// 监听事件 监听窗口关闭事件
//窗口关闭
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭后需要做的事
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//结束程序
}
});
}
}
三大布局-流式布局
package kaka;
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(FlowLayout.CENTER));
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
三大布局-东西南北中布局
package kaka;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame=new Frame("TestBorderLayout");
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);
}
}
三大布局-表格布局
package kaka;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame=new Frame("TestGridLayout");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
frame.setLayout(new GridLayout(3,2));
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(b4);
frame.add(b5);
frame.pack();//java函数,会自动选择最优秀的布局
frame.setVisible(true);
}
}
布局小练习
package kaka;
import java.awt.*;
public class Work1 {
public static void main(String[] args) {
Frame frame=new Frame("嵌套布局练习");
frame.setLocation(500,100);
frame.setSize(600,600);
Button b1 = new Button("b1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
Button b4 = new Button("b4");
Button b5 = new Button("b5");
Button b6 = new Button("b6");
Button b7 = new Button("b7");
Button b8 = new Button("b8");
Button b9 = new Button("b9");
Button b0 = new Button("b0");
frame.setLayout(new GridLayout(2,1));
//上半部分的面板p1
Panel p1=new Panel(new BorderLayout());
//上半部分中间的面板p2,在p2中加入两行一列的表格
Panel p2=new Panel(new GridLayout(2,1));
p2.add(b2);
p2.add(b3);
p1.add(b1,BorderLayout.WEST);
p1.add(p2,BorderLayout.CENTER);
p1.add(b4,BorderLayout.EAST);
//下半部分的面板p3
Panel p3=new Panel(new BorderLayout());
//下半部分的中间的面板p4,在p4中假如一个两行两列的表格
Panel p4=new Panel(new GridLayout(2,2));
p4.add(b6);
p4.add(b7);
p4.add(b8);
p4.add(b9);
p3.add(b5,BorderLayout.WEST);
p3.add(b0,BorderLayout.EAST);
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
//frame.pack();
frame.setVisible(true);
}
}

浙公网安备 33010602011771号