繁体   English   中英

Java swing 图标中断应用

[英]Java swing icon breaks application

我在 swing 中制作了一个自定义标题栏,当我使用字母“x”作为关闭按钮时,它工作正常。 但是当我用图标替换它时,整个标题栏消失了,直到我 hover 关闭按钮,这使得只出现关闭按钮。 这是我的代码片段:

// Create title bar
JPanel titleBar = new JPanel();
titleBar.setBackground(new Color(0x343434));
titleBar.setSize(screenSize.width, 36);

JButton closeButton = new JButton();
closeButton.setBackground(new Color(0, 0, 0, 0));
closeButton.setIcon(new ImageIcon(ImageIO.read(new File("src/close.png")).getScaledInstance(16, 16, Image.SCALE_SMOOTH)));
closeButton.setSize(50, 36);
closeButton.setLocation(screenSize.width - 50, 0);
closeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
    }
});
closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseEntered(MouseEvent e) {
        closeButton.setBackground(Color.RED);
    }

    public void mouseExited(MouseEvent e) {
         closeButton.setBackground(new Color(0x343434));
    }
});
closeButton.setBorder(null);
closeButton.setFocusPainted(false);
titleBar.add(closeButton);

window.add(titleBar);

编辑:它现在有时有效,但有时无效。 这是新代码:

public class Main {
    public static void main(String[] args) throws IOException {
        // Get screen size
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        // Create window
        JFrame window = new JFrame();

        //Get taskbar size
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration());
        int taskBarHeight = screenInsets.bottom;

        // Configure window
        window.setSize(screenSize.width, screenSize.height - taskBarHeight);
        window.getContentPane().setBackground(new Color(0x232323));
        window.setUndecorated(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);

        // Create title bar
        JPanel titleBar = new JPanel();
        titleBar.setBackground(new Color(0x343434));
        titleBar.setSize(screenSize.width, 36);

        JButton closeButton = new JButton();
        closeButton.setBackground(new Color(0x343434));
        Image closeImg = ImageIO.read(new File("src/close.png")).getScaledInstance(16, 16, 4);
        closeButton.setIcon(new ImageIcon(closeImg));
        closeButton.setSize(50, 36);
        closeButton.setLocation(screenSize.width - 50, 0);
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
            }
        });
        closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                closeButton.setBackground(Color.RED);
            }

            public void mouseExited(MouseEvent e) {
                closeButton.setBackground(new Color(0x343434));
            }
        });
        closeButton.setBorder(null);
        closeButton.setFocusPainted(false);
        titleBar.add(closeButton);

        window.add(titleBar);
    }
}
closeButton.setBackground(new Color(0, 0, 0, 0));

不要使用透明背景。 使用透明度时,Swing 无法正确绘制组件。

为了完全透明,您只需使用:

closeButton.setOpaque( false );

Swing 旨在与布局管理器一起使用:

closeButton.setSize(50, 36);
closeButton.setLocation(screenSize.width - 50, 0);

上面的代码什么都不做(所以摆脱它)。 JPanel 的默认布局管理器是 FlowLayout。 一旦框架可见,布局管理器将被调用,并且布局管理器将重置大小/位置。

所以让布局管理器完成它的工作。 在您的情况下,您可以使用:

//JPanel titleBar = new JPanel();
JPanel titleBar = new JPanel( new FlowLayout(FlowLayout.RIGHT) );

现在,您添加到面板的任何组件都将与右边缘对齐。

window.add(titleBar);

JFrame 的默认布局管理器是 BorderLayout。 当您不指定约束时,组件将添加到 CENTER。

对于标题栏,您希望将其添加到框架的顶部。 所以你应该使用:

//window.add(titleBar);
window.add(titleBar, BorderLayout.PAGE_START);

阅读有关布局管理器的 Swing 教程,了解有关 FlowLayout 和 BorderLayout 的更多信息和工作示例。

此外,在使框架可见之前,需要将 Swing 组件添加到框架中。 布局管理器最初在框架“打包”或“可见”时被调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM