简体   繁体   English

为什么 JRadioButton 没有显示在 JPanel 上?

[英]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.我是 Java 图形设计的新手,想实现一个单选按钮菜单,但是单选按钮不可见,并且仍然以某种方式覆盖了它下面的图形。

I thought that it was not being displayed due to setVisible being set to false, but even that did not work.我认为由于 setVisible 被设置为 false 而没有显示它,但即使这样也不起作用。

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);尝试在行add(rb);之前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.第一个是您扩展 JPanel 并实现了 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.您实际上并没有向 JPanel 添加任何新功能,您只是设置了一个面板,所以我不会这样做。 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.最后,每次创建 Panel 时,它实际上都会创建并打开一个 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.我建议查找 SOLID、 ITNEXT 的解释、Stackify 的解释,因为它会帮助您理解为什么不应该这样做。

I also noticed you are using a null layout on your panel.我还注意到您在面板上使用了null布局。 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.因此,查看布局管理器, GridBagLayout为您提供了一种设置 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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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