从JRadioButton获取价值

时间:2017-04-30 17:50:21

标签: java jradiobutton

我想更改变量的值" name"当我选择右键并点击" ok"在我的JRadio Frame上。

例如,当我选择r1并点击" ok"我想命名==" Fast"在整个包装中。

package Snake;
public class Radio extends JFrame {
    private int delay = 100;
    private String name;
    JTextField t1;
    JButton b;
    JRadioButton r1, r2;
    JLabel l;

    public void selectSpeed() {

        b = new JButton("Ok");
        r1 = new JRadioButton("Fast");
        r2 = new JRadioButton("Slow");
        l = new JLabel("Speed: ");

        ButtonGroup bg = new ButtonGroup();
        bg.add(r1);
        bg.add(r2);
        add(b);
        add(r1);
        add(r2);
        add(l);

        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                if (r1.isSelected()) {
                    name = "Fast";
                } else {
                    name = "Slow";
                }
                l.setText("Speed: " + name); // name=="Fast" when r1 is selected
            } // name=="Slow" when r2 is selected
        });
        if (name == "Fast") { // and now name is empty...
            delay = 50;

        }
        if (name == "Slow") {
            delay = 500;
        }

        setLayout(new FlowLayout());
        setSize(400, 400);
        setVisible(true);
    }

    public int setSpeed() {
        selectSpeed();
        return delay;

    }

}

3 个答案:

答案 0 :(得分:0)

您需要在JRadioButton侦听器中执行此操作。例如,就像这里一样,首先你改变变量“name”,然后在当前的监听器中你检查条件,但是你需要记住要比较你需要使用的字符串“equals”:

    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (r1.isSelected()) {
                name = "Fast";
            } else {
                name = "Slow";
            }
            l.setText("Speed: " + name); // name=="Fast" when r1 is selected

            if (name.equals("Fast")) { // and now name is empty...
                delay = 50;

            }
            if (name.equals("Slow")) {
                delay = 500;
            }
        } // name=="Slow" when r2 is selected
    });

答案 1 :(得分:0)

如果您要更改delay上的button click,您需要在ActionListener本身编写逻辑,因为您编写的用于更改delay的代码将只运行一次,并且在程序执行开始时也是如此,那时name将为空。

然后,当您点击按钮时,它只会执行ActionListener因此delay将不会随时更改。你正在犯的其他错误就是你正在以错误的方式比较字符串。有关详细信息,请查看How do I compare Strings in Java?

要在按钮点击时动态更改延迟,您需要在ActionListener

中进行更改
 b.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

        if (r1.isSelected()) {
            name = "Fast";
            delay = 50;
        } else {
            name = "Slow";
            delay = 500;
        }
        l.setText("Speed: " + name); // name=="Fast" when r1 is selected
    } // name=="Slow" when r2 is selected
});

答案 2 :(得分:0)

我现在看到了我的错误,谢谢你。

但它仍然无法按照我喜欢的方式运作。每当我在JRadio上选择右键并点击“确定”时我想改变“延迟”值,并且这个改变的值我想去另一个班级。

有一个类的代码,我需要“延迟”的值:

package Snake;
public class Gameplay extends Paint implements KeyListener, ActionListener {
private Timer timer;
private int q = 0;
Radio radio = new Radio();


public Gameplay() {

    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    timer = new Timer(radio.selectSpeed(), this); //here i need flexible "delay" value
    timer.start();
}