有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我编写了一个类,它使用控制台进行用户输入。我试图围绕它创建一个GUI来增强程序。因此,我创建了一个新的GUI类,我想用它将值传递回现有的类。我花了几个小时在论坛上搜索,似乎找不到与我的具体问题相匹配的答案

我找到的最接近的东西是pass radiobutton value that selected to another class ,我实际上使用了那门课的推荐。不过,这似乎“有时”奏效。我的意思是,当我第一次选择单选按钮“一”时,什么都没有发生。然后我点击第二个单选按钮,什么也没发生(如预期的那样)。当我再次单击第一个单选按钮时,它会按预期将文本打印到控制台。我不明白为什么第一次点击就不起作用。其次,每次我点击seecond按钮并返回第一个按钮时,它都会打印出比之前多2倍的预期输出

///单选按钮类

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());
            }
        });
    }



}

///buttonester类

package common;

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


public class ButtonTester implements ActionListener {

    public static void main(String[] args) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Hello. I'm in the action Performed Method.");
    }

}

我希望每次单击单选按钮1时,sysout行都会执行一次


共 (1) 个答案

  1. # 1 楼答案

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

    下面(A)行在ActionListener中添加了一个ActionListener,这真的毫无意义,是一种不必要的过度复杂化。这就是为什么第一次按下按钮时,没有出现可见的输出,因为发生的只是在幕后,另一个ActionListener被添加到第(B)行的单选按钮。第二次两个侦听器都启动时,第三次按下时,会有更多侦听器启动,因为您一直在向按钮添加新的侦听器

    解决方案:简化向按钮添加一个侦听器,并只添加一次:

    private void createEvents() {
        rdbtnOne.addActionListener(new ButtonTester());
    }