簡體   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