GUI编程

简介

  • GUI的核心技术:Swing AWT
    • 界面不美观
    • 需要jre 环境

AWT

Awt介绍

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

组件和容器

public static void main(String[] args) {

    Frame frame = new Frame("第一个java图形界面窗口");

    //设置可见性
    frame.setVisible(true);
    //设置宽高
    frame.setSize(400, 400);
    //设置颜色
    frame.setBackground(new Color(56, 101, 182));
    //设置坐标
    frame.setLocation(200,200);
    //设置窗口大小固定
    frame.setResizable(false);
}

尝试封装:

package com.liu.lesson01;

import java.awt.*;

public class TestFrame02 {
    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);
    }
}

面板

package com.liu.lesson01;

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

public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();

        frame.setLayout(null);
        //设置 x y 宽高
        frame.setBounds(300, 300, 500, 500);
        //设置颜色
        frame.setBackground(new Color(88, 152, 217));

        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(169, 107, 231));

        frame.add(panel);

        //设置可见性
        frame.setVisible(true);

        //监听事件,监听窗口关闭
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

布局管理器

按钮

package com.liu.lesson01;

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

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //组件-按钮
        Button button01 = new Button("button01");
        Button button02 = new Button("button02");
        Button button03 = new Button("button03");
        //设置为流式布局
//        frame.setLayout(new FlowLayout());
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(200, 200);

        frame.add(button01);
        frame.add(button02);
        frame.add(button03);
        frame.setVisible(true);

        //监听事件,监听窗口关闭
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

东西南北中

package com.liu.lesson01;

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

public class TestBorderLayout {
    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(300, 300);
        frame.setVisible(true);
        //监听事件,监听窗口关闭
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

表格

package com.liu.lesson01;

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

public class TestTable {
    public static void main(String[] args) {
        Frame frame = new Frame("TestTable");
        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();//java函数
        frame.setVisible(true);
        //监听事件,监听窗口关闭
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
} 

监听事件

package com.liu.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();

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

        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);
    }

    /**
     * 关闭窗口事件
     *
     * @param 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("bbbb");
    }
}

输入框 监听

package com.liu.lesson02;

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

public class TestText01 {
    public static void main(String[] args) {
        new MyFrane();
    }
}

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

        MyActionListener2 my = new MyActionListener2();
        textField.addActionListener(my);

        textField.setEchoChar('*');
        setSize(300,300);
        setVisible(true);
        pack();
    }
}

class MyActionListener2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获的资源
        System.out.println(field.getText()); //输出文字,获的文本框
        field.setText("");
    }
}

画笔

package com.liu.lesson03;

import java.awt.*;

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

class MyPaint extends Frame {

    public void loadFrame() {
        setBounds(300, 300, 600, 800);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
       g.setColor(Color.green);
       g.drawOval(100,100,100,100);//空心圆
       g.fillOval(300,100,100,100);//实心圆

       g.setColor(Color.pink);
       g.fillRect(500,100,100,100);//矩形
    }
}

鼠标监听

package com.liu.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("aa");
    }
}

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 addPaint(Point point) {
        points.add(point);
    }

    //适配器模式
    private class MyMouseListener extends MouseAdapter {
        //鼠标按下,弹起,长按
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //鼠标点
            myFrame.addPaint(new Point(e.getX(), e.getY()));
            
            //重新画
            myFrame.repaint();
        }
    }
}

窗口监听

package com.liu.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.green);
        setBounds(100, 100, 200, 200);
        setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            //窗口打开
            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("窗口打开");
            }

            //窗口关闭
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.exit(0);
            }

            //窗口关闭中
            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("windowClosed");
            }

            //窗口激活
            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("windowActivated");
            }
        });
    }
}

键盘监听

package com.liu.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();
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("上键");
                }
            }
        });
    }
}

Swing

窗口

package com.liu.lesson04;

import javax.swing.*;

public class JFrameDemo {
    public void init() {
        JFrame frame = new JFrame("这是一个JFrame");
        frame.setVisible(true);
        frame.setBounds(100, 100, 200, 200);

        //设置文字
        JLabel l = new JLabel("欢迎好好说说");
        frame.add(l);

        l.setHorizontalAlignment(SwingConstants.CENTER);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

弹窗

package com.liu.lesson04;

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

public class DialogDemo extends JFrame {

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

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

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

        //点击按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });

        container.add(button);

    }

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


class MyDialogDemo extends JDialog {

    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100, 100, 500, 500);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.add(new Label("dmkfdkasfh"));
    }
}

icon

package com.liu.lesson04;

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

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(20, 20);
        //图标在标签上
        JLabel label = new JLabel("BB", iconDemo, SwingConstants.CENTER);

        URL url = IconDemo.class.getResource("tx.png");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);

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


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

    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;
    }
}
posted @ 2020-12-30 21:36  流年りゅう  阅读(115)  评论(0)    收藏  举报