栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

GUI编程笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GUI编程笔记

GUI编程

GUI:图形用户界面编程

GUI编程学习路线

  • GUI是什么
  • GUI怎么写
  • GUI使用场景

组件

  • 监听
  • 弹窗
  • 面板
  • 鼠标
  • 键盘
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 外挂
  • 破解工具
一、简介

GUI核心技术:Swing AWT

缺点:

  1. 不美观
  2. 需要jre环境

为什么要学习

  1. 可以写出一些自己用的小工具
  2. 可能会涉及到swing的维护工作 -> 破解
  3. 了解MVC架构,了解监听
二、AWT 2.1、AWT介绍
  1. AWT:抽象的窗口工具,包含了很多的类和接口
  2. 元素:窗口、按钮、文本框
  3. java.awt包下

2.2.组件和容器 1.frame
import java.awt.*;

//GUI的第一个界面
public class Testframe {
    public static void main(String[] args) {
        frame frame = new frame("frame标题");
//        设置窗口可见
        frame.setVisible(true);
//        设置大小
        frame.setSize(400,400);
//        设置背景颜色
        frame.setBackground(Color.BLACK);
//        设置初始位置
        frame.setLocation(400,400);
//        设置大小固定
        frame.setResizable(false);
    }
}

问题:关闭按钮无反应,关闭程序进程才会关闭窗口

封装打开多个实现:

package com.pmpwpl.gui01;

import java.awt.*;


public class Testframe02 {

    public static void main(String[] args) {
        Myframe frame1 = new Myframe(100, 100, 200, 200, Color.blue);
        Myframe frame2 = new Myframe(300, 100, 200, 200, Color.red);
        Myframe frame3 = new Myframe(100, 300, 200, 200, Color.cyan);
        Myframe frame4 = new Myframe(300, 300, 200, 200, Color.MAGENTA);
    }
}

class Myframe extends frame{

    public Myframe(int x,int y,int w,int h,Color color){
        setVisible(true);

        setTitle("Myframe");

        setBounds(x,y,w,h);

        setBackground(color);

        setResizable(false);
    }
    
}

2.Panel
public class TestPanel {
        public static void main(String[] args) {
            frame frame = new frame("带面板的frame");
            Panel panel = new Panel();

            frame.setLayout(null);

            frame.setBounds(400,200,500,500);
            frame.setBackground(Color.BLACK);

            panel.setBounds(100,100,300,300);
            panel.setBackground(Color.YELLOW);

//        将面板放入frame中
            frame.add(panel);
            frame.setVisible(true);

//        添加监听
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    //关闭frame
                    System.exit(0);
                }
            });
        }
}

2.3.布局管理器
  • 流式布局 FlowLayout
public class TestFlowLayout {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setVisible(true);
        frame.setBounds(400,200,500,500);
        Button button1 = new Button("按钮1");
        Button button2 = new Button("按钮2");
        Button button3 = new Button("按钮3");

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        
        //设置布局
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        //        添加监听
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭frame
                System.exit(0);
            }
        });
    }
}

  • 东西南北中 BorderLayout
public class TestBorderLayout {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setVisible(true);
        frame.setBounds(400,400,500,500);
        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.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭frame
                System.exit(0);
            }
        });
    }
}

  • 表格式布局 GridLayout
public class TestGridLayout {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setVisible(true);
       frame.setBounds(400,400,500,500);

        Button button1 = new Button("but1");
        Button button2 = new Button("but2");
        Button button3 = new Button("but3");
        Button button4 = new Button("but4");
        Button button5 = new Button("but5");
        Button button6 = new Button("but6");
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.setLayout(new GridLayout(3,2));
        //自动布局大小,测试过程中发现不能在第一行写这个,需要在添加完毕后增加会分配默认size
        frame.pack();
        //        添加监听
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭frame
                System.exit(0);
            }
        });
    }
}

  • 嵌套布局:
public class TestDemo1 {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setVisible(true);
        frame.setBounds(100,100,800,800);
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        Button button7 = new Button("button7");
        Button button8 = new Button("button8");
        Button button9 = new Button("button9");
        Button button10 = new Button("button10");
        frame.setLayout(null);
        
        Panel panel1 = new Panel(new GridLayout(1,1));
        panel1.setBounds(0,0,200,400);
        panel1.add(button1);
        Panel panel3 = new Panel(new GridLayout(2,1));
        panel3.setBounds(200,0,400,400);
        panel3.add(button2);
        panel3.add(button3);
        Panel panel4 = new Panel(new GridLayout(1,1));
        panel4.setBounds(600,0,200,400);
        panel4.add(button4);

        Panel panel2 = new Panel(new GridLayout(1,1));
        panel2.setBounds(0,400,200,400);
        panel2.add(button5);
        Panel panel5 = new Panel(new GridLayout(2,2));
        panel5.setBounds(200,400,400,400);
        panel5.add(button6);
        panel5.add(button7);
        panel5.add(button8);
        panel5.add(button9);
        Panel panel6 = new Panel(new GridLayout(1,1));
        panel6.setBounds(600,400,200,400);
        panel6.add(button10);

        frame.add(panel1);
        frame.add(panel3);
        frame.add(panel4);
        frame.add(panel2);
        frame.add(panel5);
        frame.add(panel6);

        //        添加监听
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭frame
                System.exit(0);
            }
        });
    }
}

2.4、监听事件
  1. 一个按钮可以添加多个监听

    public class TestActionEvent1 {
        public static void main(String[] args) {
            frame frame = new frame();
            frame.setVisible(true);
            frame.setBounds(100,100,100,100);
    
    //        一个按钮可以同时添加多个监听
            Button button = new Button();
    
            MyActionListener1 l1 = new MyActionListener1();
            MyActionListener2 l2 = new MyActionListener2();
    
            button.addActionListener(l1);
            button.addActionListener(l2);
    
            frame.add(button);
    
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    class MyActionListener1 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("第一个监听事件");
        }
    }
    class MyActionListener2 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("第二个监听事件");
        }
    }
    
  2. 多个按钮监听一个事件

    public class TestActionEvent2 {
        public static void main(String[] args) {
            frame frame = new frame();
            frame.setVisible(true);
            frame.setBounds(100,100,100,100);
    
    //        一个按钮可以同时添加多个监听
            Button button1 = new Button("button1");
            Button button2= new Button("button2");
            Button button3 = new Button("button3");
            Button button4 = new Button("button4");
    
            frame.setLayout(new GridLayout(2,2,5,5));
            MyActionListener myActionListener = new MyActionListener();
            button1.addActionListener(myActionListener);
            button2.addActionListener(myActionListener);
            button3.addActionListener(myActionListener);
            button4.addActionListener(myActionListener);
            
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            frame.add(button4);
    
            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( e.getActionCommand() + ":监听事件");
        }
    }
    

【小结】

  1. frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到容器
  3. 布局管理
    • 流式布局
    • 东西南北中布局【边界布局】
    • 表格布局
  4. 大小、定位、可见性、背景颜色、监听
2.5、输入框监听
public class TestActionEvent3 {
    public static void main(String[] args) {
        //启动
        new Myframe();
    }
}
class Myframe extends frame{
    public Myframe(){
        setTitle("测试文本框");

        TextField textField = new TextField();

        MA ma = new MA();
        textField.addActionListener(ma);
        //将输入框中的字符替换成#
        textField.setEchoChar('#');


        add(textField);

        setBounds(400,200,200,200);

        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MA implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //System.out.println(e.getActionCommand());这个语句也能直接获取文本框的内容
        //获取文本框
        TextField tf = (TextField) e.getSource();
        //获取文本框的内容
        System.out.println(tf.getText());
        //按下回车后,文本框的内容清空
        tf.setText("");
    }
}
2.6、简易计算器

原始代码

public class TestActionEvent4 {

    public static void main(String[] args) {

        new MF();

    }
}
class MF extends frame{
    public MF(){
        setTitle("简易计算器");

        //需要三个文本框
        TextField tf1 = new TextField(10);
        TextField tf2 = new TextField(10);
        TextField tf3 = new TextField(20);
        //需要一个等号按钮
        Button button = new Button("=");
        MA1 ma1 = new MA1(tf1,tf2,tf3);
        button.addActionListener(ma1);
        //需要一个加号标签
        Label label = new Label("+");

        setLayout(new FlowLayout());
        add(tf1);
        add(label);
        add(tf2);
        add(button);
        add(tf3);

        setVisible(true);
        setLocation(400,200);
        pack();
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MA1 implements ActionListener{

    TextField tf1,tf2,tf3;

    public MA1(TextField tf1,TextField tf2,TextField tf3){
     this.tf1 = tf1;
     this.tf2 = tf2;
     this.tf3 = tf3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(tf1.getText());
        int n2 = Integer.parseInt(tf2.getText());
        tf3.setText("" + (n1+n2));
        tf1.setText("");
        tf2.setText("");
    }
}
  • 终极优化

    public class TestActionEvent4 {
    
        public static void main(String[] args) {
    
            new MF().Myframe();
    
        }
    }
    class MF extends frame{
    
        TextField tf1,tf2,tf3;
    
        public void Myframe(){
            setTitle("简易计算器");
    
            //需要三个文本框
            tf1 = new TextField(10);
            tf2 = new TextField(10);
            tf3 = new TextField(20);
            //需要一个等号按钮
            Button button = new Button("=");
            MA1 ma1 = new MA1();
            button.addActionListener(ma1);
            //需要一个加号标签
            Label label = new Label("+");
    
            setLayout(new FlowLayout());
            add(tf1);
            add(label);
            add(tf2);
            add(button);
            add(tf3);
    
            setVisible(true);
            setLocation(400,200);
            pack();
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    
        class MA1 implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                int n1 = Integer.parseInt(tf1.getText());
                int n2 = Integer.parseInt(tf2.getText());
                tf3.setText("" + (n1+n2));
                tf1.setText("");
                tf2.setText("");
            }
        }
    }
    
2.7、画笔
public class TestPaint {
    public static void main(String[] args) {

        new MF1().loadframe();
    }
}
class MF1 extends frame{
    public void loadframe(){
        setTitle("测试画笔");

        setBounds(400,200,500,500);

        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.cyan);
        g.fillOval(100,100,50,50);
    }
}
2.8、鼠标监听

目的:实现鼠标画画

public class TestMouse {
    public static void main(String[] args) {
        new Paint().loadMyPaint();
    }
}

class Paint extends frame {
    private ArrayList pointList;

    public void loadMyPaint(){
        pointList = new ArrayList<>();

        setTitle("画图");
        setVisible(true);
        setBounds(100,100,800,600);

        addMouseListener(new MyMouseListenter());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        Iterator iterator = pointList.iterator();
        while (iterator.hasNext()){
            Point point = iterator.next();
            //循环画列表的点坐标
            g.fillOval(point.x,point.y,5,5);
        }
    }
    private class MyMouseListenter extends MouseAdapter {
        // 按下鼠标时触发
        @Override
        public void mousePressed(MouseEvent e) {
            //将点添加到集合
            pointList.add(new Point(e.getX(),e.getY()));
            //画笔重新画点
            repaint();
        }
    }
}
2.9、窗口监听
public class TestWindow {

    public static void main(String[] args) {
        new MyWindow().loadMyPaint();
    }
}
    class MyWindow extends frame {
        public MyWindow(){
            super("默认窗口名称");
        }
        public void loadMyPaint(){
            setVisible(true);
            setBounds(100,100,800,600);
            addWindowListener(new WindowAdapter() {
                //            点击窗口关闭按钮
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("窗口被关闭");
                    System.exit(0);
                }

                @Override
                public void windowDeactivated(WindowEvent e) {
                    System.out.println("窗口失去焦点");
                }
                
                @Override
                public void windowIconified(WindowEvent e) {
                    System.out.println("窗口被缩小");
                }

                @Override
                public void windowDeiconified(WindowEvent e) {
                    System.out.println("窗口被还原");
                }

                @Override
                public void windowActivated(WindowEvent e) {
                    setTitle("被激活了窗口");
                }
                
            });

        }
    }
2.10、键盘监听
public class TestKeyListener {
    public static void main(String[] args) {
        new MyKeyframe().loadMyPaint();
    }
}

class MyKeyframe extends frame {
    public MyKeyframe(){
        super("默认窗口名称");
    }
    public void loadMyPaint(){
        setVisible(true);
        setBounds(100,100,800,600);

        addKeyListener(new KeyAdapter() {
            //            按下键时执行
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("按下了" + e.getKeyChar() + "键");

              
            }
        });

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
三、Swing

Swing是amt的封装演化,

3.1.窗口 Jframe
public class TestJframe1 {
    public static void main(String[] args) {
        Jframe jframe = new Jframe();
        jframe.setBounds(10,10,400,400);
        jframe.setVisible(true);

        JLabel jLabel = new JLabel("测试标签");
        //jframe.setBackground(Color.BLACK);这时设置背景颜色就不管用了
        Container contentPane = jframe.getContentPane();
        contentPane.setBackground(Color.YELLOW);//使用容器设置背景颜色
        //标签居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        jframe.add(jLabel);
//        不设置,默认是隐藏窗口
        jframe.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
    }
}
3.2弹窗 JDialog
public class TestJframe2 {
    public static void main(String[] args) {
        Jframe jframe = new Jframe();
        jframe.setBounds(10,10,400,400);
        jframe.setVisible(true);
        jframe.setResizable(false);

        JButton jButton = new JButton("测试按钮");
//        jframe.setBackground(Color.BLACK);
        Container contentPane = jframe.getContentPane();
        contentPane.setBackground(Color.YELLOW);
//        设置居中
//        jButton.setHorizontalAlignment(SwingConstants.CENTER);
        jButton.addActionListener(e ->
                new Mydiglog()
        );
        contentPane.setLayout(null);
        jButton.setLocation(200,200);
        jButton.setSize(50,30);
        contentPane.add(jButton);
//        不设置,默认是隐藏窗口
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
class Mydiglog extends JDialog{
    public Mydiglog(){
        setVisible(true);
        setBounds(10,10,500,500);
//        弹窗默认带了关闭,不需要写
//        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JLabel jLabel = new JLabel("测试弹窗标签2 ");
        Container container = getContentPane();
        container.setLayout(null);
        jLabel.setLocation(10,10);
        jLabel.setSize(100,20);
        container.add(jLabel);
    }
}
3.3 图标Icon
  • 图标

    public class Iconframe1 {
        public static void main(String[] args) {
            Jframe jframe = new Jframe();
            jframe.setBounds(10,10,400,400);
            jframe.setVisible(true);
    
            JLabel jButton = new JLabel(" 标签",new MyIconDemo(10,10),SwingConstants.CENTER);
    //        不设置,默认是隐藏窗口
            jframe.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
            jButton.setSize(100,100);
            jframe.add(jButton);
        }
    }
    
    class MyIconDemo implements Icon{
        private int iconWiddth;
        private int iconHeight;
    
        public MyIconDemo(int iconWiddth,int iconHeight){
            this.iconWiddth = iconWiddth;
            this.iconHeight = iconHeight;
        }
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.fillOval(x,y,iconWiddth,iconHeight);
        }
    
        @Override
        public int getIconWidth() {
            return iconWiddth;
        }
    
        @Override
        public int getIconHeight() {
            return iconHeight;
        }
    }
    
    • 图片
    public class IF extends Jframe {
        public static void main(String[] args) {
    
            new IF();
        }
    
        public IF(){
            JLabel jLabel = new JLabel("ImageIcon");
            URL url = IF.class.getResource("tb.png");
    
            ImageIcon imageIcon = new ImageIcon(url);
            jLabel.setIcon(imageIcon);
            jLabel.setHorizontalAlignment(SwingConstants.CENTER);
    
            Container contentPane = getContentPane();
            contentPane.add(jLabel);
    
            setBounds(400,200,300,300);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    }
    

其他创建和使用与awt类似,类名加J前缀,增加了一些快捷实现

3.4滚动条面板
public class TestPanle4 extends Jframe{
    public TestPanle4() {
        Container container = this.getContentPane();
        //文本域
        Jtextarea textarea = new Jtextarea(20, 50);
        textarea.setText("欢迎学习狂神说Java");
        //Scroll 面板
        JScrollPane scrollPane = new JScrollPane(textarea);
        container.add(scrollPane);

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

    public static void main(String[] args) {
        new TestPanle4();
    }
}
3.5、按钮
  • 图片按钮
public class JButtonDemo01 extends Jframe {
    public JButtonDemo01() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("tb.png");  //图片路径
        ImageIcon icon = new ImageIcon(resource);       //转换为图标

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

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

    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}
  • 单选按钮
public class JButtonDemo02 extends Jframe {
    public JButtonDemo02() {
        Container container = this.getContentPane();
        //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        //由于单选框只能选择一个,可以将他们分组,一个组只能选一个。
        ButtonGroup group = new ButtonGroup();      //组
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.setLayout(new FlowLayout());
        container.add(radioButton1);
        container.add(radioButton2);
        container.add(radioButton3);
        
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
  • 多选按钮
public class JButtonDemo03 extends Jframe {
    public JButtonDemo03() {
        Container container = this.getContentPane();
        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        JCheckBox checkBox03 = new JCheckBox("checkBox03");
        JCheckBox checkBox04 = new JCheckBox("checkBox04");

        container.setLayout(new FlowLayout());
        container.add(checkBox01);
        container.add(checkBox02);
        container.add(checkBox03);
        container.add(checkBox04);

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

    }
    public static void main(String[] args) {
        new JButtonDemo03();
    }
}
3.6、列表
  • 下拉框
public class TsetComboboxDemo01 extends Jframe {
    public TsetComboboxDemo01() {
        Container container = this.getContentPane();

        this.setTitle("下拉框");
        this.setSize(500,300);
        container.setLayout(null);
        JComboBox status = new JComboBox();

        status.setBounds(10,10,300,20);
        status.addItem("请选择:");
        status.addItem("正在上映");
        status.addItem("已下架");
        status.addItem("即将上映");

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

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TsetComboboxDemo01();
    }
}
  • 列表框
public class TsetComboboxDemo02 extends Jframe {
    public TsetComboboxDemo02() {
        Container container = this.getContentPane();
        //生产列表的内容
        //String[] contents = {"1","2","3"};    静态数组

        Vector contents = new Vector();

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

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

        container.add(jList);

        this.setVisible(true);
        this.setTitle("列表框");
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TsetComboboxDemo02();
    }
}
  • 应用场景
    • 下拉框:选择地址或者一些单个选项(一到两个最好使用按钮,两个以上使用下拉框,节省内存布局)
    • 列表框:展示信息,一般是动态扩容。
3.7、文本框
  • 文本框 TextField
public class TestTextDemo01 extends Jframe  {
    public TestTextDemo01() throws HeadlessException {
        Container container = this.getContentPane();


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

        container.setLayout(new FlowLayout());
        container.add(textField1);
        container.add(textField2);


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

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}
  • 密码框 PasswordField
public class TestTextDemo02 extends Jframe {
    public TestTextDemo02() throws HeadlessException {
        Container container = this.getContentPane();

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

        container.add(passwordField);


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

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
  • 文本域 textarea
public class JScrollDemo extends Jframe {
    public JScrollDemo() {
        Container container = this.getContentPane();
        //文本域
        Jtextarea textarea = new Jtextarea(100, 100);
        textarea.setText("欢迎学习狂神说Java");
        //Scroll 面板
        JScrollPane scrollPane = new JScrollPane(textarea);
        container.add(scrollPane);


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

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

【常用代码】

frame;											框架
Panel;											面板
setVisible;									    可见性true
setSize(x,x);									初始尺寸
setLocation(x,x);								 初始位置,x,y
setBounds(x,x,x,x);								 初始坐标+尺寸
setBackground(new color(x,x,x));   		          颜色,三基色
setResizable;								    大小是否可调,true,false
setLayout(new FlowLayout(FlowLayout.LEFT));		   流式布局
frame.add(east,BorderLayout.EAST);				  方向布局
frame.setLayout(new GridLayout(3,2));			  表格布局
ActionListener;									监听器
TextField;									    文本框
textarea;									    文本域
PasswordField;									密码框
Integer.parseInt();								String类转int类
paint;											画笔
MouseAdapter;									鼠标监听器
WindowListener;									窗口监听
KeyListener;									键盘监听
DefaultCloseOperation(WindowConstants.);		 关闭事件(Jframe)
ContentPane;									容器(Jframe)
Layout;											容器自动定位(Jframe)
Button;											按钮
RadioButton;									单选按钮
ButtonGroup;									组
CheckBox;										多选按钮
ComboBox;										下拉框
List;											列表框
Dialog;											对话框
Label;											标签
IconDemo;										图标
ImageIcon;										图片
Scroll;											滚动条
Timer;											定时器
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/288585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号