组件与容器
1.Frame
- 一些方法
package com.yuecheng.guistudy;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] ages){
//Frame.JDK. 看源码
Frame frame = new Frame("我的第一个java图像界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小 w h
frame.setSize(400,400);
//设置背景颜色 Color
//frame.setBackground(Color.black);
frame.setBackground(new Color(238, 201, 88));
//弹出的初识位置 注0.0点在左上角
frame.setLocation(255,255);
//设置大小固定 setResizable() 默认为true可以改变打小窗口(可拉伸,改成false即为不可拉伸)
frame.setResizable(false);
}
}
- 通过封装快速创建窗口
package com.yuecheng.guistudy;
import java.awt.*;
public class TestFrame01 {
public static void main(String[] args) {
new A1(100,300,300,200,Color.YELLOW);
new A1(300,300,300,200,Color.BLUE);
new A1(100,500,300,200,Color.MAGENTA);
new A1(300,500,300,200,Color.RED);
}
}
class A1 extends Frame{
static int id =0;//可能存在多个,所以做个计数器
public A1(int x ,int y ,int w,int h,Color color){
super("窗口"+(++id));
setVisible(true);
setBounds(x,y,w,h);
setBackground(color);
}
}
2.面板panel
package com.yuecheng.guistudy;
import java.awt.*;
// panel,可以看成一个空间,但是不能单独存在
public class TestPanel1 {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标与宽高
frame.setBounds(500,500,500,500);
//颜色
frame.setBackground(new Color(116, 43, 255));
// Panel设置坐标,相对于frame
//注意:面板必须在窗口内,也就是frame内添加面板(panel)
panel.setBounds(50,50,300,350);
panel.setBackground(new Color(255, 190, 190));
//注意:面板必须在窗口内,也就是frame内添加面板(panel)
frame.add(panel);
//设置可见性
frame.setVisible(true);
}
}
产生问题:发现窗口关闭不了,只能通过停止才能关闭
解决问题:
package com.yuecheng.guistudy;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// panel,可以看成一个空间,但是不能单独存在
public class TestPanel1 {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标与宽高
frame.setBounds(500,500,500,500);
//颜色
frame.setBackground(new Color(116, 43, 255));
// Panel设置坐标,相对于frame
//注意:面板必须在窗口内,也就是frame内添加面板(panel)
panel.setBounds(50,50,300,350);
panel.setBackground(new Color(255, 190, 190));
//注意:面板必须在窗口内,也就是frame内添加面板(panel)
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件:System.exit(0)
//适配器模式
frame.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局管理器
-
流式布局
-
package com.yuecheng.guistudy; import java.awt.*; public class TestFlowLayout01 { 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()); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setSize(500,500); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中
package com.yuecheng.guistudy;
import java.awt.*;
public class TestFlowLayout01 {
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.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// frame.setSize(500,500);
frame.setSize(500,500);
//设置东西南北中
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.setVisible(true);
}
}
- 表格式布局Grid
package com.yuecheng.guistudy;
import java.awt.*;
public class Grid {
public static void main(String[] args) {
Frame frame = new Frame();
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.setSize(500,500);
frame.setVisible(true);
}
}
HOME WORK
package com.yuecheng.guistudy;
import com.yuecheng.oop.Demo05.B;
import java.awt.*;
public class t1 {
public static void main(String[] args) {
Frame frame = new Frame("1111111111");
//把最大的框,分为上下两块
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
frame.setBounds(300,400,400,300);
//四个面板
//创建一个panel1面板,选择布局,东西南北中
//创建一个panel2面板,并选择两行一列布局
Panel panel1 = new Panel(new BorderLayout());
//新建p2面板,布局为2行1列
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
//新建p4,面板,布局为2行两列
Panel panel4 = new Panel(new GridLayout(2,2));
//上面ok
//new 两个按钮,放在东西,new a,b,两个放在panel2中
panel1.add(new Button("111"),BorderLayout.EAST);
panel1.add(new Button("222"),BorderLayout.WEST);
panel2.add(new Button("a"));
panel2.add(new Button("b"));
//将面板2放入面板1
panel1.add(panel2,BorderLayout.CENTER);
//下面
//东西两个按钮
panel3.add(new Button("555"),BorderLayout.EAST);
panel3.add(new Button("666"),BorderLayout.WEST);
//中间四个
//两行两列四个,用for循环一一放入面板4
for (int i = 0; i < 4; i++) {
panel4.add(new Button("FOR~"+i));
}
//将面板4放入面板3中,布局为居中
panel3.add(panel4,BorderLayout.CENTER);
//将p1,p2两个面板放入frame容器
frame.add(panel1);
frame.add(panel3);
}
}
总结
- Frame是一个顶级窗口,
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器(1.流式,2.东西南北中,3.表格)
- 大小。定位,背景颜色,可见性
- 事件监听(关闭窗口)
frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } });