无法设置 JPanel 颜色和 JRadioButton 不可见性 [英] can't set JPanel color and JRadioButton invisibility
问题描述
我在将 JPanel
和 JFrame
颜色设置为白色时遇到问题,尽管我使用了 panel.setBackground(Color.white)
.第二个问题是在JRadioButton
构造函数中设置ImageIcon
会导致JRadioButton
不可见.这是我的代码:
I have problem with setting JPanel
and JFrame
color to white, though I used panel.setBackground(Color.white)
. The second problem is that setting ImageIcon
in JRadioButton
constructor causes that JRadioButton
is invisible. Here is my code:
public class proby {
static JPanel panel = new JPanel();
static JPanel panel2 = new JPanel();
private void createAndShowGUI() {
final ImageIcon zielonaikona = new ImageIcon("green2.png");
final ImageIcon czerwonaikona = new ImageIcon("red2.png");
final ImageIcon niebieskaikona = new ImageIcon("blue.png");
final ImageIcon szaraikona = new ImageIcon("grey.png");
JFrame frame1 = new JFrame("MasterMind");
final JRadioButton zielony = new JRadioButton(zielonaikona);
zielony.setBackground(Color.WHITE);
final JRadioButton czerwony = new JRadioButton("czerwony");
czerwony.setBackground(Color.white);
final JRadioButton niebieski = new JRadioButton("niebieski");
niebieski.setBackground(Color.white);
final JRadioButton szary = new JRadioButton("szary");
szary.setBackground(Color.white);
zielony.setSelected(true);
ButtonGroup gruparadio = new ButtonGroup();
gruparadio.add(zielony);
gruparadio.add(czerwony);
gruparadio.add(niebieski);
gruparadio.add(szary);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton akceptuj = new JButton("Akceptuj");
akceptuj.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel label2;
if (zielony.isSelected()) {
label2 = new JLabel(zielonaikona);
} else if (czerwony.isSelected()) {
label2 = new JLabel(czerwonaikona);
} else if (szary.isSelected()) {
label2 = new JLabel(szaraikona);
} else {
label2 = new JLabel(niebieskaikona);
}
panel2.add(label2);
panel2.revalidate();
}
});
BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel.setLayout(layout);
panel2.setLayout(layout2);
panel.add(zielony);
panel.add(czerwony);
panel.add(niebieski);
panel.add(szary);
panel.add(akceptuj);
panel.setBackground(Color.WHITE);
panel2.setBackground(Color.white);
frame1.getContentPane().add(panel);
frame1.getContentPane().add(panel2);
BoxLayout layout3 = new BoxLayout(frame1.getContentPane(), BoxLayout.Y_AXIS);
frame1.setLayout(layout3);
frame1.setBackground(Color.white);
frame1.setSize(300, 300);
frame1.setVisible(true);
}
public static void main(String[] args) {
proby kk = new proby();
kk.createAndShowGUI();
}
}
推荐答案
如果要将 JFrame
背景色设置为白色,则必须获取 ContentPane
和将其设置为白色:
If you want to set your JFrame
background color to white, you have to get the ContentPane
and set that to white:
frame1.getContentPane().setBackground(Color.white);
看看JFrame.setBackground() 不起作用——为什么?
至于ImageIcon
问题,可能是因为您没有指定路径中的图像文件.(在您的情况下,它只是在项目文件夹中).
As for the ImageIcon
problem, it's probably because you don't have the image file at the path you indicated. (Which in your case is just inside the project folder).
现在我知道你想用 ImageIcon
做什么,我在看到 Andrew 后想到了这个汤普森的把戏
Now that I know what you're trying to do with the ImageIcon
, I came up with this after seeing Andrew Thompson's trick
String imageText = "<html><img src=\""+this.getClass().getResource("green2.png")
.toString()+"\"></img></html>";
JRadioButton zielony = new JRadioButton(imageText);
但是,它确实涉及将图像放置在 src
文件夹中,而不是项目文件夹中.
However, it does involve you placing the images inside the src
folder, not the project one.
这篇关于无法设置 JPanel 颜色和 JRadioButton 不可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!