GUI编程

GUI编程

告诉大家该怎么学?

GUI编程

告诉大家该怎么学?

  • 这是什么?

  • 它怎么玩?

  • 该如何去在我们平时运用?

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具

1、简介

Gui的核心技术: Swing AWT

  1. 因为界面不美观。

  2. 需要jre环境!

为什么我们要学习?

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

2、AWT

2.1、Awt介绍

  1. 包含了很多类和接口! GUI !
  2. 元素:窗口,按钮,文本框
  3. java.awt

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RsJJVx9M-1588690446645)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502065357.png)]

2.2、组件和容器

1、Frame

		Frame frame = new Frame("我的第一个GUI窗口");

        frame.setVisible(true);

        frame.setSize(400,400);

        frame.setBackground(new Color(122, 133, 255));

        frame.setLocation(200,200);

        frame.setResizable(false);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HPmQwD8C-1588690446649)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502065656.png)]

问题:发现窗口关闭不掉,停止java程序!

尝试回顾封装:

public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.magenta);
    }
}

class MyFrame extends Frame{
    static int id = 0;

    public MyFrame(int x,int y,int w,int h,Color color){
        super("MyFrame+"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);

    }

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uoIThBPY-1588690446650)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502070001.png)]

2、面板Panel

解决了关闭事件!

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

        frame.setLayout(null);

        frame.setBounds(300,300,500,500);
        frame.setBackground(Color.BLUE);

        panel.setBounds(50,50,400,400);
        panel.setBackground(Color.red);
        frame.add(panel);

        frame.setVisible(true);

        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IA8Aq3y8-1588690446651)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502070221.png)]

2.3、布局管理器

  • 流式布局
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

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

        frame.setSize(200,200);

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

        frame.setVisible(true);


    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7D9lNxq6-1588690446653)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502070514.png)]

  • 东西南北中
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(200,200);
        frame.setVisible(true);
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ekjEILYk-1588690446654)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502070640.png)]

  • 表格布局

        public static void main(String[] args) {
            Frame frame = new Frame("TestGridLayout");
    
            Button btn1 = new Button("btn1");
            Button btn2 = new Button("btn2");
            Button btn3 = new Button("btn3");
            Button btn4 = new Button("btn4");
            Button btn5 = new Button("btn5");
            Button btn6 = new Button("btn6");
    
            frame.setLayout(new GridLayout(3,2));
    
    
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            frame.add(btn4);
            frame.add(btn5);
            frame.add(btn6);
    
            frame.pack();
            frame.setVisible(true);
        }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OIKfNvnm-1588690446655)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502070721.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JCfYFgRQ-1588690446656)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502070834.png)]

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

        //4个面板
        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-btn-1"));
        panel2.add(new Button("p2-btn-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("for-"+i));
        }
        panel3.add(panel4,BorderLayout.CENTER);

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

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lAxhRCmO-1588690446656)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502071045.png)]

总结:

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中。
  3. 布局管理器
    1.流式
    2.东西南北中
    3.表格
    4.大小,定位,背景颜色,可见性,监听

2.4、事件监听

事件监听:当某个事情发生的时候,干什么?

package com.kuang.lesson02;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 8:34
 */
public class TestActionEvent {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();

        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        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("aaaa");
    }
}

多个按钮,共享一个事件

package com.kuang.lesson02;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 8:53
 */
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("button2-stop");

        MyMonitor myMonitor = new MyMonitor();

        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setBounds(300,400,400,400);
    }
}
//事件监听
class MyMonitor implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了:msg  "+e.getActionCommand());
        if ("start".equals(e.getActionCommand())){
            System.out.println("匹配到了");
        }
    }
}

2.5、输入框TextField监听

package com.kuang.lesson02;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 9:03
 */
public class TestText01 {
    public static void main(String[] args) {
        //启动
        new MyFrame();
    }
}

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

        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        
        textField.addActionListener(myActionListener2);

        textField.setEchoChar('*');

        setVisible(true);
        pack();
    }
}

class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField)e.getSource(); //获得一些资源,返回的一个对象
        System.out.println(textField.getText());    //获得输入框的文本
        textField.setText("");
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4oHjpw37-1588690446657)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502072143.png)]

2.6、简易计算器,组合+内部类回顾复习!

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

内部类:

  • 更好的包装
package com.kuang.lesson02;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 9:12
 */
public class TestCalc {
    public static void main(String[] args) {
        new 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("=");
        Label label = new Label("+");


        button.addActionListener(new MyCalculatorListener());

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

        pack();
        setVisible(true);


    }

    //监听器类
    //内部类最大的好处就是能够畅通无阻的访问外部类的属性和方法
    private class MyCalculatorListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");

        }
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rlFHJRYW-1588690446658)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502072812.png)]

2.7、画笔

package com.kuang.lesson03;

import java.awt.*;

/**
 * @author PeiroJack
 * @create 2020-05-01 10:39
 */
public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{
    public void loadFrame(){
        setBounds(200,200,600,500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {

        g.setColor(Color.red);
        g.drawOval(100,100,100,100);

        g.setColor(Color.green);
        g.fillRect(200,200,200,200);

        repaint();
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F5azSBOW-1588690446659)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502072723.png)]

2.8、鼠标监听

目的:想要实现鼠标画画!

package com.kuang.lesson03;


import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author PeiroJack
 * @create 2020-05-01 10:51
 */
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

class MyFrame extends Frame {
    ArrayList points;

    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,300);
        points = new ArrayList<>();
        setVisible(true);
        this.addMouseListener(new MyMouseListener());

    }


    @Override
    public void paint(Graphics g) {
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.YELLOW);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    public void addPoint(Point point){
        points.add(point);
    }


//适配器模式
    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame =(MyFrame) e.getSource();
            frame.addPoint(new Point(e.getX(),e.getY()));

            frame.repaint();
        }

    }

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JZpe0vNd-1588690446660)(C:\Users\Hpr\Desktop\求职之路\狂神学Java\pic\QQ截图20200502073150.png)]

2.9、窗口监听

package com.kuang.lesson03;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 11:35
 */
public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.cyan);
        setBounds(100, 100, 200, 200);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.exit(0);
            }
            
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame windowFrame = (WindowFrame)e.getSource();
                windowFrame.setTitle("被激活了");
                System.out.println("windowActivated");
            }

        });

    }


}

2.10、键盘监听

package com.kuang.lesson03;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 11:46
 */
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{

    public KeyFrame(){
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }
            }
        });
    }
}

3、Swing

3.1、窗口、面板

package com.kuang.lesson04;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 11:59
 */
public class JFrameDemo {
    public void init(){
        JFrame frame = new JFrame();

        frame.setVisible(true);

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

        frame.setBackground(new Color(122, 133, 255));

        JLabel label = new JLabel("学Java");

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}
	

3.2、弹窗

JDialog用来被弹出,默认就有关闭事件

package com.kuang.lesson04;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 20:11
 */
public class DialogDemo extends JFrame {

    public DialogDemo() {
        this.setVisible(true);
        this.setSize(700, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);

        JButton btn = new JButton("点击弹出一个对话框");
        btn.setBounds(30, 30, 200, 50);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });

        container.add(btn);


    }

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

class MyDialogDemo extends JDialog {
    public MyDialogDemo() {
        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("学习Java"));

    }
}

3.3、标签

label

new Label("xxx");

图标ICON

package com.kuang.lesson04;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 20:24
 */
public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;

    public IconDemo()  {
    }

    public IconDemo(int width, int height){
        this.width = width;
        this.height = height;
    }

    public void init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

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

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

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

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }

    @Override
    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

图片标签

package com.kuang.lesson04;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 20:43
 */
public class ImageIconDemo extends JFrame {

    public ImageIconDemo(){
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("icon.jpg");

        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

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

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


3.4、 面板

JPanel

package com.kuang.lesson05;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 20:55
 */
public class JPanelDemo extends JFrame {
    public JPanelDemo(){
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));

        JPanel panel1 = new JPanel(new GridLayout(1,3));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        JPanel panel3 = new JPanel(new GridLayout(2,1));
        JPanel panel4 = new JPanel(new GridLayout(3,2));


        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

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

    }

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

JScroll

package com.kuang.lesson05;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:02
 */
public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();

        JTextArea textArea = new JTextArea(20, 30);
        textArea.setText("学习Java");

        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


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


    }

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

3.5、 按钮

  • 图片按钮

    package com.kuang.lesson05;
    
    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    /**
     * @author PeiroJack
     * @create 2020-05-01 21:14
     */
    public class JButtonDemo01 extends JFrame {
    
        public JButtonDemo01(){
            URL url = JButtonDemo01.class.getResource("icon.jpg");
            Container container = this.getContentPane();
    
            Icon icon = new ImageIcon(url);
    
            JButton button = new JButton();
            button.setIcon(icon);
            button.setToolTipText("图片按钮");
    
            container.add(button);
    
            this.setVisible(true);
            this.setBounds(200,200,500,500);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
    new JButtonDemo01();
        }
    }
    
    
    
  • 单选按钮

package com.kuang.lesson05;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:26
 */
public class JButtonDemo02 extends JFrame {

    public JButtonDemo02(){

        Container container = this.getContentPane();
        URL url = JButtonDemo02.class.getResource("icon.jpg");
        Icon icon = new ImageIcon(url);

        JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton3");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);


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

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


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

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:30
 */
public class JButtonDemo03 extends JFrame {

    public JButtonDemo03(){

        Container container = this.getContentPane();
        URL url = JButtonDemo03.class.getResource("icon.jpg");
        Icon icon = new ImageIcon(url);

        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");

        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.SOUTH);


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

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

3.6、列表

  • 下拉框
package com.kuang.lesson06;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:36
 */
public class TestComboboxDemo01 extends JFrame {

    public TestComboboxDemo01() {

        Container container = this.getContentPane();

        JComboBox status = new JComboBox();

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

        container.add(status);


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

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


  • 列表框
package com.kuang.lesson06;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:42
 */
public class TestComboboxDemo02 extends JFrame {

    public TestComboboxDemo02() {

        Container container = this.getContentPane();

//        String[] contents = {"1","2","3"};
        Vector vector = new Vector();

        JList jList = new JList(vector);

        vector.add("1");
        vector.add("2");
        vector.add("3");

        container.add(jList);


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

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

  • 应用场景
    • 选择地区,或者- -些单个选项
    • 列表,展示信息,一般是动态扩容!

3.7.文本框

  • 文本框

    package com.kuang.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Vector;
    
    /**
     * @author PeiroJack
     * @create 2020-05-01 21:47
     */
    public class TestTextDemo01 extends JFrame {
    
        public TestTextDemo01() {
    
            Container container = this.getContentPane();
    
            JTextField textField1 = new JTextField("hello");
            JTextField textField2 = new JTextField("world",20);
    
            container.add(textField1,BorderLayout.NORTH);
            container.add(textField2,BorderLayout.SOUTH);
    
    
            this.setVisible(true);
            this.setBounds(500,350,500,500);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestTextDemo01();
        }
    }
    
    
  • 密码框

package com.kuang.lesson06;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:47
 */
public class TestTextDemo01 extends JFrame {

    public TestTextDemo01() {

        Container container = this.getContentPane();

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

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


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

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


  • 文本域
package com.kuang.lesson05;

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

/**
 * @author PeiroJack
 * @create 2020-05-01 21:02
 */
public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();

        JTextArea textArea = new JTextArea(20, 30);
        textArea.setText("学习Java");

        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


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


    }

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


贪吃蛇

帧,如果时间片足够小,就是动画,一秒30帧60帧。连起来是动画,拆开就是静态的图片!
键盘监听
定时器Timer


  1. 小蛇不能走它行走的反方向
  2. 小蛇随着等级越大,速度越快
  3. 小蛇吃到毒的果子,就剪短长度
  4. 果子的随机出现不同的地方,不跟蛇重叠5.
  5. 联机对战
  6. 保存到磁盘里面
  7. 优化界面
  8. 鼠标拖拽
  9. 登录用户
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值