can't set JPanel color and JRadioButton invisibility

≡放荡痞女 提交于 2019-12-11 05:36:18

问题


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

回答1:


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);

Take a look at JFrame.setBackground() not working — why?

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).

Edit: 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);

However, it does involve you placing the images inside the src folder, not the project one.




回答2:


There is only image visible without JRadioButton's hole.

The appearance is controlled by the Look & Feel dependent UI delegate, a subclass of ButtonUI. Short of writing your own replacement, you can use ColorIcon, seen here, to render the button as you like—with or without the hole. You can then update the icon using setIcon(), shown here.

Icon czerwonaikona = new ColorIcon(SIZE, Color.red);
JRadioButton czerwony = new JRadioButton("czerwony", czerwonaikona);


来源:https://stackoverflow.com/questions/14085840/cant-set-jpanel-color-and-jradiobutton-invisibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!