JAVA_SE实现桌面化功能

实现弹出窗口,点击按钮对图片进行隐藏

与传统javaee不同的是导入的工具类不同,对于写入的逻辑需采用相应的参数类进行设置,采用监听实现功能。

实现逻辑(直接上代码)

实现时将 ImageIcon类的图片路径改为本地图片路径即可。


public class TestGUI {
    
    
    public static void main(String[] args) {
    
    
        JFrame f = new JFrame("LoL");
        f.setSize(400, 300);
        f.setLocation(580, 200);
        f.setLayout(null);

        final JLabel l = new JLabel();

        ImageIcon i = new ImageIcon("D:\\桌面背景\\722353e851c95d7ae608bd7ce5c75efc.jpg");
        l.setIcon(i);
        l.setBounds(1, 1, i.getIconWidth(), i.getIconHeight());

        JButton c= new JButton("显示图片");
        c.setBounds(150, 200, 100, 30);

        JButton b = new JButton("隐藏图片");
        b.setBounds(50, 200, 100, 30);

        // 给按钮 增加 监听
        b.addActionListener(new ActionListener() {
    
    

            // 当按钮被点击时,就会触发 ActionEvent事件
            // actionPerformed 方法就会被执行
            public void actionPerformed(ActionEvent e) {
    
    
                l.setVisible(false);
            }
        });




        // 给按钮 增加 监听
        c.addActionListener(new ActionListener() {
    
    

            // 当按钮被点击时,就会触发 ActionEvent事件
            // actionPerformed 方法就会被执行
            public void actionPerformed(ActionEvent e) {
    
    
                l.setVisible(true);
            }
        });

        f.add(l);
        f.add(b);
        f.add(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setVisible(true);
    }
}

效果图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zhx__/article/details/119479471
今日推荐