GUI编程-图形用户界面编程

流过昼夜 提交于 2020-02-27 10:42:43

1、GUI简介

GUI的核心技术:Swing、AWT。
现在基本没人使用。
为什么学习GUI:
1、可以写一些小工具
2、工作时候,有可能需要维护到Swing界面
3、了解MVC架构,了解监听。

2、AWT(抽象的窗口工具)

包含了很多的类和接口。存在于java.awt包下。
元素:窗口、按钮、文本框……
在这里插入图片描述

2.1、组件和容器

Frame(窗口)

//gui的第一个界面
public class FrameTest {
    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个gui窗口");
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(300,300);
        //设置背景颜色
        frame.setBackground(new Color(9, 9,255));
        //设置弹出的初始位置
        frame.setLocation(200,200);
        //设置窗口大小固定
        frame.setResizable(false);//默认是true,不固定   false固定
    }
}

停止java程序,才可以关掉窗口。

public class FrameTest2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100, 100, 300, 300, Color.blue);
        MyFrame myFrame2 = new MyFrame(400, 100, 300, 300, Color.red);
        MyFrame myFrame3 = new MyFrame(100, 400, 300, 300, Color.green);
        MyFrame myFrame4 = new MyFrame(400, 400, 300, 300, Color.yellow);
    }
}
class MyFrame extends Frame{
    static int num =0;//窗口计数器

    public MyFrame(int x,int y,int w,int h,Color color){
        super("我的窗口:"+(++num));
        setBackground(color);//设置颜色
        setBounds(x,y,w,h);
        setVisible(true);
    }
}

Panel(面板)

panel无法单独显示,必须添加到某个容器中

public class PanelTest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);
        //设置窗口坐标
        frame.setBounds(100,100,300,300);
        frame.setBackground(Color.green);
        frame.setVisible(true);
        //设置面板坐标
        panel.setBounds(50,50,200,200);
        panel.setBackground(new Color(110, 5, 99));
        //添加面板
        frame.add(panel);

        //监听器,监听窗口关闭事件  解决了窗口关闭的时间
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//结束程序
            }
        });
    }
}

2.2、布局管理器

  • 流式布局 FlowLayout
//流式布局
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.RIGHT));//居右
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//居左
        frame.setSize(200,200);
        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}
  • 东西南北中布局
//东西南北中布局
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("东西南北中布局");
        //组件-按钮
        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.setSize(200,200);
        //添加按钮,设置布局
        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);
    }
}
  • 表格布局 GridLayout
//表格布局
public class TestGridLayout {
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        frame.setSize(200,200);
        //设置表格布局
        frame.setLayout(new GridLayout(3,2));
        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.setVisible(true);
    }
}

练习:

public class TestLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("练习布局");
        frame.setSize(300,100);
        frame.setLocation(100,200);
        frame.setBackground(Color.red);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));
        //创建四个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));
        frame.add(p1);
        frame.add(p3);

        //组件-按钮
        Button bt1 = new Button("bt");
        Button bt2 = new Button("bt");
        Button bt3 = new Button("bt");
        Button bt4 = new Button("bt");
        Button bt5 = new Button("bt");
        Button bt6 = new Button("bt");
        Button bt7 = new Button("bt");
        Button bt8 = new Button("bt");
        Button bt9 = new Button("bt");
        Button bt10 = new Button("bt");

        p1.add(bt1,BorderLayout.EAST);
        p1.add(bt2,BorderLayout.WEST);
        p2.add(bt3);
        p2.add(bt4);
        p1.add(p2,BorderLayout.CENTER);

        p3.add(bt5,BorderLayout.EAST);
        p3.add(bt6,BorderLayout.WEST);
        p4.add(bt7);
        p4.add(bt8);
        p4.add(bt9);
        p4.add(bt10);
        p3.add(p4,BorderLayout.CENTER);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

2.3、事件监听

当某个事情发生时,干什么

public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事情
        Frame frame = new Frame();
        Button button = new Button();
        //因为addActionListener 需要一个addActionListener对象
        // 所以我们构造一个addActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();//java函数,自适应窗口的大小位置
        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("监听了");

    }
}

2.4、文本框 TextField

public class TestText01 {
    public static void main(String[] args) {
        new MyFrame();//启动
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听文本框输入的文字
        MyActionListener3 myActionListener3 = new MyActionListener3();
        //按下enter,就会触发这个输入框的事件
        textField.addActionListener(myActionListener3);
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
//事件监听
class MyActionListener3 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getSource()  获得一些资源,返回的一个对象
        TextField field =(TextField)e.getSource();
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");
    }
}

2.5、简易计算器练习

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}
class Calculator extends Frame{
    public Calculator(){
        //创建三个文本框
        TextField num1 = new TextField(5);
        TextField num2 = new TextField(5);
        TextField num3 = new TextField(10);
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyActionListener4(num1,num2,num3));
        //一个标签
        Label label = new Label("+");
        //流式布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
class MyActionListener4 implements ActionListener{

    //获取三个变量
    private TextField num1,num2,num3;

    public MyActionListener4(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("");
    }
}

2.6、画笔

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame{
    public void loadFrame(){
        setBounds(100,100,500,500);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
        //画笔需要有颜色,画笔可以画画
        g.setColor(Color.red);//画笔颜色
        g.drawOval(100,200,100,100);//空心圆
        g.fillOval(50,50,100,100);//实心圆
        g.fillRect(200,200,150,150);//正方形
    }
}

2.7、鼠标监听

想要实现鼠标画画

//鼠标监听事件
public class TestMoseListener {
    public static void main(String[] args) {
        new MyFrame2("画图");
    }
}
//自己的类
class MyFrame2 extends Frame{
    //画笔需要画画,需要监听鼠标当前的位置,需要存储鼠标的点
    ArrayList points;
    public MyFrame2(String title){
        super(title);
        points = new ArrayList<>();//存鼠标的点
        setBounds(100,100,200,300);
        setVisible(true);
        this.addMouseListener(new MyMouseListener());//鼠标监听
    }

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        if (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) {
            MyFrame2 frame = (MyFrame2)e.getSource();//返回一些资源
            //这个点我们点击的时候,会在界面上产生一个点。
            //这个点就是鼠标的点
            frame.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            frame.repaint();//刷新
        }
    }
}

2.8、键盘监听

//键盘监听
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(100,100,200,300);
        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("你按的是上键");
                }
            }
        });
    }

}

3、Swing

3.1、窗口(JFrame)、面板(JPanel)

与Frame基本相同,设置背景颜色需要一个容器,不能直接给JFrame设置

//Swing  窗口 面板
public class JFrameTest {
    public static void main(String[] args) {
        new JFrameTest().init();
    }
    //init()  初始化
    public void init(){
        JFrame jf = new JFrame("第一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,300);

        JLabel label = new JLabel("欢迎来到狂神说java");
        jf.add(label);
        //标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //关闭窗口事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置背景颜色需要获取一个容器,给容器设置
        Container contentPane = jf.getContentPane();
        contentPane.setBackground(Color.blue);
    }
}

3.2、弹窗(JDialog)

默认就有关闭事件

//弹窗
public class DialogTest extends JFrame {
    public static void main(String[] args) {
        new DialogTest();
    }
    public DialogTest(){
        this.setBounds(100,100,200,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //获取一个容器  放东西
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);//默认布局
        //按钮
        JButton jButton = new JButton("弹出对话框");
        jButton.setBounds(10,10,50,50);
        //点击这个按钮,弹出一个对话框
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });
        contentPane.add(jButton);//添加按钮
    }
}
//弹窗的窗口
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(100,100,300,300);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new JLabel("狂神说java"));
    }
}

3.3、标签(图标Icon、图片Image)

//图标,需要实现类
public class IconTest extends JFrame implements Icon {
    public static void main(String[] args) {
        new IconTest().init();
    }
    private int width;
    private int height;
    public IconTest(){};//无参构造
    public IconTest(int width,int height){
        this.width=width;
        this.height=height;
    }
    public void init(){
        IconTest iconTest = new IconTest(10, 10);
        //图标放在标签,也可以放在按钮上
        JLabel label = new JLabel("标签", iconTest, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    @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;
    }
}
//图片
public class ImageIconTest extends JFrame {
    public static void main(String[] args) {
        new ImageIconTest();
    }
    public ImageIconTest(){
        //获取图片地址
        JLabel label = new JLabel("图片标签");
        URL url = ImageIconTest.class.getResource("朱亚文.jpg");//图片存放在项目目录下
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,200,300);
    }
}

3.4、面板JPanel

//面板
public class JPanelTest extends JFrame {
    public static void main(String[] args) {
        new JPanelTest();
    }
    public JPanelTest(){
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1,10,30));//参数的意思,表格布局
        JPanel jPanel = new JPanel(new GridLayout(1, 3));
        JPanel jPane2 = new JPanel(new GridLayout(1, 2));
        JPanel jPane3 = new JPanel(new GridLayout(2, 1));
        JPanel jPane4 = new JPanel(new GridLayout(3, 2));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPane2.add(new JButton("2"));
        jPane2.add(new JButton("2"));
        jPane3.add(new JButton("3"));
        jPane3.add(new JButton("3"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));

        container.add(jPanel);
        container.add(jPane2);
        container.add(jPane3);
        container.add(jPane4);

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

滚动框 JScrollPanel

//滚动框
public class JScrollTest extends JFrame {
    public static void main(String[] args) {
        new JScrollTest();
    }
    public JScrollTest(){
        Container container = this.getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("还有来狂神说学java");

        //scroll 面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.5、按钮

//图片按钮
public class JButtonTest extends JFrame {
    public static void main(String[] args) {
        new JButtonTest();
    }
    public JButtonTest(){
        Container container = getContentPane();
        //将一个图片放在按钮上
        URL url = JButtonTest.class.getResource("朱亚文.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("图片按钮");

        container.add(jButton);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

单选按钮

//单选按钮
public class JButtonTest02 extends JFrame {
    public static void main(String[] args) {
        new JButtonTest02();
    }
    public JButtonTest02(){
        Container container = getContentPane();
        //将一个图片放在按钮上
        URL url = JButtonTest.class.getResource("朱亚文.jpg");
        ImageIcon imageIcon = new ImageIcon(url);

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("1");
        JRadioButton jRadioButton2 = new JRadioButton("2");
        JRadioButton jRadioButton3 = new JRadioButton("3");

        //由于单选框只能选择一个,所以进行分组,一个组只能选择一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);
        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

多选按钮

//多选按钮
public class JButtonTest03 extends JFrame {
    public static void main(String[] args) {
        new JButtonTest03();
    }
    public JButtonTest03(){
        Container container = getContentPane();
        //将一个图片放在按钮上
        URL url = JButtonTest.class.getResource("朱亚文.jpg");
        ImageIcon imageIcon = new ImageIcon(url);

        //多选框
        JCheckBox jCheckBox1 = new JCheckBox("1");
        JCheckBox jCheckBox2 = new JCheckBox("1");
        JCheckBox jCheckBox3 = new JCheckBox("1");

        container.add(jCheckBox1,BorderLayout.SOUTH);
        container.add(jCheckBox2,BorderLayout.CENTER);
        container.add(jCheckBox3,BorderLayout.NORTH);

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

3.6、列表

//下拉框
public class ComboboxTest01 extends JFrame {
    public static void main(String[] args) {
        new ComboboxTest01();
    }
    public ComboboxTest01(){
        Container container = getContentPane();

        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem(null);
        jComboBox.addItem("NBA");
        jComboBox.addItem("cba");
        jComboBox.addItem("足球");

        container.add(jComboBox);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
//列表框
public class ComboboxTest02 extends JFrame {
    public static void main(String[] args) {
        new ComboboxTest02();
    }
    public ComboboxTest02(){
        Container container = getContentPane();

        //生成列表的内容
        Vector contents = new Vector();
        contents.add("你好");
        contents.add("hello");
        contents.add("world");
        //列表中需要放入的内容
        JList jList = new JList(contents);


        container.add(jList);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.7、文本框

//文本框
public class TextTest01 extends JFrame {
    public static void main(String[] args) {
        new TextTest01();
    }
    public TextTest01(){
        Container contentPane = getContentPane();

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

        contentPane.add(textField, BorderLayout.NORTH);
        contentPane.add(textField2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
//密码框
public class TextTest02 extends JFrame {
    public static void main(String[] args) {
        new TextTest02();
    }
    public TextTest02(){

        Container contentPane = getContentPane();

        //面板
        JPasswordField passwordField = new JPasswordField(); //****
        passwordField.setEchoChar('*');

        contentPane.add(passwordField);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
//文本域
public class TextTest03 extends JFrame {
    public static void main(String[] args) {
        new TextTest03();
    }
    public TextTest03(){
        Container contentPane = getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习狂神说Java");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        contentPane.add(scrollPane);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!