0.1-javaGUI

GUI编程

1.简介

GUI核心:Swing,AWT

1.界面不美观

2.需要jre环境

!了解MVC架构,了解监听

2.AWT

2.1.AWT介绍

1.包含很多类和接口

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

2.java.awt

2.2.组件和容器

2.2.1.Frame
import java.awt.*;

public class TestFrame {
    public static void main(String[] args) {

        // a frame
        Frame frame = new Frame("java Frame");
        // 设置可见性
        frame.setVisible(true);
        // 设置窗口大小
        frame.setSize(400,400);
        // 设置背景颜色
        Color color = new Color(215,213,189);
        frame.setBackground(color);
        // 设置位置
        frame.setLocation(200,200);
        // 设置大小固定
        frame.setResizable(false);
    }
}
import java.awt.*;

public class TestFrame2  {

    public static void main(String[] args) {
        Frame frame = new MyFrame("",100,100,100,100,Color.RED);
    }
}

class MyFrame extends Frame{

    public MyFrame(String title,int x, int y, int w, int h, Color color) {
        super(title);
        this.setBounds(x,y,w,h);
        this.setBackground(color);
        this.setVisible(true);
    }
}
2.2.2.Panel
import java.awt.*;

public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        // frame设置
        frame.setBounds(300,300,500,500);
        frame.setBackground(Color.GREEN);
        // 设置布局
        frame.setLayout(null);

        // panel
        Panel panel = new Panel();
        // panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(Color.RED);

        //frame添加panel
        frame.add(panel);

        //设置可见性
        frame.setVisible(true);
        
        // 监听事件
        // 窗口关闭事件
        // 适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

2.3.布局管理器

  • 流式布局

    import java.awt.*;
    
    public class TestFlowLayout {
    
        public static void main(String[] args) {
            Frame frame = new Frame();
            frame.setSize(200,200);
    
            // button
            Button btn1 = new Button("btn1");
            Button btn2 = new Button("btn2");
            Button btn3 = new Button("btn3");
    
            // flow layout
            frame.setLayout(new FlowLayout());
            /*
            流式布局设置对齐方式,无参数默认居中
             frame.setLayout(new FlowLayout(FlowLayout.CENTER));
             */
    
            // add button
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
    
            //
            frame.setVisible(true);
        }
    }
    
  • 东西南北中

    import java.awt.*;
    
    public class TestBorderLayout {
    
        public static void main(String[] args) {
            Frame frame = new Frame();
            frame.setSize(200,200);
    
            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默认布局 BorderLayout
            // Panel默认布局 FlowLayout
            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.setVisible(true);
        }
    }
    
  • 表格布局

    import java.awt.*;
    
    public class TestGridLayout {
    
        public static void main(String[] args) {
            Frame frame = new Frame();
            frame.setSize(200,200);
    
            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);
        }
    }
    
  
  

#### 总结

1. Frame是一个顶级窗口
2. Panel无法单独显示,必须添加到某一个容器中
3. 布局管理器:FlowLayout,BorderLayout,GridLayout
4. 属性:大小,定位,可见性,监听

### 2.4.事件监听

​```java
Button btn = new Button();
btn.addActionListener(new ActionListener(){
   @Override
    public void actionPerformed(ActionEvent e){
        //...
    }
});
TextField 监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText {
    public static void main(String[] args) {
        // 启动
        new MyFrame2();
    }
}

class MyFrame2 extends Frame {

    public MyFrame2(){
        TextField textField = new TextField();
        add(textField);

        // 监听
        MyActionListener listener = new MyActionListener();
        // 按回车触发
        textField.addActionListener(listener);

        // 设置替换编码
        textField.setEchoChar('*');

        //
        this.setVisible(true);
        this.pack();
    }
}

class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField t = (TextField) e.getSource();
        System.out.println(t.getText()); // 获取文本并输出
        t.setText(""); // 重置为空字符串
    }
}

2.5.画笔

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint();
    }
}

class MyPaint extends Frame{

    public MyPaint(){
        this.setBounds(200,200,400,400);
        this.setVisible(true);
    }

    @Override
    public void paint(Graphics g){
        //super.paint(g);
        //画笔颜色
        g.setColor(Color.RED);
        //绘制
//        g.drawString("Ending",10,10);
        g.fillOval(50,50,50,50);
    }
}

2.6.鼠标监听

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

class MyFrame3 extends Frame {

    private ArrayList<Point> points = new ArrayList<>();

    public MyFrame3(){
        this.setBounds(50,50,200,200);
        this.setVisible(true);

        //鼠标监听器
        this.addMouseListener(new MyMouseListener());
    }

    @Override
    public void paint(Graphics g){
        //绘画 监听鼠标事件
        Iterator<Point> iterator = points.iterator();
        while(iterator.hasNext()){
            Point p = iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(p.x,p.y,10,10);
        }
    }
    
    public void addPoint(Point p){
        points.add(p);
    }

    private class MyMouseListener extends MouseAdapter {
        // 鼠标:按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame3 frame = (MyFrame3) e.getSource();
            frame.addPoint(new Point(e.getX(),e.getY()));

            // 每次点击需要重新画
            frame.repaint(); // 执行paint(Graphics g)
        }
    }
}

2.7.窗口监听

class WindowListener extends WindowAdapter{
    @Override
    public void windowClosing(WindowEvent e){
        //...
    }
}

2.8.键盘监听

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(){
        this.setBounds(100,100,400,400);
        this.setVisible(true);

        // 添加键盘监听
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // 获取键盘 keyCode
                int keyCode = e.getKeyCode();
                
                System.out.println(KeyEvent.getKeyText(keyCode));
            }
        });
    }
}

3.Swing

3.1.窗口,面板

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

public class JFrameDemo {

    public void init(){
        // 顶级窗口
        JFrame frame = new JFrame("JFrame");
        frame.setBounds(50,50,400,400);
        frame.setVisible(true);
//        frame.setBackground(Color.GREEN); //无法生效,见17-18行

        // 关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 容器
        Container container = frame.getContentPane();
        container.setBackground(Color.GREEN);

        // JLabel
        JLabel label = new JLabel("label");
//        frame.add(label); // 与下行效果相同
        container.add(label);

        // 标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);


    }

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

3.2.弹窗

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

public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setBounds(50,50,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 容器
        Container container = this.getContentPane();
        // 布局 绝对
        container.setLayout(null);

        // 按钮
        JButton button = new JButton("弹出对话框");
        button.setBounds(30,30,200,50);

        // 添加button
        container.add(button);

        // 点击按钮,弹出弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 弹窗
                new MyDialog();
            }
        });

        //
        this.setVisible(true);
    }

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

class MyDialog extends JDialog{
    public MyDialog(){
        this.setBounds(100,100,500,500);

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

        container.add(new JLabel("hello"));

        this.setVisible(true);
    }

}

3.3.标签

//JLabel
//Icon
import javax.swing.*;
import java.awt.*;

public class IconDemo extends JFrame implements Icon {
    private int wight;
    private int height;
    public IconDemo(){
        this(0,0);
    }
    public IconDemo(int wight,int height){
        this.wight = wight;
        this.height = height;
    }
    public void init(){
        IconDemo icon = new IconDemo(15,15);
        // 图标可以放在标签,按钮上
        JLabel label = new JLabel("label",icon,SwingConstants.CENTER);

        getContentPane().add(label);

        this.setBounds(50,50,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    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,wight,height);
    }

    @Override
    public int getIconWidth() {
        return wight;
    }

    @Override
    public int getIconHeight() {
        return height;
    }
}
// ImageIcon

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

public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        //获取图片地址: 与ImageIcon同级目录下的icon.PNG
        URL url = ImageIconDemo.class.getResource("icon.PNG");
        //ImageIcon
        ImageIcon imageIcon = new ImageIcon(url);

        JLabel label = new JLabel("label");
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

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

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

3.4.面板

JPanel

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

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));
        panel1.add(new JButton("btn1-1"));
        panel1.add(new JButton("btn1-2"));
        panel1.add(new JButton("btn1-3"));

        JPanel panel2 = new JPanel(new GridLayout(1,1));
        panel2.add(new JButton("btn2"));

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

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

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

JScrollPane

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

public class JScrollDemo extends JFrame {

    public JScrollDemo(){
        Container container = this.getContentPane();

        // 文本域
        JTextArea textArea = new JTextArea(20,40);
        textArea.setText("文本域");

        // scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);

        container.add(scrollPane);

        this.setBounds(50,50,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

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

3.5.按钮

  • JButton

    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    public class JButtonDemo extends JFrame {
        public JButtonDemo(){
            Container container = this.getContentPane();
    
            //将图片设置为icon
            URL url = JButtonDemo.class.getResource("icon.PNG");
            Icon icon = new ImageIcon(url);
    
            //
            JButton btn = new JButton("button",icon);
    
            container.add(btn);
    
            this.setBounds(50,50,400,400);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            new JButtonDemo();
        }
    }
    
    
  • 单选按钮

    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    public class JButtonDemo extends JFrame {
        public JButtonDemo(){
            Container container = this.getContentPane();
    
            //将图片设置为icon
            URL url = JButtonDemo.class.getResource("icon.PNG");
            Icon icon = new ImageIcon(url);
    
            //单选框
            JRadioButton jrb1 = new JRadioButton("jrb1");
            JRadioButton jrb2 = new JRadioButton("jrb2");
            JRadioButton jrb3 = new JRadioButton("jrb3");
    
            //单选框只能选择一个,将他们分成一个组
            ButtonGroup group = new ButtonGroup();
            group.add(jrb1);
            group.add(jrb2);
            group.add(jrb3);
    
            container.add(jrb1,BorderLayout.CENTER);
            container.add(jrb2,BorderLayout.NORTH);
            container.add(jrb3,BorderLayout.SOUTH);
    
            this.setBounds(50,50,400,400);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            new JButtonDemo();
        }
    }
    
  • 复选按钮

    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    public class JButtonDemo extends JFrame {
        public JButtonDemo(){
            Container container = this.getContentPane();
    
            //将图片设置为icon
            URL url = JButtonDemo.class.getResource("icon.PNG");
            Icon icon = new ImageIcon(url);
    
            //多选框
            JCheckBox jcb1 = new JCheckBox("jcb1");
            JCheckBox jcb2 = new JCheckBox("jcb2");
            JCheckBox jcb3 = new JCheckBox("jcb3");
    
            container.add(jcb1,BorderLayout.NORTH);
            container.add(jcb2,BorderLayout.CENTER);
            container.add(jcb3,BorderLayout.SOUTH);
    
            this.setBounds(50,50,400,400);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            new JButtonDemo();
        }
    }
    

3.6.列表

  • 下拉框

    import javax.swing.*;
    import java.awt.*;
    
    public class ComboBoxDemo extends JFrame {
        public ComboBoxDemo(){
            Container container = this.getContentPane();
    
            JComboBox<String> status = new JComboBox<>();
            status.addItem("s1");
            status.addItem("s2");
            status.addItem("s3");
    
            container.add(status);
    
            this.setBounds(50,50,400,400);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            new ComboBoxDemo();
        }
    }
    
  • 列表框

    import javax.swing.*;
    import java.awt.*;
    
    public class ComboBoxDemo2 extends JFrame{
        public ComboBoxDemo2(){
            Container container = this.getContentPane();
    
            //生成列表内容
            String[] contents = {"1","2","3"};
            //列表中放入内容
            JList<String> list = new JList<>(contents); //Array Vector
    
            container.add(list);
    
            this.setBounds(50,50,400,400);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            new ComboBoxDemo2();
        }
    }
    

应用场景

  1. 选择地区,或者一些单个选项
  2. 列表,展示信息,一般动态扩容

3.7文本框

  • 文本框
    JTextField

  • 密码框
    JPasswordField
    或者设置 setEchoChar()

  • 文本域
    JTextArea
    可放置在JScrollPane

    new JScrollPane(textArea);
    

Demo

贪吃蛇

  • 概念
    • 键盘监听
    • 定时器 Timer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值