GUI重新过一遍

GUI编程

简介

Gui 核心技术:Swing AWT

缺点:界面不美观 需要jre环境

AWT

AWT介绍

1.包含很多的接口和类! GUI:

2.元素:窗口,按钮,文本框

3.java.awt包

 

组件和容器

Frame
package GUI.AWT.lesson01;
​
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.BLUE);
​
        //弹出的初始位置
        frame.setLocation(200,200);
​
        //设置大小固定
        frame.setResizable(false);
​
    }
}
​

封装这个类:

package GUI.AWT.lesson01;
​
import java.awt.*;
​
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.cyan);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.GREEN);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.RED);
​
    }
}
​
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);
    }
}

 

注意这里的x无法关闭面板

Panel面板

下面代码解决了关闭事件

package GUI.AWT.lesson01;
​
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
​
//可以看成一个空间,不能单独存在
public class TestPanel {
​
    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(new Color(40,161,35));
​
        //panel设置坐标,相对于fram坐标
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(232, 14, 14));
​
        //把panel放进frame
        frame.add(panel);
​
        frame.setVisible(true);
​
        //监听事件,监听窗口关闭事件
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭时候需要干什么
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
​
}
​

 

布局管理器
1.流式布局
package GUI.AWT.lesson01;
​
import java.awt.*;
​
public class TestFlowLayout {
​
    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.setLayout(new FlowLayout(FlowLayout.RIGHT));//靠右
​
​
        frame.setSize(200,200);
​
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
​
        frame.setVisible(true);
    }
​
}
​

2.东西南北中
package GUI.AWT.lesson01;
​
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(400,400);
        frame.setVisible(true);
    }
​
}
​

 

3.表格布局
package GUI.AWT.lesson01;
​
import java.awt.*;
​
public class TestGridLatout {
​
    public static void main(String[] args) {
        Frame frame = new Frame();
​
        Button button1 = new Button("btn1");
        Button button2 = new Button("btn2");
        Button button3 = new Button("btn3");
        Button button4 = new Button("btn4");
        Button button5 = new Button("btn5");
​
        frame.setLayout(new GridLayout(3,2));
​
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
​
        frame.pack();//java函数,可以自动按最优布局
        frame.setVisible(true);
    }
​
}
​

 

简单练习

package GUI.AWT.lesson01;
​
import java.awt.*;
​
public class ExDemo {
​
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,300);
        frame.setBackground(Color.WHITE);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));
​
        Panel p1 = new Panel(new GridLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new GridLayout());
        Panel p4 = new Panel(new GridLayout(2,2));
​
​
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(p2,BorderLayout.CENTER);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
​
​
        p3.add(new Button("East-2"),BorderLayout.EAST);
​
        for (int i = 1;i<=4;i++) {
            p4.add(new Button("p2-btn-"+i));
        }
        p3.add(p4,BorderLayout.CENTER);
        p3.add(new Button("West-2"),BorderLayout.WEST);
​
        frame.add(p1);
        frame.add(p3);
    }
​
​
}
​

 

事件监听

package GUI.AWT.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 TestActionEvent {
    public static void main(String[] args) {
​
        //按下按钮,触发事件
        Frame frame = new Frame();
​
        Button button = new Button();
​
        //因为:addActionListener() 需要一个ActionListener,所以我们需要构建一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
​
        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        frame.setSize(400,400);
        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("aaaa");
    }
}

多个按钮可以通过一个监听器去实现不同的功能

主要通过

//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值 //可以多个多个按钮只写一个监听器 button2.setActionCommand("button2-stop");

然后判断:

if (e.getActionCommand().equals("button2-stop")){ System.out.println("stop"); }

package GUI.AWT.lesson02;
​
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("button2-stop");
​
        MyMonitor myMonitor = new MyMonitor();
​
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);
​
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
​
        frame.pack();
        frame.setVisible(true);
    }
​
}
​
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
//        System.out.println("按钮被点击了:msg"+e.getActionCommand());
//        e.getActionCommand();  获得按钮的信息
        if(e.getActionCommand().equals("start")){
            System.out.println("start");
        }
        if (e.getActionCommand().equals("button2-stop")){
            System.out.println("stop");
        }
    }
}

文本框监听

package GUI.AWT.lesson02;
​
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
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();
        //按下enter,就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);
​
        //设置替换编码
        textField.setEchoChar('*');
​
        pack();
        setVisible(true);
    }
}
​
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field =(TextField) e.getSource();//获得一些资源,返回一个对象
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");//设置文本框为空白
    }
}

简单练习加法计算器

package GUI.AWT.lesson02;
​
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class TestCalc {
​
    public static void main(String[] args) {
        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 MyCalculator(num1,num2,num3));
        //一个标签
        Label label = new Label("+");
​
        //布局
​
        setLayout(new FlowLayout());
​
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
​
        setSize(400,400);
        pack();
        setVisible(true);
    }
​
}
​
//监听器类
class MyCalculator implements ActionListener{
​
    private TextField num1,num2,num3;
​
    public MyCalculator(TextField num1, TextField num2, TextField num3){
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }
​
    @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("");
    }
}

优化,使用组合和内部类

组合类写法:

package GUI.AWT.lesson02;
​
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class TestCalc {
​
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
​
}
​
//计算器类
class Calculator 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 MyCalculator(this));
​
​
        //布局
​
        setLayout(new FlowLayout());
​
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
​
        setSize(400,400);
        pack();
        setVisible(true);
    }
​
}
​
//监听器类
class MyCalculator implements ActionListener{
​
    //在一个一个类中组合另一个类
    Calculator calculator = null;
​
    public MyCalculator(Calculator calculator){
        this.calculator=calculator;
    }
​
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得数据
        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("");
​
    }
}

完全面向对象,内部类,应该使用这个

package GUI.AWT.lesson02;
​
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class TestCalc {
​
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
​
}
​
//计算器类
class Calculator 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 MyCalculator());
​
        //布局
​
        setLayout(new FlowLayout());
​
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
​
        setSize(400,400);
        pack();
        setVisible(true);
    }
​
    //内部类最大好处,就是可以畅通无阻的访问外部类
    //监听器类
    private class MyCalculator 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("");
​
        }
    }
​
}
​
​
内部类最大好处就是可以畅通无阻的访问外部类

画笔

package GUI.AWT.lesson03;
​
import java.awt.*;
​
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.fillOval(100,100,100,100);
​
        g.setColor(Color.green);
        g.fillRect(200,200,200,200);
        //养成习惯,画笔用完,将其初始化
        g.setColor(Color.black);
    }
​
}
​

鼠标监听

package GUI.AWT.lesson03;
​
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
​
//鼠标监听
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.BLUE);
            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();
        }
    }
​
}
​

窗口监听

package GUI.AWT.lesson03;
​
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(){
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
//        addWindowListener(new MyWindowListener());
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        //关闭事件
                        System.out.println("你点击了关闭");
                        System.exit(0);
                    }
​
​
                    @Override
                    public void windowActivated(WindowEvent e) {
                        //激活事件
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("被激活了;");
                        System.out.println("windowActivated");
                    }
                }
        );
​
    }
​
​
}
​

键盘监听

package GUI.AWT.lesson03;
​
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
​
//键盘监听
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();//不需要记住这个数值,直接使用静态属性 VK_XXX
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }
​
            }
        });
    }
}

Swing

窗口面板

package GUI.Swing.lesson01;
​
import javax.swing.*;
import java.awt.*;
​
public class JFrameDemo02 {
​
    public static void main(String[] args) {
        new MyJFrame2().init();
    }
​
}
​
class MyJFrame2 extends JFrame{
    public void init(){
        this.setBounds(100,100,200,300);
        this.setVisible(true);
​
        JLabel label = new JLabel("欢迎光临");
        this.add(label);
​
        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
​
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.blue);
    }
}
​

弹窗

弹窗默认有关闭事件

package GUI.Swing.lesson01;
​
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class DialogDemo extends JFrame {
​
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
​
        //JFrame 放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
​
        //按钮
        JButton button = new JButton("点击弹出弹窗");
        button.setBounds(30,30,200,50);
​
        this.add(button);
​
        //监听器
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });
    }
​
    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("学习"));
    }
}

标签

label

new Label("名字")

图标ICON

package GUI.Swing.lesson01;
​
import javax.swing.*;
import java.awt.*;
import java.net.URL;
​
public class ImageIconDemo02 extends JFrame {
​
    public static void main(String[] args) {
        new ImageIconDemo02();
    }
​
    public ImageIconDemo02(){
        //获取图片地址
        JLabel label = new JLabel("ImageIconDemo02");
        //获取了当前类的地址
        URL url = ImageIconDemo02.class.getResource("2.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);
    }
}
​

面板

JPanl

package GUI.Swing.lesson02;
​
import javax.swing.*;
import java.awt.*;
​
public class JPanelDemo extends JFrame {
​
    public static void main(String[] args) {
        new JPanelDemo();
    }
​
    public JPanelDemo(){
        Container container = this.getContentPane();
​
        container.setLayout(new GridLayout(2,1,10,10));
​
        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,2));
        JPanel jPanel3 = new JPanel(new GridLayout(2,1));
        JPanel jPanel4 = new JPanel(new GridLayout(3,2));
​
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
​
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
​
        jPanel3.add(new JButton("3"));
        jPanel3.add(new JButton("3"));
        jPanel3.add(new JButton("3"));
​
        jPanel4.add(new JButton("4"));
        jPanel4.add(new JButton("4"));
        jPanel4.add(new JButton("4"));
​
        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        container.add(jPanel4);
​
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
}
​

JSCrallPanel 滚动条

//Scroll面板 JScrollPane jScrollPane = new JScrollPane(textArea); container.add(jScrollPane);

package GUI.Swing.lesson02;
​
import javax.swing.*;
import java.awt.*;
​
public class JScrollDemo extends JFrame {
​
    public static void main(String[] args) {
        new JScrollDemo();
    }
​
    public JScrollDemo(){
​
        Container container = this.getContentPane();
​
        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("欢迎光临");
​
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
​
    }
​
}
​

按钮

图片按钮
package GUI.Swing.lesson02;
​
import javax.swing.*;
import java.awt.*;
import java.net.URL;
​
public class JButtonDemo01 extends JFrame {
​
    public static void main(String[] args) {
        new JButtonDemo01();
    }
​
    public JButtonDemo01(){
        Container container = this.getContentPane();
        URL url = JButtonDemo01.class.getResource("2.jpg");
        Icon icon = new ImageIcon(url);
​
        //把图片变成按钮
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
​
        container.add(button);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
    }
​
}
​

单选按钮
package GUI.Swing.lesson02;
​
import javax.swing.*;
import java.awt.*;
import java.net.URL;
​
public class JButtonDemo02 extends JFrame {
​
    public static void main(String[] args) {
        new JButtonDemo02();
    }
​
    public JButtonDemo02(){
        Container container = this.getContentPane();
        URL url = JButtonDemo02.class.getResource("2.jpg");
        Icon icon = new ImageIcon(url);
​
       //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton3");
​
        //因为单选只能选择一个,所以需要分组
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
​
        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
    }
​
}
​

复选按钮
package GUI.Swing.lesson02;
​
import javax.swing.*;
import java.awt.*;
import java.net.URL;
​
public class JButtonDemo03 extends JFrame {
​
    public static void main(String[] args) {
        new JButtonDemo03();
    }
​
    public JButtonDemo03(){
        Container container = this.getContentPane();
        URL url = JButtonDemo03.class.getResource("2.jpg");
        Icon icon = new ImageIcon(url);
​
       //多选框
        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
​
        container.add(jCheckBox1,BorderLayout.NORTH);
        container.add(jCheckBox2,BorderLayout.SOUTH);
​
​
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
    }
​
}
​

列表

下拉框
package GUI.Swing.lesson03;
​
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
​
public class TestComboboxDemo01 extends JFrame {
    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
​
    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.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
​
    }
​
}
​

列表框
package GUI.Swing.lesson03;
​
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
​
public class TestComboboxDemo02 extends JFrame {
    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
​
    public TestComboboxDemo02(){
​
        Container container = this.getContentPane();
​
        //生成列表的内容
//        String[] contents = {"1","2","3"};
​
        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);
​
        contents.add("ll");
        contents.add("kk");
        contents.add("zz");
​
        container.add(jList);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
​
    }
​
}
​
文本框
文本框
package GUI.Swing.lesson03;
​
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
​
public class TestTextDemo01 extends JFrame {
    public static void main(String[] args) {
        new TestTextDemo01();
    }
​
    public TestTextDemo01(){
​
        Container container = this.getContentPane();
​
        //文本框
        JTextField textField = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);
​
        container.add(textField,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
​
    }
​
}
​
密码框
package GUI.Swing.lesson03;
​
import javax.swing.*;
import java.awt.*;
​
public class TestTextDemo03 extends JFrame {
    public static void main(String[] args) {
        new TestTextDemo03();
    }
​
    public TestTextDemo03(){
​
        Container container = this.getContentPane();
​
        //密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
​
        container.add(passwordField);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,300,300);
​
    }
​
}
​
文本域
 //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("欢迎光临");
​
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);

简单练习贪吃蛇

帧,只要时间片足够小,就是动画,一秒三十帧,六十帧,连起来就是动画拆开为静态的图片

键盘监听,定时器Time

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值