我是一个新的堆栈用户并且是 Java 的新手,所以如果这无论如何不清楚或使用了错误的术语,请提前道歉。
如果没有对 JRadioButton 进行选择,我希望分配一个基值。
我可以将 JRadioButton 设置为 true,它将自动选择但不会出现在侦听器中,因为没有事件?
我可以设置一个 else 语句,但是由于它等待侦听器事件而没有再次发生?
以下代码似乎适用于我首先将值设置为双精度值。
我的问题是,虽然我得到了分配给变量 $leadtime 的正确值,但系统输出返回它的值 4 次,有人可以解释一下这个事件吗?
或者,如果有更好的方法来做到这一点,我会非常乐意学习,或者我在两个要点尝试中忽略了一些东西?
谢谢,周末战士84
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;
public class CRADIOHAND implements ActionListener{
JRadioButton $test1;
JRadioButton $test2;
JRadioButton $test3;
JRadioButton $test4;
double $leadtime = 22;
CRADIOHAND(JRadioButton $buttonJR1,JRadioButton $buttonJR2,JRadioButton $buttonJR3,JRadioButton $buttonJR4){
$test1 = $buttonJR1;
$test2 = $buttonJR2;
$test3 = $buttonJR3;
$test4 = $buttonJR4;
System.out.println($leadtime);
}
public void actionPerformed(ActionEvent re1) {
if($test1.isSelected()){
$leadtime= 22;
}else if($test2.isSelected()){
$leadtime= 47;
}else if($test3.isSelected()){
$leadtime= 113;
}else if($test4.isSelected()){
$leadtime= 130;
}
System.out.println($leadtime);
}
}
缩短布局。
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class CLAYOUT extends JFrame {
private FlowLayout $lay;
public CLAYOUT(){
super("Sample Program");
$lay = new FlowLayout();
setLayout($lay);
JRadioButton $buttonJR1 = new JRadioButton("22 Days");
add($buttonJR1);
JRadioButton $buttonJR2 = new JRadioButton("47 Days");
add($buttonJR2);
JRadioButton $buttonJR3 = new JRadioButton("113 Days");
add($buttonJR3);
JRadioButton $buttonJR4 = new JRadioButton("130 Days");
add($buttonJR4);
ButtonGroup radiogroup = new ButtonGroup();
radiogroup.add($buttonJR1);
radiogroup.add($buttonJR2);
radiogroup.add($buttonJR3);
radiogroup.add($buttonJR4);
$buttonJR1.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
$buttonJR2.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
$buttonJR3.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
$buttonJR4.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
}
}
缩短的主要
import javax.swing.JFrame;
public class CREORDER{
public static void main (String[] args){
CLAYOUT $clay = new CLAYOUT();
$clay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
$clay.setSize(1200,500);
$clay.setVisible(true);
}
}