欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GUI编程

程序员文章站 2022-03-08 10:24:57
...

GUI编程

GUI图形用户界面编程

1.AWT

1.1 AWT

1)包含了很多类和接口

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

1.2 组件和容器

import java.awt.*;

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

    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个GUI界面");
        //设置可见性
        frame.setVisible(true);
        //设置大小
        frame.setSize(400,400);
        //设置颜色
        frame.setBackground(new Color(28, 234, 234));
        //初始位置
        frame.setLocation(100, 100);
        //窗口固定
        frame.setResizable(false);
    }

}

1.3Panel面板

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(200,200,500,500);
        frame.setBackground(new Color(123,123,123));

        //面板相对于fream进行定位
        panel.setBounds(200,200,100,100);
        panel.setBackground(new Color(255, 0, 0));

        //将panel放进fream中
        frame.add(panel);
        frame.setVisible(true);

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

}

1.4三种布局管理器

1.4.1 流式布局

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 button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("ubtton4");

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

        //将按钮添加到fream中
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);

        frame.setBounds(200,200,400,400);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

1.4.2 东西南北中

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

        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button nouth = new Button("Nouth");
        Button center = new Button("Center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west, BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(nouth,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setBounds(200,200,400,400);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

1.4.3 表格布局

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

public class TestGridLayout {

    public static void main(String[] args) {
        Frame frame = new Frame("我的窗口");

        Button button1 = new Button("bt1");
        Button button2 = new Button("bt2");
        Button button3 = new Button("bt3");
        Button button4 = new Button("bt4");
        Button button5 = new Button("bt5");
        Button button6 = new Button("bt6");

        frame.setLayout(new GridLayout(2,3));

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.setSize(300,300);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

练习

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

public class LianXi {

    public static void main(String[] args) {
        Frame frame = new Frame("练习");
        frame.setBounds(200,200,400,400);
        frame.setLayout(new GridLayout(2,1));

        //四个面板
        Panel p1 = new Panel(new BorderLayout());//上半部分
        Panel p2 = new Panel(new BorderLayout());//下半部分
        Panel p3 = new Panel();//上上面板
        Panel p4 = new Panel();//上下面板

        //上半部分布局
        p1.add(new Button("shang-zuo-but"),BorderLayout.WEST);
        p1.add(new Button("shang-you-but"),BorderLayout.EAST);
        p3.setLayout(new GridLayout(2,1));
        p3.add(new Button("shang-zhong-shang-but"));
        p3.add(new Button("shang-zhong-shang-but"));
        p1.add(p3,BorderLayout.CENTER);

        //下半部分布局
        p2.add(new Button("xia-zuo-but"),BorderLayout.WEST);
        p2.add(new Button("xia-you-but"),BorderLayout.EAST);
        p4.setLayout(new GridLayout(2,2));
        p4.add(new Button("xia-zhong"));
        p4.add(new Button("xia-zhong"));
        p4.add(new Button("xia-zhong"));
        p4.add(new Button("xia-zhong"));
        p2.add(p4,BorderLayout.CENTER);

        //添加到窗口
        frame.add(p1,BorderLayout.NORTH);
        frame.add(p2,BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(LianXi.class.getResource("/com/zhao/zhao1/dog.png")));
        //关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

1.5 事件监听

事件监听:当某个事件发生时,干什么???

监听器

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 TestActionEvevt {

    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();

        button.addActionListener(new MyActionEvevt());

        frame.add(button);
        frame.setBounds(300,300,100,100);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

class MyActionEvevt implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("嘻嘻嘻嘻嘻嘻嘻");
    }
}

/*****************************************************************/
//多个按钮共享一个事件
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 button1 = new Button("start");
        Button button2 = new Button("stop");

        //setActionCommand() 可以设置触发时返回的内容
        button1.setActionCommand("qsfghgfadfgdfjgkjhgfawdfgdgf");
        MyAction action = new MyAction();
        button1.addActionListener(action);
        button2.addActionListener(action);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.setBounds(100,100,100,100);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

class MyAction implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //getActionCommand() 获取按钮中的内容
        System.out.println(e.getActionCommand());
    }
}

输入框事件监听

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 TestText {

    public static void main(String[] args) {
        MyFream myFream = new MyFream();
        myFream.die(myFream);
    }

}

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

        MyJT myJT = new MyJT();
        textField.addActionListener(myJT);
        setBounds(300,300,300,300);
        setVisible(true);
        add(textField);
    }

    public void die(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

案例:计算器

public class JiSuanQi {

    public static void main(String[] args) {
        MyFream myFream = new MyFream();
        myFream.die(myFream);
    }

}

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

public class MyFream extends Frame {

    public MyFream(){
        //三个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField he = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        //一个字符
        Label label = new Label("+");

        //添加到窗口
        setLayout(new FlowLayout());

        Service service = new Service(num1,num2,he);
        button.addActionListener(service);

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(he);

        setLocation(300,300);
        pack();
        setVisible(true);
    }

    public void die(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

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

public class Service implements ActionListener {

    private TextField num1;
    private TextField num2;
    private TextField he;

    public Service(TextField num1, TextField num2, TextField he) {
        this.num1 = num1;
        this.num2 = num2;
        this.he = he;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获取前两个框的值
        int numq = Integer.parseInt(num1.getText());
        int numh = Integer.parseInt(num2.getText());
        //2.将前两个数进行相加,并赋值给he
        he.setText("" + (numq + numh));
        //3.清空前两个框
        num1.setText("");
        num2.setText("");
    }
}

计算器代码改进

public class JiSuanQi {

    public static void main(String[] args) {
        MyFream myFream = new MyFream();
        myFream.loading();
        myFream.die(myFream);
    }

}

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 MyFream extends Frame {

    private TextField num1,num2,he;

    public void loading(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        he = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        setLayout(new FlowLayout());
        Service service = new Service();
        button.addActionListener(service);

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(he);

        setLocation(300,300);
        pack();
        setVisible(true);
    }

    private class Service implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int numq = Integer.parseInt(num1.getText());
            int numh = Integer.parseInt(num2.getText());
            he.setText("" + (numq + numh));
            num1.setText("");
            num2.setText("");
        }
    }

    public void die(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

1.6画笔

import java.awt.*;

public class HuaBi {

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

}

class MyPaint extends Frame{

    public void loading(){
        setBounds(300,300,300,300);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {//重写paint()方法即可画

        g.setColor(Color.CYAN);
        g.drawOval(100,100,100,100);

        g.setColor(Color.BLUE);
        g.fillOval(50,50,50,50);
    }
}

1.7 鼠标监听

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class ShuBiaoJianTing {

    public static void main(String[] args) {
        new MyMouse().draw();
    }

}

class MyMouse extends Frame{

    private ArrayList list;
    private Point point;

    public void draw(){
        setBounds(200,200,500,500);
        setVisible(true);

        //用来存放点
        list = new ArrayList();

        addMouseListener(new MyMouseListener());

    }

    @Override
    public void paint(Graphics g) {

        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            point = (Point) iterator.next();
            g.fillOval(point.x,point.y , 10,10 );
        }
    }

    public void cun(Point point){
        list.add(point);
    }

    private static class MyMouseListener extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            MyMouse myMouse = (MyMouse) e.getSource();
            //将鼠标的坐标存入集合
            myMouse.cun(new Point(e.getX(),e.getY()));
            // 重画一次
            myMouse.repaint();
        }
    }
}

1.8 窗口监听

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

public class TestWindow {
    public static void main(String[] args) {
        new MyWindow().win();
    }
}

class MyWindow extends Frame{
    public void win(){
        setVisible(true);
        setBounds(200,200,200,200);
        addWindowListener(new MyWin());
    }
}

class MyWin extends WindowAdapter{

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println("windowClosing");
        System.exit(0);
    }
}

1.9 键盘监听事件

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

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

class MyFream extends Frame{
    public MyFream() {
        
        setBounds(100,100,200,200);
        setVisible(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println(e.getKeyCode());
            }
        });
    }
}

2.Swing

2.1 窗口,面板

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

public class TestMyJFream {

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

}

class MyFream extends JFrame{
    public void init(){
        JFrame frame = new JFrame();
        frame.setBounds(100,100,100,100);
        //由于JFrame也是一个容器,所以想要给它设置颜色必须要获取这个容器
        Container container = frame.getContentPane();
        container.setBackground(Color.blue);

        JLabel label = new JLabel("嘻嘻嘻嘻嘻嘻嘻嘻嘻");
        label.setHorizontalAlignment(SwingConstants.CENTER);//文本居中
        frame.add(label);
        frame.setVisible(true);
        //关闭窗口
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

2.2 JDialog弹窗

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

public class TestDiaLog extends JFrame {

    public void init(){

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

        //容器,用于放东西
        Container container = this.getContentPane();

        //设置布局
        this.setLayout(null);

        //按钮
        JButton button = new JButton("点击我,弹出一个框");
        button.setBounds(30,30,200,30);
        container.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDiaLog();
            }
        });



    }

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

}

class MyDiaLog extends JDialog{
    public MyDiaLog() {

        this.setBounds(100,100,100,100);
        this.setVisible(true);


        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new JLabel("bfekjssdgigdgd"));

  

2.3 Icon,ImageIcon标签

Icon:

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

public class TestIcon extends JFrame implements Icon {

    private int width;
    private int height;

    public TestIcon(){
    }

    public TestIcon(int width, int height) throws HeadlessException {
        this.width = width;
        this.height = height;
    }

    public void init(){
        setVisible(true);
        setBounds(200,200,300,300);
        TestIcon testIcon = new TestIcon(10, 10);
        JLabel label = new JLabel("这是一段文本",testIcon,SwingConstants.CENTER);
        Container container = this.getContentPane();
        container.add(label);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

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

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

ImageIcon:

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

public class TestImageIcon extends JFrame  {

    public TestImageIcon(){
        setVisible(true);
        setBounds(100,100,500,500);

        JLabel label = new JLabel("图片");
        URL url = TestImageIcon.class.getResource("1.png");
        System.out.println(url);
        ImageIcon icon = new ImageIcon(url);
        Container container = this.getContentPane();
        label.setIcon(icon);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(label);
    }

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

}

2.4 面板

JPancel:

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

public class TestJPancel extends JFrame {

    public TestJPancel(){
        Container container = this.getContentPane();
        JPanel panel1 = new JPanel(new GridLayout(1,2));

        panel1.add(new JButton("1234"));
        panel1.add(new JButton("1234"));
        panel1.add(new JButton("1234"));

        container.add(panel1);

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

    }

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

}

JScroll:

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

public class TestJScroll extends JFrame {

    public TestJScroll() {
        Container container = this.getContentPane();
        
        JTextArea textArea = new JTextArea(20,20);
        textArea.setText("嘻嘻嘻嘻嘻嘻嘻嘻");
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

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

    }

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

}

2.5 按钮

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

public class TestButton extends JFrame {

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

        URL url = TestButton.class.getResource("1.png");
        Icon icon = new ImageIcon(url);
        JButton button = new JButton();
        button.setIcon(icon);

        container.add(button);

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

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

}

2.6 单选框与多选框

单选框:

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

public class Testdanxuankuang extends JFrame {

    public Testdanxuankuang(){
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        Container container = this.getContentPane();

        JRadioButton radioButton1 = new JRadioButton("选项1");
        JRadioButton radioButton2 = new JRadioButton("选项2");
        JRadioButton radioButton3 = new JRadioButton("选项3");
		
        //单选框必须要放到组件里,不然就不会实现单选效果
        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);
    }

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

}

多选:

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

public class Testdanxuankuang extends JFrame {

    public Testdanxuankuang(){
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        Container container = this.getContentPane();

        JCheckBox checkBox1 = new JCheckBox("选项1");
        JCheckBox checkBox2 = new JCheckBox("选项2");
        JCheckBox checkBox3 = new JCheckBox("选项3");
		//多选框不用放在组合里 
        container.add(checkBox1,BorderLayout.CENTER);
        container.add(checkBox2,BorderLayout.NORTH);
        container.add(checkBox3,BorderLayout.SOUTH);
    }

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

}

2.7 列表

下拉框:

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

public class TestLieBiao extends JFrame {

    public TestLieBiao(){
        this.setVisible(true);
        this.setBounds(100,100,100,100);

        JComboBox comboBox = new JComboBox();
        comboBox.addItem("1111111111");
        comboBox.addItem("1111111112");
        comboBox.addItem("1111111113");
        comboBox.addItem("1111111114");
        comboBox.addItem("1111111115");

        Container container = this.getContentPane();
        container.add(comboBox);
    }

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

}

列表框

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

public class TestLieBiao extends JFrame {
    public TestLieBiao(){
        this.setVisible(true);
        this.setBounds(100,100,100,100);

        //定义列表的内容
        String[] srts = {"111","222","333","444"};

        JList list = new JList(srts);

        Container container = this.getContentPane();
        container.add(list);
    }

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

2.8 文本框

文本框


密码框

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

public class TestText extends JFrame {

    public TestText(){
        setBounds(100,100,300,300);
        setVisible(true);
        Container container = getContentPane();

        JPasswordField passwordField = new JPasswordField("1234567898765432");

        container.add(passwordField);
    }

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

}

文本域

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

public class TestText extends JFrame {

    public TestText(){
        setBounds(100,100,300,300);
        setVisible(true);
        Container container = getContentPane();

        JTextArea textArea = new JTextArea("AAAAAAAAAAAAA");
        JTextArea textArea1 = new JTextArea("BBBBBBBBBBBBB");

        container.add(textArea,BorderLayout.NORTH);
        container.add(textArea1,BorderLayout.SOUTH);
    }

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

}
相关标签: swing java