单击JRadioButton时,它将无法正确打印出来

时间:2017-06-06 23:44:30

标签: java

我正在尝试制作一个代码,当用户按下它时,它将打印出正确但不是打印出正确它只是什么都不做。我已经非常清楚地研究了这个主题,但如果有任何身体可以帮助我,我似乎无法找到正确的答案,我将非常感激

import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JOptionPane;

public class JRadioButtonTest 
{

  public static void main(String[] args) 
  {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JRadioButton Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JRadioButton button1 = new JRadioButton("0.4");
    JRadioButton button2 = new JRadioButton("12");
    JRadioButton button3 = new JRadioButton("37");

    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);

    frame.add(new JLabel("what is the density of a piece of rock that is 40 grams and 25cm3"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);

    if(button2.isSelected()){
      JOptionPane.showMessageDialog(null,"correct");
      } 
    }
  }

3 个答案:

答案 0 :(得分:1)

在代码中,您可以在创建单选按钮后立即检查它们的状态。因此,您必须在setVisible(true)电话和支票之间按下单选按钮,这实际上是不可能的。

您正在寻找的是听众,如果发生特定事件,这些将被执行。 例如ItemListener,它最适用于您的示例,因为每次单选按钮的值更改时都会执行它。

button2.addItemListener(new ItemListener()
{
 @Override
 public void itemStateChanged(ItemEvent e) {
   if (e.getStateChange() == ItemEvent.SELECTED) {
    // Your selected code here.
   }
   else if (e.getStateChange() == ItemEvent.DESELECTED) {
    // Your deselected code here.
   }
 }
}

快乐的编码!

答案 1 :(得分:0)

单击单选按钮时,会在运行时发生事件。最终主方法中的代码仅在加载帧之前执行一次。为了在每次单击时触发该功能,您需要注册一个监听器。所以,你的代码看起来像这样:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    import javax.swing.JOptionPane;

    public class JRadioButtonTest 
    {

      public static void main(String[] args) 
      {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("JRadioButton Test");
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JRadioButton button1 = new JRadioButton("0.4");
        JRadioButton button2 = new JRadioButton("12");
        JRadioButton button3 = new JRadioButton("37");

        ButtonGroup colorButtonGroup = new ButtonGroup();
        colorButtonGroup.add(button1);
        colorButtonGroup.add(button2);
        colorButtonGroup.add(button3);

        frame.add(new JLabel("what is the density of a piece of rock that is 40 grams and 25cm3"));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.pack();
        frame.setVisible(true);

        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"correct");

            }
        });
      }
    }

理想情况下,编写自己的ActionListener实现并将其注册到所需的所有元素会更好。您可以使用' e'捕获元素。对象

答案 2 :(得分:0)

只是提示让这更容易:

有一些Eclipse Neon软件包允许您实现一个视图,通过单击按钮可以启用诸如此类的功能,并允许您实时查看项目。

帮助>安装包

http://downlods.eclipse.org/releases/neon

仔细观察可能对您有用!