栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习笔记 GUI编程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习笔记 GUI编程

GUI:图像用户界面

 

Awt介绍
  • Abstract windows tools ,包含了很多的类和接口
  • 元素:窗口、按钮、文本框
  • java.awt包

 如何使用这些类??

组件和容器

.setVisible(true/false)

.setSize(x,y) 窗口大小

.setBackground(new Color(R,G,B,透明度))

.setLocation(x,y) 默认左上角

.setResizable(true/false); 能否改变窗口大小

import java.awt.*;
//GUI的第一个类
public class Testframe {
    public static void main(String[] args) {
        //frame
        frame f = new frame("我的第一个Java图形界面窗口");
        //可见性 默认没有长宽高
        f.setVisible(true);
        //设置窗口大小
        f.setSize(400,400);
        //颜色
        f.setBackground(new Color(34, 175, 232, 255));
        //弹出的初始位置(默认最中间),0,0点在最左上角
        f.setLocation(200,200);
        //设置大小固定
        f.setResizable(false);
    }
}
Panel面板

特性:可以看成一个空间,在frame上,不能单独存在。

package com.Gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestPanel {    
    public static void main(String[] args) {
        frame f = new frame();
        Panel p = new Panel();

        f.setLayout(null);
        f.setBounds(300,300,500,500);
        f.setBackground(new Color(107, 208, 121));
        //设置面板panel坐标,相对于frame
        p.setBounds(50,50,400,400);
        p.setBackground(new Color(255, 0, 0));
        f.setVisible(true);
        f.add(p);
        p.setVisible(true);

        f.addWindowListener(new WindowAdapter() {
            //点击窗口关闭时做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);

            }
        });


    }
}
三种布局管理器 流式布局
package com.Gui;

import java.awt.*;

public class WindowLayout {
    public static void main(String[] args) {
        frame frame = new frame();
        //按钮
        Button button1 = new Button("固傻");
        Button button2 = new Button("固傻1");
        Button button3 = new Button("gusha");
        //设置流式布局
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(200,200);
        frame.setVisible(true);
        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

    }


}

 东西南北中
package com.Gui;

import java.awt.*;

public class WindowLayout {
    public static void main(String[] args) {
        frame frame = new frame("GUSHA");

        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(300,600);
        frame.setVisible(true);
    }


}

 

表格布局

案例:做出如图所示的画面:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346480.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号