窗口显示
多个窗口显示 (封装)
package GUIstudy;
import java.awt.Color;
import java.awt.Frame;
public class TestFrame2{
public static void main(String[] arg) {
MyFrame myframe1 = new MyFrame(100,100,100,100,Color.black);
MyFrame myframe2 = new MyFrame(200,100,100,100,Color.red);
MyFrame myframe3 = new MyFrame(100,200,100,100,Color.yellow);
MyFrame myframe4 = new MyFrame(200,200,100,100,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);
}
}
单个窗口显示
package GUIstudy;
import java.awt.*;
public class TestFrame {
public static void main(String[] arg) {
Frame frame = new Frame("第一个GUI图形界面");
//设置窗口可见
frame.setVisible(true);
//设置窗口大小
frame.setSize( 400,400);
//设置背景颜色
frame.setBackground(Color.red);
//弹出初始位置
frame.setLocation(500,300);
//设置大小固定
frame.setResizable(false);
}
}
面板
布局
流式布局:
package GUIstudy;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] arg) {
Frame frame = new Frame();
//组件按钮
Button button1 = new Button("11111");
Button button2 = new Button("22222");
Button button3 = new Button("33333");
//设置为流式布局
frame.setLayout(new FlowLayout());
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东南西北中
表格布局