• 【GUI】-- 07 Icon & ImageIcon、JPanel & JScroll


    GUI编程

    03 Swing

    3.3 Icon

    1. 自定义Label的图标
    package com.duo.lesson04;
    
    import javax.swing.*;
    import java.awt.*;
    
    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 iconDemo1 = new IconDemo(20, 20);
            //图标可以放在标签,按钮上
            JLabel jLabel = new JLabel("icon_1", iconDemo1, SwingConstants.CENTER);
            Container contentPane = getContentPane();
            contentPane.add(jLabel);
    
            setTitle("图标测试窗口");
            setSize(650, 500);
            setLocationRelativeTo(null);
            setVisible(true);
            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 width;
        }
    
        @Override
        public int getIconHeight() {
            return height;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    运行结果:

    图1

    1. 以图片作为Label的Icon:
    package com.duo.lesson04;
    
    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    public class ImageIconDemo extends JFrame{
    
        public ImageIconDemo() {
            //获取图片地址
            URL url = ImageIconDemo.class.getResource("通用头像.jpg");
    
            JLabel jLabel = new JLabel("ImageIcon");
    
            assert url != null;
            ImageIcon imageIcon = new ImageIcon(url);  //注意类名不要与内置类ImageIcon重名
            jLabel.setIcon(imageIcon);
            jLabel.setHorizontalAlignment(SwingConstants.CENTER);
    
            Container contentPane = getContentPane();
            contentPane.add(jLabel);
    
            setTitle("图片图标测试窗口");
            setSize(650, 500);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new ImageIconDemo();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    运行结果:

    图2

    3.4 JPanel

    1. JFrame下的面板JPanel:
    package com.duo.lesson05;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class JPanelDemo extends JFrame {
        public JPanelDemo() {
            Container contentPane = getContentPane();
            contentPane.setLayout(new GridLayout(2, 1, 10, 10));  //hgap和vgap代表面板之间的间距
    
            JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
            jPanel1.add(new JButton("One"));
            jPanel1.add(new JButton("One"));
            jPanel1.add(new JButton("One"));
    
            JPanel jPanel2 = new JPanel(new GridLayout(2, 1));
            jPanel2.add(new JButton("Two"));
            jPanel2.add(new JButton("Two"));
    
            JPanel jPanel3 = new JPanel(new GridLayout(3, 2));
            jPanel3.add(new JButton("Three"));
            jPanel3.add(new JButton("Three"));
            jPanel3.add(new JButton("Three"));
            jPanel3.add(new JButton("Three"));
            jPanel3.add(new JButton("Three"));
            jPanel3.add(new JButton("Three"));
    
            JPanel jPanel4 = new JPanel(new GridLayout(1, 2));
            jPanel4.add(new JButton("Four"));
            jPanel4.add(new JButton("Four"));
    
            contentPane.add(jPanel1);
            contentPane.add(jPanel2);
            contentPane.add(jPanel3);
            contentPane.add(jPanel4);
    
            setTitle("面板测试窗口");
            setSize(650, 500);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new JPanelDemo();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    运行结果:

    图3

    1. JScroll

    JScroll是带有滚轮可上下左右滑动的面板。

    package com.duo.lesson05;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class JScrollDemo extends JFrame {
        public JScrollDemo() {
            Container contentPane = getContentPane();
    
            JTextArea jTextArea = new JTextArea("你好", 40, 100);
            //jTextArea.setText("你好");
            jTextArea.setFont(new Font("宋体", Font.PLAIN, 20));
    
            //Scroll
            JScrollPane jScrollPane = new JScrollPane(jTextArea);
            contentPane.add(jScrollPane);
    
            setVisible(true);
            setSize(650, 500);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new JScrollDemo();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    运行结果:

    图4


  • 相关阅读:
    前馈神经网络(FFNN)和多层感知机(MLP)
    C专家编程 第11章 你懂得C,所以C++不再话下 11.8 继承---复用已经定义的操作
    MobileViT——论文简述
    Python高级技巧
    salesforce如何以admin的角色执行apex
    Paddle安装
    1.2 变量和数据类型(Python)
    Python异步编程小解一
    rabbitmq 延时队列和死信队列的分析
    PTA 睡前速刷(C++ & Java)
  • 原文地址:https://blog.csdn.net/qq_51916086/article/details/134386874