gpt4 book ai didi

java - JLabel 也呈现在按钮上

转载 作者:行者123 更新时间:2023-12-02 11:17:43 24 4
gpt4 key购买 nike

我的 Swing 应用程序出现问题。如果我启动应用程序并将鼠标指针放在按钮所在的位置,上方的 JLabel 就会出现在按钮上。

这是一张显示我的问题的图片

/image/jcBoF.png

    setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

defFont = new Font("Impact", Font.PLAIN, 32);

JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BorderLayout());
mainpanel.setPreferredSize(new Dimension(480, 320));
mainpanel.setBackground(Color.white);

JLabel title = new JLabel("-----Title-----");
title.setFont(defFont);
title.setHorizontalAlignment(JLabel.CENTER);

JLabel footer = new JLabel("Footer");
footer.setFont(defFont.deriveFont(Font.TRUETYPE_FONT, 14f));

JPanel midPanel = new JPanel();
midPanel.setBackground(Color.GRAY);
midPanel.setLayout(new GridBagLayout());

JButton startGameButton = new JButton("New Game");
applySettings(startGameButton);

midPanel.add(startGameButton);

mainpanel.add(title, BorderLayout.NORTH);
mainpanel.add(midPanel, BorderLayout.CENTER);
mainpanel.add(footer, BorderLayout.PAGE_END);

add(mainpanel);
pack();
setVisible(true);

编辑:忘记粘贴 applySettings 方法。按钮上有一些样式,也许是功能,使其透明是问题所在。

我对代码进行了一些测试,当我尝试设置背景颜色时,它崩溃了。

private void applySettings(JButton button)
{
button.setPreferredSize(new Dimension(200, 100));
button.setFont(defFont.deriveFont(Font.TRUETYPE_FONT, 24f));
button.setBorder(BorderFactory.createLineBorder(Color.black, 2, true));

button.setFocusPainted(false);
button.setBackground(new Color(255, 255, 255, 100));
button.setContentAreaFilled(true);
}

最佳答案

找到this网上解决了,问题就解决了。

引用号:Backgrounds With Transparency

关于java - JLabel 也呈现在按钮上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com