GUI编程

GUI编程

1、简介

Gui的核心技术: Swing AWT

1.界面不美观

2.需要jre环境

为什么要学习?

  1. 可以写出自己心中想要的一些小工具
  2. 工作时候,也可能维护到swing界面,但概率极小。
  3. 了解MVC架构,了解监听!

AWT(Swing前身,讲底层的实现)

2.1 AWT介绍

  1. 包含了很多类和接口!GUI:图像用户界面。 Eeclipse:java环境写的
  2. 元素:窗口、按钮、文本框
  3. java.awt包里

2.2组件和容器

窗口
public class TestFrame01 {
    public static void main(String[] args) {
        //Frame,源码,帮助文档
        Frame frame = new Frame("喻言");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(526,526);
        //设置颜色
        frame.setBackground(new Color(158, 220, 97));
        //弹出的初始位置
        frame.setLocation(400,400);
        //设置大小是否固定
        frame.setResizable(true);
    }
}

多个窗口

import java.awt.*;
public class TestFrame02 {
    //想要展示多个窗口 new
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 100, 100,Color.cyan);
        MyFrame myFrame2 = new MyFrame(300, 100, 100, 100,Color.green);
        MyFrame myFrame3 = new MyFrame(100, 300, 100, 100,Color.red);
        MyFrame myFrame4 = new MyFrame(300, 300, 100, 100,Color.yellow);
    }
    static class MyFrame extends Frame{
        //可能存在多个窗口,我需要一个计数器来进行计数
        static int id = 0;
        public MyFrame(int x,int y,int w,int h,Color color){
            super("喻言+"+(++id));  //id自增
            setVisible(true);
            setBounds(x,y,w,h);
            setBackground(color);
        }
    }
}
面板
import java.awt.*;
//panel 可以看做一个空间,但不能单独存在
public class TestPanel01 {
    public static void main(String[] args) {
        //布局的概念
        Panel panel = new Panel();
        Frame frame = new Frame();
        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(225, 229, 172));
        //panel设置坐标,相对于Frame
        panel.setBounds(50,50,200,200);
        panel.setBackground(new Color(175, 243, 57));
        //panel依托于Frame
        frame.add(panel);
        //设置可见
        frame.setVisible(true);
    }
}

关闭窗口

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

2.3布局管理器

流式布局

frame.setLayout(new FlowLayout(FlowLayout.LEFT))

import java.awt.*;
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //设置按钮
        Button button = new Button("yuyan1");
        Button button1 = new Button("yuyan2");
        Button button2 = new Button("yuyan3");
        //设置为流式布局
        frame.setLayout(new FlowLayout());    //默认居中
       // frame.setLayout(new FlowLayout(FlowLayout.LEFT));  //左
        //添加按钮
        frame.add(button);
        frame.add(button1);
        frame.add(button2);
        //显示
        frame.setVisible(true);
    }
}
东西南北中

BorderLayout.EAST

import java.awt.*;
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("喻言");
        Button button1 = new Button("East");
        Button button2 = new Button("west");
        Button button3 = new Button("south");
        Button button4 = new Button("north");
        Button button5 = new Button("center");
        //添加东西南北中效果
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setSize(526,526);
        frame.setVisible(true);
    }
}
表格布局

GridLayout(3,2)(几行几列)

import java.awt.*;
public class TestGridLayout {
    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");
        Button button4 = new Button("4");
        Button button5 = new Button("5");
        Button button6 = new Button("6");
        //设置行列
        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        //Java的函数,自动布局
        frame.pack();
        frame.setVisible(true);
    }
}
package GUI;

import java.awt.*;

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


        frame.setSize(400,300);
        frame.setLocation(300,400);//弹窗的初始位置
        //frame.setBackground(Color.WHITE);

        frame.setLayout(new GridLayout(2,1));
        frame.setVisible(true);
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));
        panel1.add(new Button("East-1"),BorderLayout.EAST);
        panel1.add(new Button("West-1"),BorderLayout.WEST);

        panel2.add(new Button("P2-1"));
        panel2.add(new Button("P2-2"));

        panel1.add(panel2,BorderLayout.CENTER);

        panel3.add(new Button("East-2"),BorderLayout.EAST);
        panel3.add(new Button("West-2"),BorderLayout.WEST);

        for (int i = 0; i < 4; i++) {
            panel4.add(new Button("P4-"+i));
        }
        panel3.add(panel4,BorderLayout.CENTER);

        frame.add(panel1);
        frame.add(panel3);


    }
}

2.4事件监听

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 TsetActionEvent {
    public static void main(String[] args) {
        //按下按钮发生一些事件
        Frame frame = new Frame();
        Button button = new Button("please!");
        //
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button);
        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("喻言");
    }
}
//两个按钮,实现同一个监听
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("stop1");
        mymoniter mymoniter = new mymoniter();
        button1.addActionListener(mymoniter);
        button2.addActionListener(mymoniter);
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.pack();
        frame.setVisible(true);
    }
}
class mymoniter implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取按钮上的信息
        System.out.println(e.getActionCommand());
    }
}

2.5输入框

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Testfield {
    public static void main(String[] args) {
        //启动
        MyFrame myFrame = new MyFrame();
        myFrame.setVisible(true);
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        ActionListener1 actionListener1 = new ActionListener1();
        //监听文本框输入的文字
        textField.addActionListener(actionListener1);
        //设置替换编码
        textField.setEchoChar('*');
    }
}
class ActionListener1 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField fisld =(TextField) e.getSource(); //获得一些资源,返回一个对象
        System.out.println(fisld.getText());        //enter 即可获取
        fisld.setText("");
    }
}

2.6计算器

oop原则:组合,大于继承

class A extend B{
    
}//继承

class A{
    public B b;
}//组合
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 TestCalculator {
    public static void main(String[] args) {
        //运行
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame{
    public Calculator(){
        //三个文本框
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(textField1, textField2, textField3));
        //一个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//监听类
class MyCalculatorListener implements ActionListener{
    private TextField textField1;
    private TextField textField2;
    private TextField textField3;
    public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3) {
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(textField1.getText());
        int n2 = Integer.parseInt(textField2.getText());
        //2.将
        textField3.setText(""+(n1+n2));
        //3.清除前两个框
        textField1.setText("");
        textField2.setText("");
    }
}
改造版(传参变成传对象)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator1{
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator1 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 MyCalculatorListener1(this));
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
 class MyCalculatorListener1 implements ActionListener {
    //获取计算器这个对象,在一个类中组合另一个类
    Calculator1 calculator = null;
    public MyCalculatorListener1(Calculator1 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("");
    }
}
终极版
  • 内部类
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator1{
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator1 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 MyCalculatorListener1());
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    //监听器类---》内部类
    //内部类好处,畅通无阻的访问外部类
  private class MyCalculatorListener1 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("");
        }
    }
}

2.8鼠标监听

实现鼠标画画

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听
public class TestMouse {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
//鼠标类
class MyFrame extends Frame{
    //画画需要画笔,需要监听鼠标当前的位置
    // 需要集合来存储这个点
    ArrayList points;
    public MyFrame(String title)  {
        super(title);
        setBounds(200,200,400,400);
        //存鼠标的点
        points = new ArrayList<>();
        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouse());
        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,5,5);//画点时显示的其实是实心圆
        }
    }
    //添加一个点到界面上,写一个方法
    public void addPoint(Point point){
        points.add(point);
    }
    //适配器模式
    private class MyMouse extends MouseAdapter{
        //鼠标   按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //当我们点击的时候就会在界面上产生一个点
            //这个点就是鼠标的点
            myFrame.addPoint(new Point(e.getX(),e.getY()));
            //每一次点击鼠标都需要重新画一遍
            myFrame.repaint();//刷新
        }
    }
}

2.9窗口监听

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(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
        addWindowListener(new MyWindowListener());
    }
    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);//正常退出
        }
    }
}
匿名内部类
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(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );
    }
}
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(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowOpened(WindowEvent e) {
                        System.out.println("windowOpened");
                    }
                    //关闭窗口
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                    }
                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println("windowClosed");
                    }
                    //最小化
                    @Override
                    public void windowIconified(WindowEvent e) {
                        System.out.println("windowIconified");
                    }
                    @Override
                    public void windowDeiconified(WindowEvent e) {
                        System.out.println("windowDeiconified");
                    }
                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        System.out.println("windowActivated");
                    }
                }
        );
    }
}

2.10键盘监听

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键盘监听
public class TestKey {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        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窗口

import javax.swing.*;
//Swing-----JFrame
public class TestJFrame {
    public static void main(String[] args) {
        new JFrame().init();
    }
}
class JFrame{
    public void init(){
        javax.swing.JFrame jFrame = new javax.swing.JFrame("Jframe窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,200,200);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

文本居中

import javax.swing.*;
import java.awt.*;
//Swing-----JFrame
public class TestJFrame {
    public static void main(String[] args) {
        new MyJFrame().init();
    }
}
class MyJFrame extends JFrame{
    public void init(){
        //顶级窗口
        JFrame jFrame = new JFrame("Jframe窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,200,200);
        //设置文字Jlabel
        JLabel Jlabel = new JLabel("喻言");
        jFrame.add(Jlabel);
        //让文本居中
        Jlabel.setHorizontalAlignment(SwingConstants.CENTER);
        //容器实例化
        Container container = this.getContentPane();
        container.setBackground(Color.blue);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.2弹窗

默认有关闭事件

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
//弹窗
public class TestDialog {
    public static void main(String[] args) {
        new Dialog1();
    }
}
//主窗口
class Dialog1 extends JFrame{
    public Dialog1(){
        this.setVisible(true);
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Jframe放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton jButton = new JButton("弹出对话框");//创建
        jButton.setBounds(30,30,200,50);
        container.add(jButton);
        //点击这个按钮的时候,弹出一个弹窗(监听事件)
        jButton.addActionListener(new AbstractAction() {  //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new Dialog2();
            }
        });
        container.add(jButton);
    }
}
//弹窗的窗口
class Dialog2 extends JDialog{
    public Dialog2(){
        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("喻言我老公"));
    }
}

3.3标签

  • Label
  • g.fillOval(x,y,width,high);画的小图标
import javax.swing.*;
import java.awt.*;
//图标
public class TestIcon {
    public static void main(String[] args) {
        new Icon1(100,100).init();
    }
}
class Icon1 extends JFrame implements Icon{
    //属性
    private int width;
    private int high;
    public Icon1(){}//无参构造
    public Icon1(int width,int high){
        this.width = width;
        this.high = high;
    }
    //方法
    public void init(){
        setBounds(100,100,300,300);
        Icon1 icon1 = new Icon1(15,15);
        //图标放在标签上,也可以放在按钮上
        JLabel jLabel = new JLabel("喻言",icon1,SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,high);
    }
    @Override
    public int getIconWidth() {
        return this.width;
    }
    @Override
    public int getIconHeight() {
        return this.high;
    }
}
  • 标签上加图片
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
    public static void main(String[] args) {
        new TestImageIcon();
    }
    public TestImageIcon()  {
        //获取图片地址     url是一个具体的地址
        JLabel jLabel = new JLabel("ImageIcon");
        URL url = TestImageIcon.class.getResource("1.jpg");//获取当前类下的资源
        ImageIcon imageIcon = new ImageIcon(url);//命名避免冲突
        jLabel.setIcon(imageIcon);//图片饭在jLabel上
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.4面板

import javax.swing.*;
import java.awt.*;
public class TestJPane extends JFrame {
    public TestJPane()  {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));  //间距
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,3));
        JPanel jPanel3 = new JPanel(new GridLayout(1,3));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("2"));
        jPanel.add(new JButton("3"));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("3"));
        jPanel2.add(new JButton("1"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("3"));
        jPanel3.add(new JButton("1"));
        jPanel3.add(new JButton("2"));
        jPanel3.add(new JButton("3"));
        container.add(jPanel);
        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        setVisible(true);
        setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJPane();
    }
}

JScrollPonel(滚动)

import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
    public static void main(String[] args) {
        new TestImageIcon();
    }
    public TestImageIcon()  {
        //获取图片地址     url是一个具体的地址
        JLabel jLabel = new JLabel("ImageIcon");
        URL url = TestImageIcon.class.getResource("1.jpg");//获取当前类下的资源
        ImageIcon imageIcon = new ImageIcon(url);//命名避免冲突
        jLabel.setIcon(imageIcon);//图片饭在jLabel上
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.5按钮

  • 单选
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//多选
public class TestJButton1 extends JFrame {
    public TestJButton1() {
        Container container = this.getContentPane();
        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("1");
        JRadioButton jRadioButton2 = new JRadioButton("2");
        JRadioButton jRadioButton3 = new JRadioButton("3");
        //由于单选框只能选一个所以我们要分组,一个组中只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        group.add(jRadioButton3);
        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJButton1();
    }
}
  • 多选
import javax.swing.*;
import java.awt.*;
//多选框
public class TestJButton2 extends JFrame {
    public TestJButton2() {
        Container container = this.getContentPane();
        JCheckBox jCheckBox = new JCheckBox("1");
        JCheckBox jCheckBox1 = new JCheckBox("2");
        container.add(jCheckBox,BorderLayout.SOUTH);
        container.add(jCheckBox1,BorderLayout.NORTH);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJButton2();
    }
}

3.6 列表

  • 下拉框(应用:选择地区,或一些单个选项)
import javax.swing.*;
import java.awt.*;
//下拉框
public class TestJCombobox extends JFrame {
    public TestJCombobox() {
        Container container = this.getContentPane();
        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem(null);
        jComboBox.addItem("正在上映");
        jComboBox.addItem("已下架");
        jComboBox.addItem("即将上映");
        container.add(jComboBox);
        this.setBounds(100,100,300,300);
        this.setBackground(Color.CYAN);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJCombobox();
    }
}

应用:展示信息,一般是动态扩容。

Vector contents = new Vector();             //动态
//列表需要放入内容
JList jList = new JList(contents);
contents.add("喻言");
contents.add("李瑾");
contents.add("宝宝");
container.add(jList);

3.7文本框

  • 文本框
import javax.swing.*;
import java.awt.*;
//文本框
public class TestText1 extends JFrame {
    public TestText1() {
        Container container = getContentPane();
        JTextField jTextField = new JTextField("yuyan");
        JTextField jTextField1 = new JTextField("lijin",20);
        //布局
        container.add(jTextField,BorderLayout.NORTH);
        container.add(jTextField1,BorderLayout.SOUTH);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestText1();
    }
}
  • 密码框
import javax.swing.*;
import java.awt.*;
//密码框
public class TestText2 extends JFrame {
    public TestText2() {
        Container container = getContentPane();
        JPasswordField jPasswordField = new JPasswordField();   //有默认值,但也可以设置
        //jPasswordField.setEchoChar('+');
        container.add(jPasswordField);
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestText2();
    }
}
  • 文本域
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("喻言");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值