简体   繁体   中英

Why is JRadioButton not being displayed on the JPanel?

I am new to Java Graphic Design and wanted to implement a Radio Button Menu, however the radio button is not visible and is still somehow covering up the graphics below it.

I thought that it was not being displayed due to setVisible being set to false, but even that did not work.

public class Panel extends JPanel implements ActionListener {
    JFrame frame;
    JRadioButton rb;

    Panel() {
        setLayout(null);
        addMouseListener(this);

        // Creating Frame
        frame = new JFrame();
        frame.setContentPane(this);
        frame.setTitle("Testing Stuff");
        frame.getContentPane().setPreferredSize(new Dimension(600, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // Creating the radio button
        rb = new JRadioButton();
        rb.setText("Button 1 Text");
        rb.setName("Button 1 Name");
        rb.setSelected(true);
        rb.setVisible(true);

        rb.setActionCommand("Button 1 Clicked");
        rb.addActionListener(this); 

// Adding the radio button
        add(rb);
    }
}

Where is the probelm in my code?

This happens because you have not set the size and position of you radio button on your panel.

Try adding this before the line add(rb);

rb.setBounds(10, 10, 100, 100);

I would like to suggest a few things about how you have laid your code out though.

The first is you extend JPanel and implemented Action Listener. You are not actually adding any new functionality to JPanel, you are just setting a panel up, so I would not do this. Similar Panel is not an action listener, you just want to use an action listener on one of your components in the panel. Finally, every time you make a Panel, it actually creates and opens a JFrame. This would be very hard to understand just by looking at the name "Panel". These are breaches of the Single Responsibility Principle and probably many others. I would recommend looking up SOLID, explanation from ITNEXT , explanation from Stackify , as it will help you understand why you should not do that.

I also noticed you are using a null layout on your panel. This is generally very bad. I recommend you read this question as it will help explain why null layouts are bad.

So looking at layout managers, GridBagLayout gives you a powerful way to set up a UI.

A short example might be

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class Example {
    private void run() {
        JFrame window = createWindow();
        window.setVisible(true);
    }

    private JFrame createWindow() {
        JFrame frame = new JFrame();
        frame.setTitle("Testing Stuff");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(createPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        return frame;
    }

    private JPanel createPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        constraints.weighty = 1;

        String birdString = "Bird";
        JRadioButton birdButton = new JRadioButton(birdString);
        birdButton.setMnemonic(KeyEvent.VK_B);
        birdButton.setActionCommand(birdString);
        birdButton.setSelected(true);
        constraints.gridy = 0;
        panel.add(birdButton, constraints);

        String catString = "Cat";
        JRadioButton catButton = new JRadioButton(catString);
        catButton.setMnemonic(KeyEvent.VK_C);
        catButton.setActionCommand(catString);
        constraints.gridy = 1;
        panel.add(catButton, constraints);

        String dogString = "Dog";
        JRadioButton dogButton = new JRadioButton(dogString);
        dogButton.setMnemonic(KeyEvent.VK_D);
        dogButton.setActionCommand(dogString);
        constraints.gridy = 2;
        panel.add(dogButton, constraints);

        String rabbitString = "Rabbit";
        JRadioButton rabbitButton = new JRadioButton(rabbitString);
        rabbitButton.setMnemonic(KeyEvent.VK_R);
        rabbitButton.setActionCommand(rabbitString);
        constraints.gridy = 3;
        panel.add(rabbitButton, constraints);

        String pigString = "Pig";
        JRadioButton pigButton = new JRadioButton(pigString);
        pigButton.setMnemonic(KeyEvent.VK_P);
        pigButton.setActionCommand(pigString);
        constraints.gridy = 4;
        panel.add(pigButton, constraints);

        //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(birdButton);
        group.add(catButton);
        group.add(dogButton);
        group.add(rabbitButton);
        group.add(pigButton);

        RadioActionListener listener = new RadioActionListener();

        //Register a listener for the radio buttons.
        birdButton.addActionListener(listener);
        catButton.addActionListener(listener);
        dogButton.addActionListener(listener);
        rabbitButton.addActionListener(listener);
        pigButton.addActionListener(listener);

        return panel;
    }

    private class RadioActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Example().run());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM