GUI基础知识

2023-11-16

GUI编程

1,简介

  1. 图形用户界面,Graphical User Interface,又称图形用户接口,是指采用图形方式显示的计算机操作用户界面。
  2. GUI的核心技术:AWT,Swing

2,Awt

2.1 AWT简介

  1. AWT:Abstract Window Toolkit,抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的GUI的基本工具
  2. 包含了很多类和接口
  3. 元素:窗口,按钮,文本框
  4. java.awt
    在这里插入图片描述

2.2 组件与容器

1, Frame
package com.ym.lesson01;

import java.awt.*;

public class FrameTest {
    public static void main(String[] args) {
        //Frame类    看源码
        Frame frame = new Frame("我的第一个图形用户界面窗口");

        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(Color.blue);
        //设置弹出的初始位置
        frame.setLocation(200,200);
        //设置窗口大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述

生成多个Frame

package com.ym.lesson01;

import java.awt.*;

public class FrameTest2 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.green);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.green);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.green);
        MyFrame myFrame4 = new MyFrame(300, 300, 400, 400, Color.green);

    }
}
//封装
class MyFrame extends Frame{
    static int id=0;
   public  MyFrame(int x,int y,int w,int h,Color color){
       super("MyFrame"+(++id));
       //setLocation(200,200);
       //setSize(400,400);
       setBounds(x,y,w,h);
       setBackground(color);
       setVisible(true);
       setResizable(false);
    }
}

在这里插入图片描述

2,Panel

解决了窗口关闭问题

package com.ym.lesson01;

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

public class PanelTest {
    public static void main(String[] args) {

        Frame frame = new Frame("frame");
        Panel panel = new Panel();

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

        //frame坐标,背景颜色
        frame.setBounds(100,100,200,200);
        frame.setBackground(new Color(35, 128, 90));

        //panel坐标,背景颜色
        panel.setBounds(50,50,50,50);
        panel.setBackground(new Color(55, 91, 128));

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

        frame.setVisible(true);
        frame.setResizable(false);

        //监听事件,监听窗口关闭事件 system.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
//                super.windowClosing(e);
                //结束进程
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

2.3,布局管理器

2.3.1 流式布局,FlowLayout
package com.ym.lesson01;

import java.awt.*;

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

        Button button1 = new Button("1");
        Button button2 = new Button("2");
        Button button3 = new Button("3");

        frame.setBounds(100,100,200,200);
        frame.setVisible(true);

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        //FlowLayout
//        frame.setLayout(new FlowLayout());//默认center
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//左
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右
    }
}

在这里插入图片描述

3.2 东南西北中,BorderLayout
package com.ym.lesson01;

import java.awt.*;

public class BorderLayoutTest {
    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.setBounds(100,100,100,100);
        frame.setVisible(true);
    }
}

在这里插入图片描述

2.3.3 表格布局,GridLayout
package com.ym.lesson01;

import java.awt.*;

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

        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.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.setLayout(new GridLayout(3,2));

        frame.setVisible(true);
//        frame.setSize(200,200);
        frame.pack();//java中的一个函数,确定frame的最佳大小
    }
}

在这里插入图片描述

2.3.4 练习

在这里插入图片描述

package com.ym.lesson01;

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

public class Test {
    public static void main(String[] args) {

        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setBounds(100,100,400,300);
        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));
        Panel p4 = new Panel();
        p4.setLayout(new GridLayout(2,2));

        p1.add(new Button("p1_btn_west"),BorderLayout.WEST);
        p1.add(new Button("p1_btn_east"),BorderLayout.EAST);

        p2.add(new Button("p2_btn_top"));
        p2.add(new Button("p2_btn_bottom"));

        p1.add(p2,BorderLayout.CENTER);


        //下面
        p3.add(new Button("p3_btn_west"),BorderLayout.WEST);
        p3.add(new Button("p3_btn_east"),BorderLayout.EAST);

        p4.add(new Button("p4_btn_top1"));
        p4.add(new Button("p4_btn_top2"));
        p4.add(new Button("p4_btn_bottom1"));
        p4.add(new Button("p4_btn_bottom2"));

        p3.add(p4,BorderLayout.CENTER);

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

在这里插入图片描述

2.4, 事件监听

package com.ym.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 ActionEventTest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();
//因为addActionListener()需要一个ActionListener,
//所以构造了一个MyActionListener类
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

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

       windowClose(frame);
    }

    //关闭窗口方法
    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("111");
    }
}
2.4.1 多个按钮共享一个事件
package com.ym.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 ActionEventTest2{
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn1 = new Button("start");
        Button btn2 = new Button("stop");

        btn1.setActionCommand("btn1_start");//设置标签值

        MyListener myListener = new MyListener();
        btn1.addActionListener(myListener);
        btn2.addActionListener(myListener);

        frame.add(btn1,BorderLayout.NORTH);
        frame.add(btn2,BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.pack();

    }
static class MyListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());//getActionCommand()得到按钮上的label
        if(e.getActionCommand().equals("btn1_start")){
            System.out.println("点击了开始btn1_start");
        }else{
            System.out.println("点击了结束stop");
        }
    }
}

}

2.5,输入框TextField

package com.ym.lesson02;

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

public class TextFieldTest {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }
}

class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        MyTextListener myTextListener = new MyTextListener();
        //按下回车键就会触发事件
        textField.addActionListener(myTextListener);
        //输入框中的文本替换为*,但控制台显示的是文本,不是*
        textField.setEchoChar('*');

        pack();
        setVisible(true);
    }
}
class MyTextListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getSource());//e.getSource()得到资源,返回object
        TextField textField = (TextField) e.getSource();
        System.out.println(textField.getText());//获得输入框中的文本
        textField.setText("");//按下回车就会清空输入框中的内容
    }
}

2.6,简易计算器

在这里插入图片描述

package com.ym.lesson02;

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

//简易计算器
public class CalcTest {
    public static void main(String[] args) {
        Calculator calculator = 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("+");

        //流式布局,FlowLayout
        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("");
    }
}
改造为面向对象写法(组合)
package com.ym.lesson02;

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

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

//计算器类
class Calculator extends Frame {

    //属性
    TextField num1;
    TextField num2;
    TextField num3;

    //方法
    public void loadFrame(){
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(10);

        //一个按钮
        Button button = new Button("=");
        //为按钮添加监听事件
        button.addActionListener(new MyCalculatorListener(this));

        //一个标签
        Label label = new Label("+");

        //流式布局,FlowLayout
        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,获得加数和被加数
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());

        //2,加法运算后将得数放到第三个框中
        calculator.num3.setText(""+(n1+n2));

        //3,清除前两个框中的内容
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}
内部类
package com.ym.lesson02;

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

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

//计算器类
class Calculator extends Frame {

    //属性
    TextField num1;
    TextField num2;
    TextField num3;

    //方法
    public void loadFrame(){
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        //一个按钮
        Button button = new Button("=");
        //为按钮添加监听事件
        button.addActionListener(new MyCalculatorListener());

        //一个标签
        Label label = new Label("+");

        //流式布局,FlowLayout
        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,获得加数和被加数
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            //2,加法运算后将得数放到第三个框中
            num3.setText(""+(n1+n2));

            //3,清除前两个框中的内容
            num1.setText("");
            num2.setText("");
        }
    }
}

2.7画笔paint

package com.ym.lesson03;

import java.awt.*;

public class PaintTest {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{

    public void loadFrame(){

        setVisible(true);
        setBounds(100,100,400,400);
    }

    //画笔
    @Override
    public void paint(Graphics g) {
//        super.paint(g);

        //画笔颜色
//        g.setColor(Color.green);

        //画圆
        g.drawOval(100,100,100,100);
        g.fillOval(100,100,100,100);//实心圆

//        g.setColor(Color.red);
        g.fillRect(200,200,100,100);//实心圆

        //画笔用完之后要还原到画笔默认的颜色


    }
}

2.8鼠标监听

2.9窗口监听

package com.ym.lesson03;

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

public class WindowListenerTest {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.blue);
        setVisible(true);
        setBounds(100,100,200,200);

//        addWindowListener(new WindowListener());
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
            }

            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame wf=(WindowFrame) e.getSource();
                wf.setTitle("windowActivated");
                System.out.println("windowActivated");
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                System.out.println("windowDeactivated");
            }

        });
    }
    class WindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);//程序退出
        }
    }
}

2.10键盘监听

package com.ym.lesson03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyListenerTest {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(100,100,200,200);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }

            }
        });
    }
}

3, Swing

3.1窗口,面板

package com.ym.lesson04;

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

public class JFrameTest {
    public static void main(String[] args) {
        new JFrameTest().init();
    }

    //init()初始化
    public void init(){

        JFrame jFrame = new JFrame();
        jFrame.setVisible(true);
        jFrame.setBounds(10,10,200,200);
        jFrame.setBackground(Color.blue);
        //获得一个容器
        Container contentPane = jFrame.getContentPane();
        contentPane.setBackground(Color.red);

        JLabel jLabel = new JLabel("哈哈哈");
        //设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        jFrame.add(jLabel);

    }
}

3.2 弹窗JDialog

package com.ym.lesson04;

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

public class DialogTest extends JFrame {

    public DialogTest() {

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

        Container container = this.getContentPane();

        container.setLayout(null);//绝对布局

        JButton jButton = new JButton("点击弹出对话框");
        jButton.setBounds(30,30,100,100);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MyDialog myDialog = new MyDialog();
            }
        });
        container.add(jButton);
    }

    public static void main(String[] args) {
        DialogTest dialogTest = new DialogTest();
    }
}

class MyDialog extends JDialog{

    public MyDialog() {

        this.setVisible(true);
        this.setBounds(100,100,100,100);
//        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//默认有关闭事件

        Container container = this.getContentPane();

        container.setLayout(null);

        container.add(new JLabel("abc"));

    }
}

3.3 标签

图标和图片图标

package com.ym.lesson04;

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

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(20, 20);

        //把图标放在标签上
        JLabel jLabel = new JLabel("iconDemo",iconDemo,SwingConstants.CENTER);

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

        this.setVisible(true);
        this.setBounds(10,10,150,150);
        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 width;
    }

    @Override
    public int getIconHeight() {
        return height;
    }
}
package com.ym.lesson04;

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

public class ImageIconDemo extends JFrame {

    public  ImageIconDemo(){

        JLabel jLabel = new JLabel("ImageIcon");

        //获取图片地址
        URL url = ImageIconDemo.class.getResource("imageicondemo.png");

        ImageIcon imageIcon = new ImageIcon(url);//注意命名冲突问题

        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

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

        this.setVisible(true);
        this.setBounds(10,10,100,100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }


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

}

3.4 面板

JPanel
package com.ym.lesson05;

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

public class JPanelDemo extends JFrame {
    public JPanelDemo(){

        Container container = getContentPane();
        container.setLayout(new GridLayout(1,2,5,5));

        JPanel jPanel = new JPanel(new GridLayout(2,1));
        JPanel jPane2 = new JPanel(new GridLayout(1,2));

        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPane2.add(new JButton("2"));
        jPane2.add(new JButton("2"));

        container.add(jPanel);
        container.add(jPane2);

        setVisible(true);
        setBounds(10,10,100,100);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

JScrollPane
package com.ym.lesson05;

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

public class JScrollDemo extends JFrame {

    public JScrollDemo(){

        //文本域
        JTextArea jTextArea = new JTextArea(10,10);
        jTextArea.setText("你好呀");

        //scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);

        Container container = getContentPane();

        container.add(jScrollPane);


        this.setVisible(true);
        this.setBounds(10,10,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

3.5 按钮

Jbutton
package com.ym.lesson05;

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

public class JButtonDemo01 extends JFrame {

    public JButtonDemo01() {

        //将图片变为图片图标
        URL url = JButtonDemo01.class.getResource("btnicon.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //把图标放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("图片图标按钮");

        Container container = this.getContentPane();
        container.add(jButton);

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

    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}
单选按钮
package com.ym.lesson05;

import javafx.scene.control.RadioButton;

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

public class JButtonDemo02 extends JFrame {

    public JButtonDemo02() {
        Container container = this.getContentPane();
        //将图片变为图片图标
        URL url = JButtonDemo02.class.getResource("btnicon.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //把图标放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton();

        //把这三个按钮分到一个组里
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        container.add(jRadioButton1,BorderLayout.NORTH);
        container.add(jRadioButton2,BorderLayout.CENTER);
        container.add(jRadioButton3,BorderLayout.SOUTH);

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

    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
复选按钮
package com.ym.lesson05;

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

public class JButtonDemo03 extends JFrame {

    public JButtonDemo03() {
        Container container = this.getContentPane();
        //将图片变为图片图标
        URL url = JButtonDemo03.class.getResource("btnicon.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //把图标放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);

        //复选框
        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");

        container.add(jCheckBox1,BorderLayout.NORTH);
        container.add(jCheckBox2,BorderLayout.SOUTH);
//        this.setLayout(new FlowLayout(FlowLayout.LEFT));
//        this.setLayout(new GridLayout(2,1));

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

    }

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

3.6 列表

下拉列表
package com.ym.lesson06;

import javafx.scene.control.ComboBox;

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

public class ComboboxDemo extends JFrame {
    public ComboboxDemo() {

        Container container = this.getContentPane();

        JComboBox status = new JComboBox();
        status.addItem("就业");
        status.addItem("考研");
        status.addItem("考公");

        container.add(status);

        this.setVisible(true);
        this.setBounds(10,10,100,100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

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

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

public class ComboboxDemo2 extends JFrame {
    public ComboboxDemo2() {

        Container container = this.getContentPane();

        //生成列表的内容
        String[] contents1={"ha","haha","哈哈哈"};
        
        JList jList1 = new JList(contents1);
        container.add(jList1,BorderLayout.NORTH);
        
        Vector contents2 = new Vector();
        contents2.add("la");
        contents2.add("lala");
        contents2.add("啦啦啦");
        
        JList jList2 = new JList(contents2);
        container.add(jList2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(10,10,100,100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

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

3.7 文本框

文本框,文本域和密码框
package com.ym.lesson06;

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

public class TextDemo extends JFrame {
    public TextDemo() {

        Container container = this.getContentPane();

        //文本框JTextField
        JTextField jTextField = new JTextField("haha",8);

        //文本域JTextArea
        JTextArea jTextArea = new JTextArea("hahahahahha",8,8);

        //密码框JPasswordField
        JPasswordField jPasswordField = new JPasswordField("12345678",8);

        JPanel jPanel = new JPanel();
        jPanel.add(jTextField);
        jPanel.add(jTextArea);
        jPanel.add(jPasswordField);

        JScrollPane jScrollPane = new JScrollPane(jPanel);

        container.add(jScrollPane);

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

    public static void main(String[] args) {
        new TextDemo();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GUI基础知识 的相关文章

随机推荐

  • Django-42-ORM多对多查询(ManyToManyField自动创建)

    前提 初始表数据 手动创建与自动创建的区别主要在于自动创建的表没有第三张可操作的表 其他没区别 此篇仅以自动创建为例说明 book表 author表 book authors表 关系表 django study app01 views py
  • 渗压计特性特点计算方法应用

    渗压计是一种测量渗流水或静力压力的传感器 适用于回填或原位孔隙水压力的测定 扬压力的测定 水位或容器中流体压力的测定 具有抗干扰能力强 长期稳定 密封可靠等特点 广泛应用于建筑 铁路 交通 水电大 坝 隧道等土木工程领域 特性 结构简单 紧
  • 软件工程:(四)概要设计

    一 定义 概要设计是一个设计师根据用户交互过程和用户需求来形成交互框架和视觉框架的过程 其结果往往以反映交互控件布置 界面元素分组以及界面整体板式的页面框架图的形式来呈现 这是一个在用户研究和设计之间架起桥梁 使用户研究和设计无缝结合 将对
  • KNN实现手写数字识别

    其他实现手写数字识别的方法 1 聚类 K means 实现手写数字识别 2 卷积神经网络 CNN 实现手写数字识别 3 全连接神经网络实现手写数字识别 4 聚类 K means 实现手写数字识别 2 实验数据是老师收集了所有人的手写数字图片
  • jeesite快速开发平台(一)----简介

    以下内容来自官网 一 平台简介 JeeSite是基于多个优秀的开源项目 高度整合封装而成的高效 高性能 强安全性的开源Java EE快速开发平台 JeeSite是您快速完成项目的最佳基础平台解决方案 JeeSite是您想学习Java平台的最
  • 寄存器的基本原理

    参考大神博客 https blog csdn net qq 37340753 article details 80935423 https blog csdn net u012493828 article details 53439226
  • kali工具的使用

    一 netcat简介与使用 nc的全称为NetCat 它能够建立并接受传输控制协议 TCP 和用户数据报协议 UDP 的连接 Netcat可在这些连接上读写数据 直到连接关闭为止 它可以通过手工或者脚本与应用层的网络应用程序或服务进行交互
  • openblas 第二弹: openblas Android版调用和编译

    1 编译 如果需要在Android下使用openblas 则需要编译Android版本的openblas a文件进行调用 1 openblas的编译时主要参考链接 参考链接一 参考链接二 具体细节太久了 已经忘了 下面是编译好的时候的环境变
  • Linux中普通用户和ROOT用户对Java JDK的配置

    Linux中对对各种工具文件不需要想Windows中似的 还要先一步一步的安装 有的还需要配置环境变量 比如Windows对Java的安装过程 在Linux中 使用指令 tar zxvf 文件名 注意空格 解压完 tar gz 文件 或使用
  • Spring事务实现原理

    Spring事务的原理是基于AOP实现的 所以流程也可以理解为与AOP一样分为3步 解析切面 织入通知和运行时增强 1 解析切面 Srping事务的是通过 EnableTransactionManagement注解开启的 该注解往IoC容器
  • 【逆向】使用CE查找Android中变量的偏移

    0x00 准备工作下载Cheat Engine以及调试器服务端 https www cheatengine org index php 夜神模拟器 https www yeshen com 下载安装贪婪洞窟 梦境模式 http a 4399
  • 【华为OD机试】路灯照明问题 (C++ Python Java)2023 B卷

    时间限制 C C 1秒 其他语言 2秒 空间限制 C C 262144K 其他语言524288K 64bit IO Format lld 题目描述 在一条笔直的公路上安装了N个路灯 从位置0开始安装 路灯之间间距固定为100米 每个路灯都有
  • oracle修改块大小设置,oracle性能调整(1)

    1调整数据库服务器的性能Oracle数据库服务器是整个系统的核心 它的性能高低直接影响整个系统的性能 为了调整Oracle数据库服务器的性能 主要从以下几个方面考虑 1 1Oracle 调整操作系统以适合Oracle数据库服务器运行数据库服
  • 利用PicGo+Gitee配置图床

    引言 配置图床 方便我们的使用 比如 我们利用typora写的笔记 直接把发送给别人也可以正常使用 不再会有由于本地图片 而加载不出来图片的情况 此外 图片文件遗失亦可以正常加载出来 因为图片已上传 这里已 Typora Gitee Pic
  • java String(一)—— Java中的String类型

    一 需要理解的代码 import java lang reflect Array import java util ArrayList import java util Arrays import java util HashMap imp
  • DNS服务器正向/反向解析配置

    第四次作业 题目 配置DNS正反向解析 一 正向解析 1 装包 2 配置服务 3 配置服务器 4 测试 1 yum install bind y 2 vim etc named conf 监听53号端口 访问的是本机ip 129 168 2
  • c++命名空间

    命名空间 主要解决全局变量的冲突 内部不允许私有变量 所有变量都是公有的 namespace data int x 10 data x 为域作用符 直接使用等同于使用全局变量 不存在就是0 不包含匿名命名空间内变量 同一个文件引用stati
  • 相见恨晚的办公插件合集(二)

    之前有分享过一些办公的插件 如不坑盒子 打工人插件 易用宝等 下面就简单的介绍一下上面的几个神器后再补充一些其它办公神器吧 不坑盒子 word wps 这是一个非常好用的插件工具 专门应用在Word文档和wps 支持Office 2010以
  • 拓数派入选中国信通院 “铸基计划”「高质量数字化转型产品及服务全景图」

    7 月 27 日 由中国信息通信研究院 以下简称 中国信通院 主办的 2023 数字生态发展大会 暨中国信通院 铸基计划 年中会议在京召开 本次大会深度展示了中国信通院在数字化领域的工作成果 并正式发布了 高质量数字化转型产品及服务全景图
  • GUI基础知识

    GUI编程 1 简介 图形用户界面 Graphical User Interface 又称图形用户接口 是指采用图形方式显示的计算机操作用户界面 GUI的核心技术 AWT Swing 2 Awt 2 1 AWT简介 AWT Abstract