核心技术:
Swing、AWT
现在不常使用的原因:
- 需要jre环境。
- 界面不美观
那么为什么我们还要学习GUI编程呢?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
//GUI的第一个界面
pub1ic class TestFrame {
pub1ic static void main(String[] args) {
//Frame,JDK,看源码!
Frame frame = new Frame ("我的第一个Java图像界面窗口");
//需要设置可见性w h
frame. setvisible(true);
//设置窗口大小
frame. setsize (400, 400);
//设置背景颜色Color
frame . setBackground(new co1or(85,150, 68));
//弹出的初始位置
frame. setLocati on (200,200);
//设置大小固定
frame . setResizable(false);
}
将代码封装:
package com. kuang. lesson01;
import java. awt.*;
public class TestFrame2 {
pub1ic static void main(String[] args) {
//展示多个窗口new
MyFrame myFrame1 = new MyFrame(100,100, 200, 200, color.b1ue);
MyFrame myFrame2 = new MyFrame(300, 100, 200,200, color. yel1ow);
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,co1or co1or){
super ("Myframe+"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
Panel面板
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame( );
//布局的概念
Panel panel = new Panel();
//设置布局
frame. setlayout(nu11);
//坐标
frame . setBounds( x: 300, y: 300, width: 500, height: 500);
frame. setBackground(new Color( r: 40,g: 161,b:35));
//panel设置坐标,相对于frame
panel. setBounds( x: 50, y: 50, width: 400, height: 400) ;
panel. setBackground(new Color( r: 193,g:15, b: 60));
//frame . add(panel)
frame . add(panel);
frame. setVisible(true);
监听事件,监听窗口关闭事件 System.exit(0);
适配器模式
frame.addWindowListener(new WindowAdapter(){
public void windowclosing(WindowEvent e) {
System.exit(0);
}
});
组件-按钮的使用
流式布局
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button( label: "button1");
Button button2 = new Button( label: "button2");
Button button3 = new Button( label: "button3");
//设置为流式布局(靠左)
frame. setLayout(new FlowLayout(FlowLayout.LEFT));
frame. setSize( width: 200, height: 200);
//把按钮添加上去
frame . add(button1);
frame . add(button2);
frame. add(button3);
frame . setVisible(true);
//frame . add(panel)
//frame . add(panel);
frame. setVisible(true);
东西南北中布局
Frame frame = new Frame( title: "TestBorderLayout");
Button east = new Button( label: "East");
Button west = new Button( label: "West");
Button south = new Button( label: "South" );
Button north = new Button( label: "North");
Button center = new Button( label: "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( width: 200, height: 20d|) ;
frame. setVisible(true);
Frame frame = new Frame( title: "TestGridLayout");
Button btn1 = new Button( label: "btn1");
Button btn2 = new Button( label: "btn2" ) ;
Button btn3 = new Button( label: "btn3");
Button btn4 = new Button( label: "btn4");
Button btn5 = new Button( label: "btn5");
Button btn6 = new Button( label: "btn6");
frame . setLayout(new GridLayout( rows: 3, cols: 2));
frame . add(btn1);
frame . add(btn2);
frame . add(btn3);
frame . add(btn4);
frame . add(btn5);
frame . add(btn6);
frame. pack(); //Java函数,自动化会选择一个最优秀的位置开摆放
frame . setVisible(true);