Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java swing图标中断应用程序_Java_Swing - Fatal编程技术网

Java swing图标中断应用程序

Java swing图标中断应用程序,java,swing,Java,Swing,我正在swing中制作一个自定义标题栏,当我使用字母“x”作为关闭按钮时,它工作正常。但当我用图标替换它时,整个标题栏就会消失,直到我将鼠标悬停在“关闭”按钮上,这只会使“关闭”按钮出现。以下是我的代码片段: // Create title bar JPanel titleBar = new JPanel(); titleBar.setBackground(new Color(0x343434)); titleBar.setSize(screenSize.width, 36); JButton

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

// 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);
    }
}
不要使用透明背景。使用透明度时,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。如果未指定约束,则零部件将添加到中心

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

//window.add(titleBar);
window.add(titleBar, BorderLayout.PAGE_START);
有关FlowLayout和BorderLayout的更多信息和工作示例,请阅读上的Swing教程


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

plese?。@PaulSamsotha我不知道我可以。整个过程需要65行代码,它所做的就是用这些代码创建一个窗口。65行没有那么长。您还可以通过只包含重现问题所需的代码来缩短它。这意味着只需添加导致问题的按钮和运行程序的其他代码。测试它,确保代码仍然重现问题。这就是最小可重复性问题。包括这些问题的问题会得到最好的帮助。如果您通过使代码可运行且可复制来帮助我们,这将节省志愿者的时间,并使我们希望help@PaulSamsotha“65行没那么长。”真的!一个少于100行代码的“准备运行”示例非常少。OP提示:例如,获取图像的一种方法是热链接到中看到的图像。例如,嵌入图像的热链接。@LysanderMealy整个过程需要65行,。这65行代码中的大部分都与您提出的问题无关。您的问题是关于在JFrame上显示关闭按钮。因此,对于这个应用程序,您需要一个JFrame、一个JPanel和一个JButton。ActionListener代码和MouseListener代码与所述问题无关。首先,您将学习如何正确显示按钮,然后您将担心如何将侦听器添加到按钮。这就是制作“MRE”的方法。
//window.add(titleBar);
window.add(titleBar, BorderLayout.PAGE_START);