单选按钮内的Java单选按钮

时间:2014-06-02 10:07:18

标签: java swing radio-button listener

我已经学会了使用java创建一个带单选按钮的菜单,但我想知道如何在单选按钮上创建单选按钮。真正的问题是我终于在'内创建了单选按钮。通过显示带有一组新按钮的新菜单的单选按钮,但每当我点击这些新的单选按钮时,都没有任何反应。好像按钮没有被收听。这是我的代码(例如,但仍然是相同的想法):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestRadioButtons extends JPanel implements ActionListener
{
    private String test = "Test Button 1";
    private String random = "RANDOM";

    private String test2 = "Button to Click";
    private String random2 = "RANDOM MK. 2"; 

    private static boolean menu = false;
    public TestRadioButtons()
    {
        super(new BorderLayout());

        //creates first set of radio buttons
        JRadioButton testButton = new JRadioButton(test);
        testButton.setMnemonic(KeyEvent.VK_T);
        testButton.setActionCommand(test);

        JRadioButton randomButton = new JRadioButton(random);
        randomButton.setMnemonic(KeyEvent.VK_R);
        randomButton.setActionCommand(random);

        //groups the first set of buttons
        ButtonGroup group1 = new ButtonGroup();
        group1.add(testButton);
        group1.add(randomButton);

        //register listener for first radio buttons
        testButton.addActionListener(this);
        randomButton.addActionListener(this);

        //put first radio buttons into a column in a panel
        JPanel radioPanel1 = new JPanel(new GridLayout(0, 1));
        radioPanel1.add(testButton);
        radioPanel1.add(randomButton);

        //set first menu border
        add(radioPanel1, BorderLayout.LINE_START);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,250));

        //I also have it so that if the a boolean value equals true, the following menu appears:
        if (menu == true)
        {
            JRadioButton test2Button = new JRadioButton(test2);
            test2Button.setMnemonic(KeyEvent.VK_A);
            test2Button.setActionCommand(test2);

            JRadioButton random2Button = new JRadioButton(random2);
            random2Button.setMnemonic(KeyEvent.VK_B);
            random2Button.setActionCommand(random2);

            ButtonGroup group2 = new ButtonGroup();
            group2.add(test2Button);
            group2.add(random2Button);

            test2Button.addActionListener(this);
            random2Button.addActionListener(this);

            JPanel radioPanel2 = new JPanel(new GridLayout(0, 1));
            radioPanel2.add(test2Button);
            radioPanel2.add(random2Button);

            add(radioPanel2, BorderLayout.LINE_START);
            setBorder(BorderFactory.createEmptyBorder(20,20,20,250));
        }
    }

    public static void menu2()
    {
        JFrame innerMenu = new JFrame();
        innerMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent innerComponent = new TestRadioButtons();
        innerComponent.setOpaque(true);
        innerMenu.setContentPane(innerComponent);

        innerMenu.pack();
        innerMenu.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (test.equals(e.getActionCommand()))
        {
            menu = true;
            menu2();
            if (test2.equals(e.getActionCommand()))
            {
                JOptionPane.showMessageDialog(null, "This is just a TEST!");
            }
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Radio Button Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent component = new TestRadioButtons();
        component.setOpaque(true);
        frame.setContentPane(component);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

如果有人能够帮助我(以及在我创建新菜单的部分修改我的代码),我将不胜感激。

1 个答案:

答案 0 :(得分:1)

只是一个}问题

像这样更改public void actionPerformed(ActionEvent e)

public void actionPerformed(ActionEvent e)
    {

        if (test.equals(e.getActionCommand()))
        {
            menu = true;
            menu2();
        }
            if (test2.equals(e.getActionCommand()))
            {
                JOptionPane.showMessageDialog(null, "This is just a TEST!");
            }

    }