GUI编程-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
GUI编程-创新互联
组件
  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具
简介

Gui的核心技术:Swing AWT

创新互联公司主要从事做网站、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务宽城,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
  1. 因为界面不美观
  2. 需要jre环境
    为什么我们要学习?
  3. 可以写出自己心中想要的一些小工具
  4. 工作时候,也可能需要维护到swing界面,概率极小
  5. 了解MVC架构,了解监听
AWT AWT 介绍【抽象的窗口工具】
  1. 包含了很多类和接口!GUI
    【Eclipse:Java写的】
  2. 元素:窗口,按钮,文本框
    在这里插入图片描述
组件和容器
package com.laomao.lesson01;

import java.awt.*;

// GUi 的第一个界面
public class TestFrame {public static void main(String[] args) {// Frame
        Frame frame = new Frame("我的第一个Java图形界面窗口");

        //需要设置可见性  w  h
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400, 400);

        // 设置背景颜色 Color
        frame.setBackground(new Color(97, 71, 128));

        // 弹出的初始位置
        frame.setLocation(200, 200);

        // 设置大小固定
        frame.setResizable(false);
        
    }
}

在这里插入图片描述
问题:无法关闭窗口。

package com.laomao.lesson01;

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));
        setBackground(color);
        setBounds(x, y, w, h);
        setVisible(true);
    }
}

在这里插入图片描述

面板Panel

解决了关闭问题

package com.laomao.lesson01;

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

// Panel 可以看成是一个空间,但是不能单独存在
public class TestPanel {public static void main(String[] args) {Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(40, 160, 35));

        // panel 设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(193,15,60));

        //frame.add(panel)
        frame.add(panel);

        frame.setVisible(true);

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

    }
}

在这里插入图片描述

布局管理器
  • 流式布局
package com.laomao.lesson01;

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.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setSize(200, 200);

        //把按钮添加上去
        frame.add(button1);
        frame.add(button3);
        frame.add(button2);

        frame.setVisible(true);
    }
}

在这里插入图片描述

  • 东西南北中
package com.laomao.lesson01;

import java.awt.*;

public class TestBorderLayout {public static void main(String[] args) {Frame frame = new Frame();

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

    }
}

在这里插入图片描述

  • 表格式布局
package com.laomao.lesson01;

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.setVisible(true);
    }

}

在这里插入图片描述

Exercise
package com.laomao.lesson01;

import java.awt.*;

public class Exercise {public static void main(String[] args) {Frame frame = new Frame();
        frame.setLayout(new GridLayout(2,1));
        frame.setVisible(true);
        frame.setLocation(300,400);
        frame.setBackground(Color.black);
        frame.setSize(400,300);


        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("Eask-1"),BorderLayout.EAST);
        p1.add(new Button("Wesk-1"),BorderLayout.WEST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        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);


    }
}

在这里插入图片描述

事件监听

当某个事件发生时,干什么?

package com.laomao.lesson02;

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 TestActionEvent {public static void main(String[] args) {// 按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();

        // 因为 addActionListener()需要一个ActionListen,所以我们需要构造7一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();

        windowClose(frame);
        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("aaa");
    }
}

多个按钮,共享一个事件

package com.laomao.lesson02;

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

public class TestActionEvent02 {public static void main(String[] args) {// 两个按钮,实现同一个监听
        // 开始 停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button();
        Button button2 = new Button();

        //可以显示的定义触发会返回的命令,如果不显示定义,这回走默认的值。
        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) {System.out.println("按钮被点击了:msg"+e.getActionCommand());
        if (e.getActionCommand().equals("start")){
        }

    }
}
输入框TextField监听
package com.laomao.lesson02;

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

public class TestTest01 {public static void main(String[] args) {// 启动!
        new MyFrame();
    }
}

class MyFrame extends Frame {public MyFrame(){TextField textField = new TextField();
        add(textField);

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

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

class MyActionListener2 implements ActionListener{@Override
    public void actionPerformed(ActionEvent e) {TextField field = (TextField) e.getSource();  //获得一些资源,返回了一个对象
        System.out.println(field.getText()); // 获得输入框中的文本
        field.setText("");
    }
}
简易计算器

oop原则:组合,大于继承!

package com.laomao.lesson02;

import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;

import javax.swing.*;
import javax.xml.ws.soap.Addressing;
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(){// 3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        // 1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));
        // 1个标签
        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("");
    }
}

组合

class A extends B{}

classA{public B b;
}
package com.laomao.lesson02;

import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;

import javax.swing.*;
import javax.xml.ws.soap.Addressing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCalc {public static void main(String[] args) {new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame {//属性
    TextField num1,num2,num3;

    //方法
    public void loadFrame(){num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener(this));

        // 布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}

//监听器类
class MyCalculatorListener implements ActionListener{//获取计算机这个对象,在一个类中组合另外一个类
    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator){this.calculator = calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {// 1. 获得加数和被加数
        // 2. 将这个值 + 法运算后,放到第三个框
        // 3. 清除前两个框

        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        calculator.num3.setText(""+(n1+n2));
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

完全改造为面向对象【内部类:更好的包装】

package com.laomao.lesson02;

import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;

import javax.swing.*;
import javax.xml.ws.soap.Addressing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCalc {public static void main(String[] args) {new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame {//属性
    TextField num1,num2,num3;

    //方法
    public void loadFrame(){num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener());

        // 布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

    //监听器类
    //内部类大的好处,就是可以畅通无阻的访问外部类的属性和方法
    private class MyCalculatorListener implements ActionListener{@Override
        public void actionPerformed(ActionEvent e) {// 1. 获得加数和被加数
            // 2. 将这个值 + 法运算后,放到第三个框
            // 3. 清除前两个框

            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }
}
画笔
package com.laomao.lesson03;

import java.awt.*;
import java.util.Collections;

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) {//画笔,需要有颜色,画笔可以画画
        g.setColor(Color.red);
        g.drawOval(100,100,100, 100);
        g.fillOval(100,100,100,100);    // 实心圆

        g.setColor(Color.GREEN);
        g.fillRect(150,200,200,200);
        
        // 养成习惯,画笔用完,将他还原到最初的颜色
        g.setColor(Color.black);
    }
}
鼠标监听

目的:想要实现鼠标画画

package com.laomao.lesson03;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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<>();

        setVisible(true);

        // 鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());

    }

    @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 addPoint(Point point){points.add(point);
    }

    private class MyMouseListener extends MouseAdapter{//鼠标 按下,弹起,按住不放

        @Override
        public void mousePressed(MouseEvent e) {MyFrame frame = (MyFrame) e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点!画
            frame.addPoint(new Point(e.getX(), e.getY()));

            // 每次点击鼠标都需要重新画一遍
            frame.repaint();//刷新
        }
    }
}

在这里插入图片描述

窗口监听
package com.laomao.lesson03;

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(){setBackground(Color.BLUE);
        setBounds(100,100,200,200);
        setVisible(true);
        // addWindowListener(new MyWindowListener());
        
        this.addWindowListener(
            new WindowAdapter() {//激活窗口
                @Override
                public void windowActivated(WindowEvent e) {System.out.println("windowActivated");
                }
                // 关闭窗口
                @Override
                public void windowClosing(WindowEvent e) {System.out.println("windowClosing");
                    System.exit(0);
                }
            }
        );
    }
}
键盘监听
package com.laomao.lesson03;

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(1, 2, 300, 400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {// 键盘按下
            @Override
            public void keyPressed(KeyEvent e) {// 获得键盘下的键是哪一个
                int KeyCode = e.getKeyCode();
                System.out.println(KeyCode);    // 不需要去记录这个数值,直接使用静态属性 VK_xxx
                if (KeyCode == KeyEvent.VK_UP) {System.out.println("你按下了上键");
                }
                // 根据按下不同操作,产生不同的结果
            }
        });
    }
}
Swing 窗口、面板
package com.laomao.lesson;

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

public class JFrameDemo {// init();初始化
    public void init(){// 顶级窗口
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);
        jf.setBackground(Color.BLUE);
        //设置文字 Jlabel
        JLabel label = new JLabel("欢迎来到孝义市玩耍!");
        jf.add(label);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //容器实例化
        Container container = jf.getContentPane();
        container.setBackground(Color.blue);


        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {//建立一个窗口
        new JFrameDemo().init();
    }
}
弹窗
package com.laomao.lesson04;

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);

        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("好好学习,天天向上"));
    }

}
标签

label

new JButton("xxx");

图标Icon

package com.laomao.lesson04;

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;
    }
}
面板
package com.laomao.lesson05;

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 panel = new JPanel(new GridLayout(1,3));

        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));

        container.add(panel);

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

    public static void main(String[] args) {new JPanelDemo();
    }
}
按钮
  • 普通按钮(图片按钮)
package com.laomao.lesson05;

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("icon.jpg");
        Icon icon = new ImageIcon(resource);

        // 把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        // add
        container.add(button);

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

    }

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

在这里插入图片描述

  • 单选按钮
package com.laomao.lesson05;

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

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

        //单选框
        JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");

        // 由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton01);
        group.add(jRadioButton02);
        group.add(jRadioButton03);

        container.add(jRadioButton01,BorderLayout.CENTER);
        container.add(jRadioButton02,BorderLayout.NORTH);
        container.add(jRadioButton03,BorderLayout.SOUTH);

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

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

}

在这里插入图片描述

  • 复选按钮
package com.laomao.lesson05;

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

public class JButtonDemo03 extends JFrame {public JButtonDemo03(){Container container = this.getContentPane();

        //多选框
        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();
    }
}

在这里插入图片描述

列表
  • 下拉框
package com.laomao.lesson06;

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

public class TestComboboxDemo01 extends JFrame {public TestComboboxDemo01(){
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();
        
        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已下架");
        status.addItem("即将上映");

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

    public static void main(String[] args) {new TestComboboxDemo01();
    }
}
  • 列表框
package com.laomao.lesson06;

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

public class TestComboboxDemo02 extends JFrame {public TestComboboxDemo02(){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.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,350);
    }

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

应用场景:

  • 选择地区,或者一些单个选项
  • 列表,展示信息。一般是动态扩容
文本框
  • 文本框
package com.laomao.lesson06;

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

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

        JTextField textField = new JTextField("hello");
        JTextField textField2 = new JTextField("world", 20);

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


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

    public static void main(String[] args) {new TestTextDemo01();
    }
}
  • 密码框
package com.laomao.lesson06;

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

public class TestTextDemo02 extends JFrame {public TestTextDemo02(){Container container = this.getContentPane();
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        
        container.add(passwordField);


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

    public static void main(String[] args) {new TestTextDemo02();
    }
}
  • 文本域

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页名称:GUI编程-创新互联
URL链接:http://scgulin.cn/article/pdseo.html