Java自学笔记之GUI编程

GUI编程

GUI简介


GUI的核心技术:Swing、AWT

GUI:图形用户编程

使用领域少的原因

  1. 界面不美观
  2. 需要jre环境支持

为什么需要学习:

  1. 可以写出自己想要的一些小工具
  2. 工作时候,也可能需要维护到swing界面(概率小)
  3. 了解吗MVC架构,了解监听

AWT


AWT介绍

  1. 包含了很多接口和类
  2. 元素:窗口、按钮、文本框
  3. java.awt

在这里插入图片描述

Frame实战

public class TestFrame01 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.green);
        MyFrame myFrame2 = new MyFrame(100,300,200,200,Color.blue);
        MyFrame myFrame3 = new MyFrame(300,100,200,200,Color.red);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.white);
    }
}

//使用构造器
class MyFrame extends Frame{
    static int id = 0;//可能存在多个窗口,所以需要一个计数器

    public MyFrame(int x, int y, int w, int d, Color color){
        super("MyFrame"+(++id));
        setBounds(x,y,w,d);
        setBackground(color);
        setVisible(true);
    }
}

在这里插入图片描述

Panel实战

public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();

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

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

        //设置frame坐标以及背景色
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(0x2BCB00));

        //设置panel坐标以及背景色
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(0xD0070B));

        //将面板放进框架
        frame.add(panel);

        //监听事件,监听窗口关闭事件 System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

布局管理器(frame.setLayout)

按钮流式布局
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        FlowLayout layout = new FlowLayout();

        Button button1 = new Button("First");
        Button button2 = new Button("Second");
        Button button3 = new Button("Third");

        //设置流式布局,按钮默认居中显示 否则:layout.(方位)
        frame.setLayout(layout);
        frame.setSize(500,500);
        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

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

}

在这里插入图片描述

东西南北中布局
public class TestFlowLayout01 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn1 = new Button("East");
        Button btn2 = new Button("West");
        Button btn3 = new Button("South");
        Button btn4 = new Button("North");
        Button btn5 = new Button("Center");

        frame.add(btn1,BorderLayout.EAST);
        frame.add(btn2,BorderLayout.WEST);
        frame.add(btn3,BorderLayout.SOUTH);
        frame.add(btn4,BorderLayout.NORTH);
        frame.add(btn5,BorderLayout.CENTER);

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

在这里插入图片描述

表格布局(GridLayout)
public class TestGrid {
    public static void main(String[] args) {
        Frame frame = new Frame();
        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.setSize(200,200);
        //frame.pack(); //Java函数,根据里面的东西来自动填充框体大小(可有可无)
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

作业
public class TestGridLayout01 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLayout(new GridLayout(2,1));

        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new BorderLayout());
        Panel panel3 = new Panel(new GridLayout(2,1));
        Panel panel4 = new Panel(new GridLayout(2,2));

        frame.setSize(400,400);
        panel1.setSize(400,200);
        panel2.setSize(400,200);

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

        frame.add(panel1);
        frame.add(panel2);


        panel1.add(new Button("btn1"),BorderLayout.WEST);
        panel1.add(new Button("btn4"),BorderLayout.EAST);
        panel1.add(panel3,BorderLayout.CENTER);

        panel3.add(new Button("btn2"));
        panel3.add(new Button("btn3"));

        panel2.add(new Button("btn5"),BorderLayout.WEST);
        panel2.add(new Button("btn10"),BorderLayout.EAST);
        panel2.add(panel4,BorderLayout.CENTER);

        for (int i = 6; i < 10; i++) {
            panel4.add(new Button("btn"+i));
        }
    }
}

总结
  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器当中
  3. 布局管理器
    1. 流式布局
    2. 东西南北中局部
    3. 表格布局
  4. 大小、定位、背景、颜色、可见性、监听

事件监听

public class TestAction {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn = new Button("btn");

        //点击按钮触发的事件
        MyActionListener myActionListener = new MyActionListener();
        btn.addActionListener(myActionListener);

        frame.add(btn);
        frame.pack();
        frame.setVisible(true);
        WindowClosing.windowClose(frame);
    }
}

//事件监听,当点击按钮的时候输出:OK
class MyActionListener implements ActionListener{

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

在这里插入图片描述

多个按钮共享一个事件
public class TestAction01 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn1 = new Button("start");
        Button btn2 = new Button("stop");
        frame.add(btn1,BorderLayout.NORTH);
        frame.add(btn2,BorderLayout.SOUTH);

        btn1.setActionCommand("start");
        btn2.setActionCommand("stop");

        MyActionEvent myActionEvent = new MyActionEvent();
        btn1.addActionListener(myActionEvent);
        btn2.addActionListener(myActionEvent);

        frame.pack();
        frame.setVisible(true);
        WindowClosing.windowClose(frame);
    }
}
class MyActionEvent implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("start")){
            System.out.println("您点击了按钮=>"+e.getActionCommand());
        }
        else
            System.out.println("您没有点击start按钮");
    }
}

在这里插入图片描述

输入框(TextField)

public class TestTextField {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame{
    public MyFrame(){
        //new一个文本框对象
        TextField textField = new TextField();
        add(textField);
        //设置文本框的监听事件
        MyActionListener01 myActionListener01 = new MyActionListener01();
        //按下enter时会触发监听事件
        textField.addActionListener(myActionListener01);
        //设置替换编码为 *
        textField.setEchoChar('*');
        pack();
        setVisible(true);
    }
}

class MyActionListener01 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();//使textField对象获取到文本框内的内容
        System.out.println(textField.getText());//后台输出内容
        textField.setText("");//按下回车后设置文本内容为空
    }
}
实现简易的计算功能
public class Clam {
    public static void main(String[] args) {
        new MyClam().loadFrame();
    }
}

class MyClam extends Frame{

    //属性
    TextField text1,text2,text3;
    Label label;
    Button btn;
    //方法
    public void loadFrame(){
        //设置组件
        text1 = new TextField(10);
        text2 = new TextField(10);
        text3 = new TextField(20);
        label = new Label("+");
        btn = new Button("=");

        btn.addActionListener(new MyActionListener());

        //设置布局
        setLayout(new FlowLayout());
        add(text1);
        add(label);
        add(text2);
        add(btn);
        add(text3);

        pack();
        setVisible(true);
        windowClose();
    }
    //监听器类
    private class MyActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(text1.getText());
            int n2 = Integer.parseInt(text2.getText());
            text3.setText(String.valueOf(n1+n2));
        }
    }
    //
    public void windowClose(){
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

画笔

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().LoadFrame();
    }
}
class MyPaint extends Frame{
    public void LoadFrame(){
        setVisible(true);
        setBounds(200,200,500,500);
        windowClosing();
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);//画笔的颜色设置
        g.fillOval(100,100,100,100);//实心圆

        g.setColor(Color.blue);
        g.drawOval(100,200,100,100);//空心圆
    }

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

}

在这里插入图片描述

鼠标监听

public class TestMouse {

    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
class MyFrame extends Frame {
    ArrayList points;

    public MyFrame(String title) {
        super(title);
        setBounds(100,100,500,500);

        points = new ArrayList<>();
        this.addMouseListener(new MyMouseAction());//鼠标监听器
        setVisible(true);
        WindowClosing();
    }

    //画画,监听鼠标事件
    @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,5,5);
        }
    }

    //添加一个点到页面上
    public void addPoint(Point point){
        points.add(point);
    }

    //适配器模式,监听鼠标动作
    private class MyMouseAction extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame frame = (MyFrame)e.getSource();
            frame.addPoint(new Point(e.getX(),e.getY()));//画出来获取到鼠标点击的那个点

            frame.repaint();//每次鼠标点击都进行画板刷新
        }
    }

    //关闭窗口
    public void WindowClosing(){
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

窗口监听

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground(Color.blue);
        setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            //关闭窗口
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            //激活窗口
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame windowFrame= (WindowFrame) e.getSource();
                windowFrame.setTitle("不要离开我哦");
            }

        });
    }
}

在这里插入图片描述

键盘监听

public class TestKey {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        Label label = new Label();
        setBounds(100,100,200,200);
        setVisible(true);
        add(label);
        //设置窗口监听事件,实现关闭窗口
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //设置键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获取当前按下的按键对应的码
                int keycode = e.getKeyCode();
                //判断
                if(keycode == KeyEvent.VK_UP)
                label.setText("You used the up!");
            }
        });
    }
}

在这里插入图片描述

Swing


窗口(JFrame)

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

    //init初始化
    public void init(){
        JFrame jFrame = new JFrame("这是一个JFrame!");
        JLabel jLabel = new JLabel("欢迎来到小梁的自学Java世界",0);//SwingConstants.CENTER 等同于 0
        jFrame.add(jLabel);
        jFrame.setBounds(100,100,200,200);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.getContentPane().setBackground(Color.red);
    }
}

弹窗

public class DiologDemo extends JFrame{
    public static void main(String[] args) {
        new DiologDemo();
    }

    public DiologDemo(){
        JButton jButton = new JButton("点击弹出一个对话框");
        jButton.setBounds(100,100,200,50);
        setTitle("弹窗");
        setBounds(100,100,400,400);
        setVisible(true);
        //设置窗体可关闭
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置一个容器
        Container container = this.getContentPane();
        //设置绝对布局
        container.setLayout(null);
        //添加按钮至容器中
        container.add(jButton);
        //添加点击按钮的事件
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {//监听器
                new MyDialogDemo();
            }
        });
    }
}

//弹窗窗口
class MyDialogDemo extends JFrame{
    public MyDialogDemo(){
        setTitle("小窗体");
        setVisible(true);
        setBounds(150,150,200,100);
        add(new JLabel("欢迎来到小梁的弹窗界面",0));
    }
}

在这里插入图片描述

标签(JLabel)

JLabel

new JLabel("文本");

面板(JPanel)

public class TestJPanel extends JFrame {
    public static void main(String[] args) {
        new TestJPanel();
    }
    public TestJPanel() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));//后面两个参数为上下间距

        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPane2 = new JPanel(new GridLayout(2,3));
        JPanel jPane3 = new JPanel(new GridLayout(3,1));
        JPanel jPane4 = new JPanel(new GridLayout(2,2));
        add(jPanel);
        add(jPane2);
        add(jPane3);
        add(jPane4);

        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"));
        jPane2.add(new JButton("2"));
        jPane2.add(new JButton("2"));
        jPane2.add(new JButton("2"));
        jPane2.add(new JButton("2"));
        jPane3.add(new JButton("3"));
        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"));

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

在这里插入图片描述

JScrollPanel
public class TestScrollPanel extends JFrame {
    public static void main(String[] args) {
        new TestScrollPanel();
    }

    public TestScrollPanel() {
        Container container = this.getContentPane();//实例化容器

        JTextArea jTextArea = new JTextArea();//设置的文本域
        JScrollPane jScrollPane = new JScrollPane(jTextArea);//设置滚动窗格并且将文本域放进去
        container.add(jScrollPane);//将窗格放置容器内

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

在这里插入图片描述

按钮(JButton)

public class TestImageButton extends JFrame {
    public static void main(String[] args) {
        new TestImageButton();
    }

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

        JButton jButton = new JButton();
        //将图片变为图标
        URL resource = TestImageButton.class.getResource("text.png");
        Icon icon = new ImageIcon(resource);

        jButton.setIcon(icon);
        jButton.setToolTipText("这是一个图片按钮");
        container.add(jButton);

        setBounds(200,200,400,400);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
单选按钮(JRadioButton)
public class TestJButton extends JFrame {
    public static void main(String[] args) {
        new TestJButton();
    }

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

        //单选框
        JRadioButton radioButton01 = new JRadioButton("btn1");
        JRadioButton radioButton02 = new JRadioButton("btn2");
        JRadioButton radioButton03 = new JRadioButton("btn3");
        //将按钮添加到组内
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton01);
        group.add(radioButton02);
        group.add(radioButton03);
        container.add(radioButton01,BorderLayout.NORTH);
        container.add(radioButton02,BorderLayout.CENTER);
        container.add(radioButton03,BorderLayout.SOUTH);
        
        setVisible(true);
        setBounds(100,100,400,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

在这里插入图片描述

多选按钮(JCheckBox)
public class TestJButton extends JFrame {
    public static void main(String[] args) {
        new TestJButton();
    }

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

        //多选框
        JCheckBox checkBox1 = new JCheckBox("checkbox1");
        JCheckBox checkBox2 = new JCheckBox("checkbox2");
        JCheckBox checkBox3 = new JCheckBox("checkbox3");
        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.CENTER);
        container.add(checkBox3,BorderLayout.SOUTH);

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

在这里插入图片描述

下拉框(jComboBox)

public class TestList extends JFrame {
    public static void main(String[] args) {
        new TestList();
    }

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

        Panel panel = new Panel();
        JComboBox jComboBox = new JComboBox();
        panel.setSize(100,50);
        panel.add(jComboBox);
        container.add(panel);

        jComboBox.addItem("正在热映");
        jComboBox.addItem("已下架");
        jComboBox.addItem("即将上映");

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

在这里插入图片描述

应用场景

选择地区或者一些单个选项

列表框(JList)

public class TestJList extends JFrame {
    public static void main(String[] args) {
        new TestJList();
    }

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

        //创建一个适量
        Vector vector = new Vector();
        //创建一个列表
        JList jList = new JList(vector);
        //添加内容
        vector.add("张三");
        vector.add("李四");
        vector.add("王五");
        //将列表添加到容器当中
        container.add(jList);

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

在这里插入图片描述

应用场景

展示信息,一般是动态扩容

文本框(JTextField)

public class TestJText extends JFrame {
    public static void main(String[] args) {
        new TestJText();
    }

    public TestJText() {
        Container container = this.getContentPane();
        //设置绝对布局
        container.setLayout(null);

        //文本框
        JTextField jTextField = new JTextField("这是一个文本框",20);
        jTextField.setBounds(10,10,150,50);
        container.add(jTextField);

        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');
        jPasswordField.setBounds(10,70,150,50);
        container.add(jPasswordField);

        //文本域
        JTextArea jTextArea = new JTextArea("这是一个文本域");
        jTextArea.setBounds(10,130,150,100);
        container.add(jTextArea);

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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值