1

我在设置JPanelJFrame颜色为白色时遇到问题,尽管我使用了panel.setBackground(Color.white). 第二个问题是构造函数中的设置导致它ImageIcon是不可见的。这是我的代码:JRadioButtonJRadioButton

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();
    }
}
4

2 回答 2

3

如果要将JFrame背景颜色设置为白色,则必须获取ContentPane并将其设置为白色:

frame1.getContentPane().setBackground(Color.white);

看看JFrame.setBackground() 不起作用 - 为什么?

至于ImageIcon问题,可能是因为您指定的路径没有图像文件。(在您的情况下,它就在项目文件夹中)。

编辑:既然我知道你想用 做什么,我在看到Andrew Thompson 的把戏ImageIcon后想出了这个

String imageText = "<html><img src=\""+this.getClass().getResource("green2.png")
            .toString()+"\"></img></html>";
JRadioButton zielony = new JRadioButton(imageText);

但是,它确实涉及您将图像放置在src文件夹中,而不是项目中。

于 2012-12-29T21:09:08.913 回答
3

只有没有JRadioButton' 洞的图像可见。

外观由外观和感觉相关的 UI 委托控制,它是ButtonUI. 如果没有编写自己的替换,您可以使用ColorIcon, 在这里看到,根据需要渲染按钮 - 带或不带孔。然后您可以使用此处setIcon()显示的更新图标。

Icon czerwonaikona = new ColorIcon(SIZE, Color.red);
JRadioButton czerwony = new JRadioButton("czerwony", czerwonaikona);
于 2012-12-29T21:40:23.077 回答