java - isSelected 不为 JRadioButton 返回 true 或 false

标签 java swing jradiobutton

我有一个程序,可以根据所选的单选按钮计算 2 个变量中的任意一个。出于某种原因,isSelected() 没有返回 true 或 false。我将在下面发布我的代码:

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

public class FutureValueFrame extends JFrame {
  public FutureValueFrame() {
    setTitle("Sample App");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public static void main(String[] args) {

    JFrame f = new FutureValueFrame();

    //GUI and BUTTONS
    JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment");
    JRadioButton loanAmountButton = new JRadioButton("Loan Amount");
    ButtonGroup selection = new ButtonGroup();
    selection.add(monthlyRadioButton);
    selection.add(loanAmountButton);

    JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));
    JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));
    JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##"));
    JFormattedTextField monthlyPaymentField = new JFormattedTextField(new DecimalFormat("####.##"));


    JPanel menuPanel = new JPanel();
    menuPanel.setLayout(new GridLayout(1,2));

    //ACTION LISTENER FOR RADIO BUTTONS
    monthlyRadioButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField));
    loanAmountButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField));

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridLayout(1,2));
    topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    topPanel.add(monthlyRadioButton);
    topPanel.add(loanAmountButton);

    JPanel botPanel = new JPanel();
    botPanel.setLayout(new GridLayout(4,2));

    botPanel.add(new JLabel("Loan Amount:"));
    botPanel.add(loanAmountField);

    botPanel.add(new JLabel("Yearly Interest Rate:"));
    botPanel.add(interestRateField);

    botPanel.add(new JLabel("Number of Years:"));
    botPanel.add(yearField);

    botPanel.add(new JLabel("Monthly Payment:"));
    botPanel.add(monthlyPaymentField);

    JPanel container = new JPanel();
    container.setLayout(new GridLayout(3,1));
    container.add(topPanel);
    container.add(botPanel);
    container.add(menuPanel);

    f.add(container);

    JButton calculateButton = new JButton("Calculate");

    if (monthlyRadioButton.isSelected()){
        calculateButton.addActionListener(new CalculateMonthlyListener(loanAmountField, interestRateField, yearField, monthlyPaymentField));
    }
    if (loanAmountButton.isSelected()){
        calculateButton.addActionListener(new CalculateLoanListener(loanAmountField, interestRateField, yearField, monthlyPaymentField));
    }

    JButton exitButton = new JButton("Exit");
    exitButton.addActionListener(new ExitListener());

    menuPanel.add(calculateButton);
    menuPanel.add(exitButton);     

    f.setVisible(true);
    f.setLocationRelativeTo(null);

    }

    class CalculateMonthlyListener implements ActionListener {

private JFormattedTextField loanAmountField;
private JFormattedTextField monthlyPaymentField;
private JFormattedTextField interestRateField;
private JFormattedTextField yearField; 
private float result;

 public CalculateMonthlyListener (JFormattedTextField loanAmountField, JFormattedTextField interestRateField, JFormattedTextField yearField, JFormattedTextField monthlyPaymentField)
{
  this.interestRateField = interestRateField;   
  this.yearField = yearField;
  this.loanAmountField = loanAmountField;
  this.monthlyPaymentField = monthlyPaymentField;
}

public void actionPerformed(ActionEvent event){

        monthlyPaymentField.setValue(new Double(12.22));
        System.out.println("You selected monthly");

}
}


class CalculateLoanListener implements ActionListener {

private JFormattedTextField loanAmountField;
private JFormattedTextField monthlyPaymentField;
private JFormattedTextField interestRateField; 
private JFormattedTextField yearField;
private float result;

public CalculateLoanListener (JFormattedTextField loanAmountField, JFormattedTextField interestRateField, JFormattedTextField yearField, JFormattedTextField monthlyPaymentField)
{
  this.interestRateField = interestRateField;   
  this.yearField = yearField;
  this.loanAmountField = loanAmountField;
  this.monthlyPaymentField = monthlyPaymentField;
}

public void actionPerformed(ActionEvent event){

   loanAmountField.setValue(new Double(12.22));

   System.out.println("You selected loan");



}
}

class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent event){
    //f.dispose();
    System.exit(0);
    //System.out.println("You clicked exit");
}
}

class SelectionListener implements ActionListener {

private JRadioButton monthlyRadioButton;
private JRadioButton loanAmountButton;
private JFormattedTextField loanAmountField;
private JFormattedTextField monthlyPaymentField;


public SelectionListener (JRadioButton monthlyRadioButton, JRadioButton loanAmountButton, JFormattedTextField loanAmountField, JFormattedTextField monthlyPaymentField)
{
    this.monthlyRadioButton = monthlyRadioButton;   
    this.loanAmountButton = loanAmountButton;
    this.loanAmountField = loanAmountField;
    this.monthlyPaymentField = monthlyPaymentField;

}

public void actionPerformed(ActionEvent event){
      if(event.getSource() == monthlyRadioButton){
        loanAmountField.setEditable(false);
        monthlyPaymentField.setEditable(true);
      }
      else {
        monthlyPaymentField.setEditable(false);
        loanAmountField.setEditable(true);

      }
}
}



}

我认为问题出在这段代码中:

    if (monthlyRadioButton.isSelected()){
        calculateButton.addActionListener(new CalculateMonthlyListener(loanAmountField, interestRateField, yearField, monthlyPaymentField));
    }
    if (loanAmountButton.isSelected()){
        calculateButton.addActionListener(new CalculateLoanListener(loanAmountField, interestRateField, yearField, monthlyPaymentField));
    }

isSelected 未返回 true。我已经尝试创建一个 int i 并将其设置为 1。我在每个条件下检查了 i==1 并且它们正确执行。

有什么见解吗?

最佳答案

您应该将选择检查代码放在附加到按钮的单个 Action 监听器中,而不是根据选择决定将哪个 Action 监听器附加到按钮。

将您认为是问题根源的代码替换为:

calculateButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (selection.getSelection().equals(monthlyRadioButton.getModel())) {
             monthlyPaymentField.setValue(new Double(12.22));
            System.out.println("You selected monthly");
        } else {
            loanAmountField.setValue(new Double(12.22));
            System.out.println("You selected loan");
        }
    }
});

为了编译它,您必须使在上述 Action 监听器中访问的变量成为最终变量。或者将 CalculateMonthlyListener 和 CalculateLoanListener 替换为一个类,其功能与上面所示相同。

请注意,我没有直接使用 JRadioButton.isSelected(),而是使用了您的 ButtonGroup.getSelection()。您也可以检查单选按钮本身。

关于java - isSelected 不为 JRadioButton 返回 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11151152/

相关文章:

java - Swing:调整 RadioButton 的大小

java - 在java上分层绘画?

java - 我如何从一行进行变量更新

java - 禁用自动滚动

java - 如何获取JRadioButton的ButtonGroup

java - 获取从java中的单选按钮中选择的值

java - 随时查找执行地点

java - Spring Hibernate 多对多关联与 transient 值

java - 如何以 Java 8 方式获取特定 URL 上的文本行流?

java - Intellij无法访问