Java学习--GUI编程

11 篇文章 0 订阅

核心技术:
Swing、AWT
现在不常使用的原因:

  1. 需要jre环境。
  2. 界面不美观

那么为什么我们还要学习GUI编程呢?

  1. 可以写出自己心中想要的一些小工具
  2. 工作时候,也可能需要维护到swing界面,概率极小!
  3. 了解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(8515068));
//弹出的初始位置
frame. setLocati on (200200);
//设置大小固定
frame . setResizable(false);
}

将代码封装:

package com. kuang. lesson01;
import java. awt.*;
public class TestFrame2 {
pub1ic static void main(String[] args) {
//展示多个窗口new
MyFrame myFrame1 = new MyFrame(100100200200, color.b1ue);
MyFrame myFrame2 = new MyFrame(300100200200, color. yel1ow);
MyFrame myFrame3 = new MyFrame(100300200200, color. red);
MyFrame myFrame4 = new MyFrame(300300200200Color . 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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值