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

Java GUI编程

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

Java GUI编程

1.简介

GUI的核心技术:Swing、AWT

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

为什么我们要学习?

  1. 可以写出自己心中想要的一些小工具
  2. 工作时候,可能需要维护到Swing界面,概率极小
  3. 了解MVC架构,了解监听
2.AWT 2.1.AWT介绍

1.包含了很多类和接口!
GUI:图形用户界面编程
Eeclipse:JAVA

2.元素:窗口、按钮、文本框
3.Java.awt

2.2.组件和容器

2.2.1 frame
import java.awt.*;

//gui的第一个界面
public class Testframe {
    public 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 Color(85,150,85));
        //弹出的初始位置
        frame.setLocation(200,200);
        //弹出大小固定
        frame.setResizable(false);
    }
}

结果显示:

import java.awt.*;

public class Testframe1 {
    public static void main(String[] args) {
    //展示多个窗口 new
        Myframe myframe0 = new Myframe(100,100,200,200,Color.blue);
        Myframe myframe1 = new Myframe(300,100,200,200,Color.YELLOW );
        Myframe myframe2 = new Myframe(100,300,200,200,Color.PINK );
        Myframe myframe3 = new Myframe(300,300,200,200,Color.RED );
    }
}
class Myframe extends frame {
    static int id=0;//可能存在多个窗口,我们需要一个计数器
    public Myframe (int x,int y ,int w,int h,Color color){
        super("myfame+"+(++id));
        setBackground(color) ;
        setBounds(x,y,w,h);
        setVisible(true);

    }
}

结果显示:

发现问题:
窗口关闭不掉

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

//Panel可以看成是一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        frame frame = new frame();
        //布局的概念
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(100, 100, 500, 500);
        frame.setBackground(new Color(22, 129, 73));
        //panel 设置坐标,相对于frame
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(229, 234, 232));

        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件 System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            //窗口点击关闭的时候需要做的事情
            public void windowClosing(WindowEvent e) {
                System.exit(0);//结束程序
            }
        });

    }
}

结果显示:

解决问题:
监听窗口关闭后就解决了窗口关闭不了的问题

2.2.3 布局管理器

- 流式布局

import java.awt.*;

public class TestFlowLayout {
    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.CENTER));
        frame.setSize(200,200);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}

X
效果:

- 东西南北中

import java.awt.*;

public class TestBoaderLayout {
    public static void main(String[] args) {
        frame frame = new frame();
        Button button1 = new Button("EAST");
        Button button2 = new Button("WEST");
        Button button3 = new Button("SOUTH");
        Button button4 = new Button("NORTH");
        Button button5 = new Button("CENTER");
        
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}

效果:

- 表格布局

import java.awt.*;

public class TestGridLayout {
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        
        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.pack();//java函数 自动布局
        frame.setSize(500,500);
        frame.setVisible(true);

    }
}

效果:

练习题:

构思:

四个面板,frame按照表格布局分成上下两部分(两个面板),上部分的中间又一个面板,左右两边的按钮按照东西布局,面板里的按钮按照表格布局。
下部分的中间又一个面板,左右两边的按钮按照东西布局,面板里的按钮按照表格布局。

import java.awt.*;

//练习题的讲解
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

//练习题的讲解
public class ExDemo {
    public static void main(String[] args) {
        //总frame
        frame frame = new frame();
        frame.setVisible(true);
        frame.setSize(400,300);
        frame.setLocation(300,400) ;
        frame.setBackground(new Color(255, 255, 255) ) ;
        frame.setLayout(new GridLayout(2,1));
        //四个面板
        Panel p1 = new Panel(new BorderLayout() );
        Panel p2 = new Panel(new GridLayout(2,1) );
        Panel p3 = new Panel(new BorderLayout() );
        Panel p4 = new Panel(new GridLayout(2,2) );

        //上面
        p1.add(new Button("East-1"),BorderLayout.EAST );
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("P11") );
        p2.add(new Button("P12") );
        p1.add(p2,BorderLayout.CENTER);

        //下面
        p3.add(new Button("East-2"),BorderLayout.EAST );
        p3.add(new Button("West-2"),BorderLayout.WEST);
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-"+i) );
        }
        p3.add(p4,BorderLayout.CENTER ) ;

        frame.add(p1);
        frame.add(p3);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

效果:

2.2.4 事件监听
  • 事件监听:当某个事件发生的时候,干什么?
2.2.5 输入框 TexField 监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText01 {
    public static void main(String[] args) {
        //启动!!
        new Myframe();
    }
}
class Myframe extends frame {
    public Myframe(){
        TextField textField = new TextField();
        add(textField);

        //监听这个文本框输入的文字
        MyActionListener myActionListener = new MyActionListener();
        //按下enter就会触发这个输入框的事件
        textField.addActionListener(myActionListener) ;

        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack() ;

    }
}
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field=(TextField) e.getSource();//获得一些资源,返回一个对象
        System.out.println(field.getText());//获得输入框的文本
        field.setText(" ");
    }
}
总结

3.Swing
// An highlighted block
var foo = 'bar';
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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