GUI编程学习

GUI编程学习

1、简介

GUI的核心技术:Swing AWT

①界面不美观

②需要jre环境

2、AWT

2.1 AWT介绍

  1. 包括了很多类
  2. 元素:窗口、按钮、文本框等
  3. 在java.awt包下
    在这里插入图片描述

button:按钮

TextArea:文本域

Label:标题

Frame:窗口

Dialog:弹窗

Applet:用来网页编程

2.2 组件和容器

1.Frame
import java.awt.*;

//GUI的第一个界面
public class TestFrame {

    public static void main(String[] args) {

        //创建一个frame,用来显示标题栏
        Frame frame = new Frame("我的第一个Java图像界面窗口!");

        //让窗口显示出来
        frame.setVisible(true);

        //设置窗口的大小
        frame.setSize(400,400);

        //设置窗口的颜色
        frame.setBackground(new Color(1,1,1));

        //设置窗口弹出的位置
        frame.setLocation(400,400);

        //设置窗口的大小不可调节
        frame.setResizable(false);




    }

}
2.面板Panel
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(400,300,500,500);
        frame.setBackground(new Color(226, 10, 10));

        panel.setBounds(100,100,200,200);
        panel.setBackground(new Color(72, 219, 20));

        frame.add(panel);

        frame.setVisible(true);

        //编写监听事件 监听窗口的关闭
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭发生的事件
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


}
3.布局管理器
  • 流式布局
import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        //界面窗口
        Frame frame = new Frame();

        //设置布局为流式布局
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setSize(400,400);
        frame.setVisible(true);
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");


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



    }

}
  • 东西南北中
import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");

        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.setVisible(true);
        frame.setSize(400,400);
        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);


    }

}
  • 表格
import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");

        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.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);
        frame.setSize(1000,1000);

        //pack()是一个Java的函数,作用是调节窗口的大小,使窗口大小正好把fram里面的内容包裹起来
        frame.pack();

    }

}
4.事件监听

事件监听:当一个事件发生时,要怎么处理

5.输入框TextField监听
6.简易计算器

面向过程代码:

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

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

        //创建一个标签
        Label label = new Label("+");

        //创建一个按钮
        Button button = new Button("=");

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

        //给button按钮添加事件
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));
        setVisible(true);
        pack();

    }
}

class MyCalculatorListener implements ActionListener{
    private TextField num1,num2,num3;
    public MyCalculatorListener(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("");

    }
}

组合优化:

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

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

        //创建一个标签
        Label label = new Label("+");

        //创建一个按钮
        Button button = new Button("=");

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

        //给button按钮添加事件
        button.addActionListener(new MyCalculatorListener(this));
        setVisible(true);
        pack();
    }

    public Calculator(){


    }
}

class MyCalculatorListener implements ActionListener{
    Calculator calculator = null;
    public MyCalculatorListener(Calculator calculator){
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获得加数和被加数
        int num1 = Integer.parseInt(calculator.num1.getText());
        int num2 = Integer.parseInt(calculator.num2.getText());


        //将第三个数放到第三个框
        calculator.num3.setText(""+(num1+num2));

        //清楚前两个框
        calculator.num1.setText("");
        calculator.num2.setText("");

    }
}

面向对象写法(内部类):

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

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

        //创建一个标签
        Label label = new Label("+");

        //创建一个按钮
        Button button = new Button("=");

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

        //给button按钮添加事件
        button.addActionListener(new MyCalculatorListener());
        setVisible(true);
        pack();
    }

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

        }
    }


}
7.画笔
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,500,500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //super.paint(g);
        //画笔需要有颜色才能画画
        g.setColor(Color.red);

        //画一个空心的圆
        g.drawOval(100,100,50,50);

        g.setColor(Color.green);

        g.fillOval(200,200,40,40);

        //画笔用完需要还原最初的颜色(黑色)
    }
}
8.鼠标监听
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 MyFram("画图");
    }

}

class MyFram extends Frame{
    //画画需要画笔
    ArrayList points;


    public MyFram(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.red);
            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) {
            MyFram fram = (MyFram) e.getSource();
            //这里我们点击就会在界面产生一个点
            fram.addPaint(new Point(e.getX(),e.getY()));

            //每次点击刷新一次
            fram.repaint();

        }
    }
}
9.窗口监听
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(){

        setBounds(200,200,600,600);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowActivated(WindowEvent e) {
                //当点击脱离弹窗再返回时就会修改标题
                WindowFrame source = (WindowFrame) e.getSource();
                source.setTitle("hahahaah");
            }
        });



    }


}
10.键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKey {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(200,200,600,600);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //当点击方向键的上键时,会返回对应的keyCode
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println(keyCode);
                }
            }
        });

    }
}

3、Swing

1.窗口、面板
import javax.swing.*;
import java.awt.*;

public class JFrameTest {
    //init()初始化
    public void init(){
        //顶级窗口
        JFrame JF = new JFrame("这是我第一个JFrame");
        JF.setVisible(true);
        JF.setBounds(200,200,600,600);
        JF.setBackground(Color.cyan);

        //放到容器里面
        Container container = JF.getContentPane();
        container.setBackground(Color.cyan);

        //添加标签
        JLabel jlabel = new JLabel("你好哈哈哈哈");
        container.add(jlabel);

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

        //关闭
        JF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JFrameTest().init();
    }

}
2.弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class Dialog extends JFrame {
    public Dialog(){
        this.setVisible(true);
        this.setSize(400,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

        //按钮
        JButton button = new JButton("点击弹出");
        button.setBounds(30,20,100,200);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });
        container.add(button);

    }

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

//弹窗的窗口
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        //弹窗有默认关闭的函数,不需要重复写
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

        container.add(new Label("你好哈哈哈哈"));
    }


}
3.标签
import javax.swing.*;
import java.awt.*;

public class IconTest extends JFrame implements Icon {

    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(15,15);

        //图标放在标签上
        JLabel jLabel = new JLabel("icon-test", iconTest, SwingConstants.CENTER);

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


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new IconTest().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;
    }
}
4.面板
import javax.swing.*;
import java.awt.*;

public class JPanelTest extends JFrame {

    //创建构造器
    public JPanelTest(){
        Container container = this.getContentPane();

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

        JPanel panel1 = new JPanel(new GridLayout(2,2));
        JPanel panel2 = new JPanel(new GridLayout(2,2));

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

        Button button11 = new Button("button11");
        Button button21 = new Button("button21");
        Button button31 = new Button("button31");
        Button button41 = new Button("button41");
        panel1.add(button1);
        panel1.add(button2);
        panel1.add(button3);
        panel1.add(button4);

        panel2.add(button11);
        panel2.add(button21);
        panel2.add(button31);
        panel2.add(button41);

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

        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JPanelTest();
    }
}
//JScroll可滑动滑轮
import javax.swing.*;
import java.awt.*;

public class JScrollTest extends JFrame {
    public JScrollTest(){
        //创建容器
        Container container = this.getContentPane();

        JTextArea textArea = new JTextArea(20,20);
        textArea.setText("你好哈哈哈哈");

        //普通面板
//        container.add(textArea);
        //JScroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);

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

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

    }
}

5.按钮
  • 单选按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonTest02 extends JFrame{
    public JButtonTest02(){
        Container container = this.getContentPane();

        //单选按钮
        JRadioButton jRadioButton1 = new JRadioButton("A");
        JRadioButton jRadioButton2 = new JRadioButton("B");
        JRadioButton jRadioButton3 = new JRadioButton("C");
        JRadioButton jRadioButton4 = new JRadioButton("D");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);
        buttonGroup.add(jRadioButton4);

        container.add(jRadioButton1,BorderLayout.SOUTH);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.EAST);
        container.add(jRadioButton4,BorderLayout.WEST);


        this.setVisible(true);
        this.setBounds(200,200,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonTest02();
    }
}
  • 多选按钮

    import javax.swing.*;
    import java.awt.*;
    
    public class JButtonTest03 extends JFrame{
        public JButtonTest03(){
            Container container = this.getContentPane();
    
            //多选按钮
            JCheckBox jcheckBox1 = new JCheckBox("a");
            JCheckBox jcheckBox2 = new JCheckBox("b");
            JCheckBox jcheckBox3 = new JCheckBox("c");
    
            container.add(jcheckBox1,BorderLayout.NORTH);
            container.add(jcheckBox2,BorderLayout.SOUTH);
            container.add(jcheckBox3,BorderLayout.CENTER);
    
    
            this.setVisible(true);
            this.setBounds(200,200,400,400);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new JButtonTest03();
        }
    }
    
6.列表
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JComboBoxTest02 extends JFrame {
    JComboBoxTest02(){
        Container container = this.getContentPane();

        //列表
//        String[] contents = {"1","2","3"};
//
//        JList jlist = new JList(contents);

//        container.add(jlist);

        Vector vector = new Vector();
        JList jList = new JList(vector);

        vector.add("你好");
        vector.add("啊");
        vector.add("宿舍");
        container.add(jList);


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

    }

    public static void main(String[] args) {
        new JComboBoxTest02();
    }
}
7.文本框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JTextFieldTest extends JFrame{
    JTextFieldTest(){
        Container container = this.getContentPane();

        JTextField jTextField1 = new JTextField("Hello");
        JTextField jTextField2 = new JTextField("hi",10);

        container.add(jTextField1,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);



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

    }

    public static void main(String[] args) {
        new JTextFieldTest();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值