将单选按钮值从一类传递到另一类

时间:2019-04-18 23:58:45

标签: java swing class object radio-button

我编写了一个使用控制台进行用户输入的类。我正在尝试围绕它创建一个GUI,以增强程序。结果,我创建了一个新的GUI类,希望将其传递回现有类。我已经花了数小时在论坛上进行搜索,但似乎找不到与我的具体问题相符的答案。

我找到的最接近的东西是pass radiobutton value that selected to another class ,实际上我已经使用了该课程的推荐。不过,它似乎“有时”起作用。我的意思是,当我第一次选择单选按钮“ one”时,什么也没有发生。然后,我单击第二个单选按钮,并且没有任何反应(按预期方式)。当我再次单击第一个单选按钮时,它将按预期方式将文本打印到控制台。我不知道为什么它在第一次点击时不起作用。其次,每当我单击seecond按钮并再次单击第一个按钮时,它将比预期时间多输出2倍的预期输出。

///单选按钮类

├── frontend
│   ├── Components
│   ├── index.html
│   ├── css
│   │   ├── style.css
│   │   ├── materialize.css
│   │   └── materialize.min.css
│   ├── README.md
│   ├── node_modules
│   ├── js
│   │   ├── init.js
│   │   ├── materialize.js
│   │   └── materialize.min.js
│   ├── src
│   │   └── word_card.js

/// ButtonTester类

package views;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

import common.ButtonTester;

public class RadioButtons extends JFrame {

    private JPanel contentPane;
    private JRadioButton rdbtnOne, rdbtnTwo;
    private ButtonGroup grp;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RadioButtons frame = new RadioButtons();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public RadioButtons() {
        setTitle("Button Demo");


        initComponents();
        createEvents();
    }


    **/// Components**
    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        rdbtnOne = new JRadioButton("One");
        rdbtnTwo = new JRadioButton("Two");

        grp = new ButtonGroup();
        grp.add(rdbtnOne);
        grp.add(rdbtnTwo);

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(126)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(rdbtnTwo)
                        .addComponent(rdbtnOne))
                    .addContainerGap(189, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(87)
                    .addComponent(rdbtnOne)
                    .addGap(48)
                    .addComponent(rdbtnTwo)
                    .addContainerGap(70, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);

    }

    **/// Event handlers**
    private void createEvents() {
        rdbtnOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rdbtnOne.addActionListener(new ButtonTester());
            }
        });
    }



}

我希望每次单击单选按钮一次,sysout行就会执行一次。

1 个答案:

答案 0 :(得分:1)

private void createEvents() {
    rdbtnOne.addActionListener(new ActionListener() {
        // ****** A *****
        public void actionPerformed(ActionEvent e) {
            // ***** B ****
            rdbtnOne.addActionListener(new ButtonTester());
        }
    });
}

(A)下面的行在ActionListener中添加了一个ActionListener,这实际上是没有意义的,并且是不必要的过度复杂化。这就是为什么第一次按下按钮时没有可见输出的原因,因为发生的一切是在幕后的(B)单选按钮上添加了另一个ActionListener。由于您不断向该按钮添加新的监听器,因此第二个监听器都将触发第二次,第三次按下时,将触发更多的监听器。

解决方案:简化-将单个侦听器添加到按钮,并将其添加一次且仅添加一次:

private void createEvents() {
    rdbtnOne.addActionListener(new ButtonTester());
}
相关问题