JAVA图形化基础--GUI编程


1、GUI简介

GUI的核心技术:

  • AWT: 抽象窗口工具包,早期编写图形界面应用程序的包
  • Swing : 为解决 AWT 存在的问题而新开发的图形界面包;Swing是对AWT的改良和扩展

GUI因界面不美观,且运行需要jre环境(占用内存大,繁琐)被逐渐淘汰,现阶段学习GUI主要是为了了解 MVC架构(了解监听),写一些小工具,和工作中可能需要维护Swing界面。


2、AWT

2.1 AWT介绍

  1. 包含了很多类和接口! GUI:图形用户界面编程
  2. 元素:窗口,按钮,文本框
  3. 包都在java(awt)包

2.2 组件和容器

在这里插入图片描述
Frame框架

import java.awt.*;
//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //看源码 选中+ctrl+左键
        //看结构 alt+7
        Frame frame = new Frame("我的第一个Java图形界面窗口");//Frame 是一个顶级窗口
        //Frame的方法
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色 Color
        frame.setBackground(new Color(51, 158, 22));
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
        //窗口无法关闭!!!最小化、最大化、窗口尺寸已经默认存在!
    }
}
import java.awt.*;
public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
        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,Color color){
        super("Myframe+"+(++id));//继承
        setBounds(x, y, w, h);//设置坐标
        setBackground(color);//设置颜色
        setVisible(true);//设置可见性
    }
}

面板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();  //new 窗口
        //布局的概念
        Panel panel = new Panel();  //new 面板
        Panel panel1 = new Panel();
        //设置布局,不设置面板会置顶
        frame.setLayout(null);
        //窗口坐标和颜色
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(140, 208, 212));
        //panel 设置坐标,相对于frame
        panel.setBounds(50,50,400,100);
        panel.setBackground(new Color(181, 186, 54));
        panel1.setBounds(50,200,400,250);
        panel1.setBackground(new Color(165, 34, 101));
        //将panel添加进frame
        frame.add(panel1);//Panel 无法单独显示,必须添加到某个容器中
        frame.add(panel);
        frame.setVisible(true);
		//监听时间,监听窗口关闭事件
        //适配器模式(二十三种设计模式)
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

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(0));	0为左,1为中...
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//两种方式
        frame.setSize(200,200);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}

东西南北中布局

import java.awt.*;
public class TestBorderLayout {
    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.setSize(400,400);
        //不同布局的方位
        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);
    }
}

栅格布局

import java.awt.*;
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        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.pack();//Java函数,自适应,用于优化大小;
        // frame.setSize(400,400);
        frame.setVisible(true);
    }
}

2.4 事件监听

ActionListener

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TesActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();
        /*按钮可以new一个接口,需要命名内部类,把他的实现类写下来。但一般不这么做
        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {  }  });*/
        
        //因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
        //接口就写实现类,父类就继承
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button,BorderLayout.CENTER);
        windowClose(frame);
        frame.pack();
        frame.setVisible(true);
    }
    //关闭窗体的事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyActionListener implements ActionListener{
    //事件监听
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("事件监听,您按下了按钮");
    }
}

多按钮共享一个事件

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始--停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
      // button2.setActionCommand("button2-stop");
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        /*e.getActionCommand() 获取按钮的信息
        System.out.println("按钮被点击了:msg=>"+e.getActionCommand());
        输出结果butoo2为"按钮被点击了:msg=>stop";可以显示的定义触发会返回的命令
        butoo1为"start"。无显示定义,则会走默认的值。
        */
        //可以多个按钮只写一个监听类
        if (e.getActionCommand().equals("start")){//equals 等号
            System.out.println(e.getActionCommand()+"按钮被点击");
        } if (e.getActionCommand().equals("stop")){
            System.out.println(e.getActionCommand()+"按钮被点击");
        }
    }
}

输入框TextField监听

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();//文本 TextArea文本域,可以写多行
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class MyActionListener2 implements ActionListener{//监听器
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField)e.getSource();//获得一些资源,返回的一个对象
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");//设置enter 后的状态
    }
}

2.5 简易计算器

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class  Calculator extends Frame{
    public Calculator() {
        //三个文本框
        TextField num1 = new TextField(10);//字符数
        TextField num2 = new TextField(10);//字符数
        TextField num3 = new TextField(20);//字符数
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1,num2,num3));
        //一个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取三个变量
    private TextField num1,num2,num3;

    public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        //2.将这个值+法运算后,放到第三个框
        num3.setText(""+(n1+n2));
        //3.清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

2.6 画笔

import java.awt.*;
public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame {
    public void loadFrame(){
        setBounds(200,200,600,500);
        setVisible(true);
    }
    //画笔,颜色,可以画画
   @Override
    public void paint(Graphics g) {
       // super.paint(g);有些类里面有初始化操作,就无法删除
       g.setColor(Color.red);
       //g.drawOval(100,100,100,100);   //draw空心
       g.fillOval(100,100,100,100);   //fill实心、填充的
       g.setColor(Color.GREEN);
       g.fillRect(200,200,200,200);
       //养成习惯,画笔用完,将它还原到最初的颜色,不然你再画一个图会带上之前的颜色。
    }
}

2.7 鼠标监听

在这里插入图片描述

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

//鼠标监听事件
public class TestMouseListener {	
    public static void main(String[] args) {
    new MyFrame("画图");
    }
}
//自己的类
class MyFrame extends Frame{		
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;

    public MyFrame(String title) {//--------框架
        super(title);       //名字                                           
        setBounds(200, 200, 400, 300);
        //存标点击的点
        points = new ArrayList<>();
        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());        //-监听鼠标

        setVisible(true);
    }
    @Override				
    public void paint(Graphics g) { //画画                  //--------------画笔存储实施
        //画画,需要监听鼠标的事件
        Iterator iterator = points.iterator();              //-迭代器
        while (iterator.hasNext()){                         //检查序列中是否还有元素
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //添加一个点到界面上,点集合		
    public void addPaint(Point point){
        points.add(point);                  //将(点)传到迭代器里


    }

    //适配器模式,就是别人已经写好的端口,不用全部重写内部类,直接继承更加方便。
    private class MyMouseListener extends MouseAdapter{      //----------监听器
            //鼠标,按下,弹起,按下不放。
            @Override
            public void mousePressed(MouseEvent e) {        //-鼠标按下
               MyFrame frame = (MyFrame) e.getSource();     //-鼠标按下的来源
                //这里我点击的时候,就会在界面上产生一个点
                //这个点就是鼠标的点
                frame.addPaint(new Point(e.getX(),e.getY()));//--将监控的(点的坐标)传到点集合
                //每次点击鼠标都需要重写画一遍
                frame.repaint();                             //再次刷漆
            }
        }
}            

2.8 窗口监听

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

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame("窗口监听");
    }
}
class WindowFrame extends Frame{
    public WindowFrame(String kk) {
        super(kk);
        setBackground(Color.cyan);
        setBounds(100,100,200,200);
        setVisible(true);
        //匿名内部类
        this.addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowOpened(WindowEvent e) {
                        System.out.println("窗口打开");
                    }
                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println("窗口关闭中");
                    }
                    @Override
                    public void windowActivated(WindowEvent e) {
                        System.out.println("窗口激活");
                        WindowFrame source = (WindowFrame) e.getSource();       //获取框架信息
                        source.setTitle("被激活了");
                    }
                    @Override
                    public void windowStateChanged(WindowEvent e) {
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("状态改变了");
                        System.out.println("窗口状态改变");
                    }
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("窗口关闭");
                        System.exit(0);
                    }
                }
        );
    }
   /* 通过匿名内部类可以不用另外创建类
   class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);      //隐藏窗口,通过按钮点击事件
            System.exit(0);         //0正常退出,1非正常退出
        }
    }*/
}

2.9 键盘监听

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键盘
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame() {
        setBounds(10,10,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得当前键盘的码
                int keyCode = e.getKeyCode();       //不需要去记录这个数值,直接使用静态属性VK_XXX
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP){     //KeyEvent.VK 按键类
                    System.out.println("你按下了上键!");
                }
                //根据按下不同操作,产生不同结果。
            }
        });
    }
}


3、Swing

3.1 窗口、面板

import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
    //init();初始化
    public void init(){
        //顶级窗口
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        //jf.setBackground(Color.cyan);     因为在容器中,直接颜色没效果,需要容器实例化
        jf.setBounds(100,100,200,200);
        JLabel jLabel = new JLabel("欢迎学习Java GUI");  //标签
        jf.add(jLabel);
        //让文本标签居中,设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //需要容器实例化,颜色才能现象
        Container contentPane = jf.getContentPane();
        contentPane.setBackground(Color.cyan);
        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

3.2 弹窗

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setVisible(true);      //可见
        this.setSize(700,500);          //尺寸
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);       //关闭事件
        //JFrame 放东西,容器
        Container container = this.getContentPane();
        //绝对布局,会相对容器自动定位
        container.setLayout(null);
        //按钮
        JButton button = new JButton("点击弹出一个对话框");      //创建
        button.setBounds(30,30,200,50);
        //点击这个按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() {           //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //监听弹窗
                new MyDialogDemo();
            }
        });
        container.add(button);      //将按钮放进容器中
    }
    public static void main(String[] args) {
        new DialogDemo();
    }
}
        //弹窗的窗口
class MyDialogDemo extends JDialog{
            public MyDialogDemo() {
                this.setVisible(true);
                this.setBounds(100,100,500,500);
           // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);    弹窗可以被关掉,不需要额外添加事件
                Container container = this.getContentPane();
                container.setLayout(null);

                container.add(new Label("Java弹窗"));
            }
        }

3.3 标签

/*图标ICON*/
import javax.swing.*;
import java.awt.*;

//图片,需要实现类,Frame 继承
public class IconDemo extends JFrame implements Icon {

    private int width;
    private int height;

    public IconDemo(){}

    public IconDemo(int width,int height){
        this.width = width;
        this.height = height;
    }


    public void init(){     //图标
        IconDemo iconDemo = new IconDemo(15,15);
        //图标放在标签上,也可以放在按钮上!
        //标签,图标,位置
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        new IconDemo().init();
    }
    @Override       //图标尺寸
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }
}
/* 图片 */
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {
    public ImageIconDemo()  {
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("xxx.jpg");//获取当前类以下的东西

        ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container contentPane = getContentPane();
        contentPane.add(label);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,300,500);
    }
    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

3.4 面板

import javax.swing.*;
import java.awt.*;


public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();
        //后面参数的意思,面板与面板的间距
        container.setLayout(new GridLayout(2,1,10,10));

        JPanel panel1 = new JPanel(new GridLayout(1, 3));   //GridLayout网格布局
        JPanel panel2 = new JPanel(new GridLayout(2, 1));
        JPanel panel3 = new JPanel(new GridLayout(2, 3));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        new JPanelDemo();
    }
}

3.5 边框、文本域

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("边框、文本域");
        //Scroll 面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setBounds(100,100,300,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

3.6 按钮

图片按钮

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("123.jpg");  //图片路径
        ImageIcon icon = new ImageIcon(resource);       //转换为图标

        //把这个图标放到按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");      //图片按钮提示
        //add
        container.add(button);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

单选按钮

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo02 extends JFrame{
    public JButtonDemo02() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("123.jpg");  //图片路径
        ImageIcon icon = new ImageIcon(resource);       //转换为图标

        //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        //由于单选框只能选择一个,可以将他们分组,一个组只能选一个。
        ButtonGroup group = new ButtonGroup();      //组
        group.add(radioButton1);                    
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

复选按钮

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("123.jpg");  //图片路径
        ImageIcon icon = new ImageIcon(resource);       //转换为图标

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        
        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        new JButtonDemo03();
    }
}

3.7 列表

下拉框

/*选择地址或者一些单个选项(一到两个最好使用按钮,两个以上使用下拉框,节省内存布局)*/
import javax.swing.*;
import java.awt.*;

public class TsetComboboxDemo01 extends JFrame {
    public TsetComboboxDemo01() {
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在上映");
        status.addItem("已下架");
        status.addItem("即将上映");

        // status.addActionListener(); 监听获取值

        container.add(status);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TsetComboboxDemo01();
    }
}

列表框

/*展示信息,一般是动态扩容。*/
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class TsetComboboxDemo02 extends JFrame {
    public TsetComboboxDemo02() {
        Container container = this.getContentPane();
        //生产列表的内容
        //String[] contents = {"1","2","3"};    静态数组

        Vector contents = new Vector();

        //列表中需要放入内容
        JList jList = new JList(contents);      //列表

        //动态数组
        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");
        
        container.add(jList);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TsetComboboxDemo02();
    }
}

3.8 文本框

文本框 TextField

import javax.swing.*;
import java.awt.*;

public class TestTextDemo01 extends JFrame  {
    public TestTextDemo01() throws HeadlessException {
        Container container = this.getContentPane();


        JTextField textField1 = new JTextField("hello",50);    //文本框+尺寸
        JTextField textField2 = new JTextField("world");

        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

密码框 PasswordField

import javax.swing.*;
import java.awt.*;

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02() throws HeadlessException {
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();    //密码框***
        //passwordField.setEchoChar('*');                         //密码框显示符号

        container.add(passwordField);


        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}

文本域 TextArea

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("文本域 TextArea");
        //Scroll 面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


        this.setBounds(100,100,300,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

4、常用类

Frame;											框架
Panel;											面板
setVisible;									    可见性true
setSize(x,x);									初始尺寸
setLocation(x,x);								初始位置,x,y
setBounds(x,x,x,x);								初始坐标+尺寸
setBackground(new color(x,x,x);   		    颜色,三基色
setResizable;								    大小是否可调,true,false
setLayout(new FlowLayout(FlowLayout.LEFT));		流式布局
frame.add(east,BorderLayout.EAST);				方向布局
frame.setLayout(new GridLayout(3,2));			表格布局
ActionListener;									监听器
TextField;										文本框
TextArea;										文本域
PasswordField;									密码框
Integer.parseInt();								String类转int类
paint;											画笔
MouseAdapter;									鼠标监听器
WindowListener;									窗口监听
KeyListener;									键盘监听
DefaultCloseOperation(WindowConstants.);		关闭事件(JFrame)
ContentPane;									容器(JFrame)
Layout;											容器自动定位(JFrame)
Button;											按钮
RadioButton;									单选按钮
ButtonGroup;CheckBox;										多选按钮
ComboBox;										下拉框
List;											列表框
Dialog;											对话框
Label;											标签
IconDemo;										图标
ImageIcon;										图片
Scroll;											滚动条
Timer;											定时器

(部分内容参考【狂神说Java】GUI编程入门到游戏实战)

  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Java GUI图形界面编程是指使用Java编程语言来创建图形用户界面(Graphical User Interface,GUI)应用程序的过程。Java提供了一组丰富的类库和工具,使得开发人员可以快速、方便地创建交互性强、用户友好的应用程序。 Java GUI图形界面编程的主要组成部分是SwingJavaFX两个库。Swing是一个基于Java的图形用户界面工具包,提供了丰富的用户界面组件,如按钮、文本框、表格等,可以在应用程序中创建和管理这些组件,实现用户界面的交互。JavaFX是Java平台上的富客户端应用程序框架,提供了更强大、更灵活的界面组件和布局控制,可以创建更加先进、更具吸引力的GUI应用程序。 使用Java GUI图形界面编程进行应用程序开发有许多优点。首先,Java GUI库提供了丰富的组件和控件,开发人员可以根据自己的需求选择合适的组件,快速构建出用户界面。其次,Java GUI库具有良好的跨平台性,可以在不同操作系统上运行,并且具有相同的用户界面。再者,Java提供了强大的事件驱动模型,使得应用程序能够响应用户的操作,实现交互性。 总之,Java GUI图形界面编程是一种灵活、方便、强大的开发方式,使得开发人员能够更加简单地创建出功能强大、用户友好的应用程序。无论是简单的桌面应用还是复杂的企业级应用,都可以通过Java GUI图形界面编程来实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会思想的苇草i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值