【问题标题】:Conversion of JRadioButton to Boolean?将 JRadioButton 转换为布尔值?
【发布时间】:2015-02-20 20:48:55
【问题描述】:

我正在尝试让它根据是否按下单选按钮来使用 Java 图形并创建一个带有预设绘图的小程序。尽管我尝试使用 if 语句,但它告诉我无法将 JRadioButton 转换为布尔值,但我觉得这不是解决此问题的正确方法。我正在拉扯我的头发,因为我不明白如何做到这一点。

帮助/指导将不胜感激:D!谢谢 !

我的代码如下所示:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RadioButton extends JPanel {

    static JFrame frame;

   JLabel pic;
   RadioListener myListener = null;
   public RadioButton() {



       // Create the radio buttons and assign Keyboard shortcuts using Mnemonics
       JRadioButton displacement = new JRadioButton("Displacement");
       displacement.setMnemonic(KeyEvent.VK_N);
       displacement.setSelected(true);
        //Displacement Button, set to automatically be clicked

       JRadioButton accel = new JRadioButton("Acceleration");
       accel.setMnemonic(KeyEvent.VK_A);
       accel.setActionCommand("acceleration");
        //Acceleration Button

       JRadioButton time = new JRadioButton("Change in time");
       time.setMnemonic(KeyEvent.VK_S);
       time.setActionCommand("deltaT");
        //The change in time button

       // Creates the group of buttons
       ButtonGroup group = new ButtonGroup();
       group.add(displacement);
       group.add(accel);
       group.add(time);

              myListener = new RadioListener();
                displacement.addActionListener(myListener);
                accel.addActionListener(myListener);
                time.addActionListener(myListener);


      // Set up the picture label
       pic = new JLabel(new ImageIcon(""+"numbers" + ".jpg"));          //Set the Default Image

       pic.setPreferredSize(new Dimension(177, 122)); 


       // Puts the radio buttons down
       JPanel panel = new JPanel();
       panel.setLayout(new GridLayout(0, 1));
       panel.add(displacement);
       panel.add(accel);
       panel.add(time);


       setLayout(new BorderLayout());
       add(panel, BorderLayout.WEST);
       add(pic, BorderLayout.CENTER);
       setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
   } 
  if ( displacement.setSelected(true))
  {

  }


   //Listening to the buttons
   class RadioListener implements ActionListener { 
       public void actionPerformed(ActionEvent e) {
           pic.setIcon(new ImageIcon(""+e.getActionCommand() 
                                         + ".jpg"));
       }
   }

   public static void main(String s[]) {
        frame = new JFrame("∆x = Vavg * time");
        frame.addWindowListener(new WindowAdapter() {

【问题讨论】:

  • 我仍然需要创建一个运行小程序的类,但我需要让它改变它是否被按下

标签: java radio-button boolean project


【解决方案1】:

您将true 设置为isSelected()。但是,根据您的描述,您想检查它是否被选中。

用途:

if ( displacement.isSelected())
  {

  }

代替:

if ( displacement.setSelected(true))
  {

  }

【讨论】:

  • 快速问题:当我这样做时,我是在另一个班级中做的,它说无法解决可变位移。我扩展了另一个类,但是没有用,那么我如何得到它,以便它可以到达可变位移。
  • 老实说,我不知道该怎么做,所以如果有人可以帮助我,那就太棒了!
猜你喜欢
  • 1970-01-01
  • 2016-04-27
  • 2012-02-25
  • 1970-01-01
  • 2021-06-27
  • 2017-03-07
  • 2019-06-11
  • 2011-03-22
  • 1970-01-01
相关资源
最近更新 更多