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();
    }
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐