gpt4 book ai didi

java - JTextField.getText() 返回空值,radiobuttons.isSelected() 总是返回 false

转载 作者:行者123 更新时间:2023-11-30 08:36:07 29 4
gpt4 key购买 nike

2 个不同的错误,我不知道是什么导致了它们中的任何一个。它们似乎都在 actionPerformed 方法中。这是 3 个不同的文件,很抱歉格式不正确,我永远无法让它正常工作。

package A8;

import javax.swing.*;

import java.awt.*;

public class a8main {

public static final int RES_X = 600;
public static final int RES_Y = 125;

public static JFrame window;
public static JPanel panel = new JPanel();
public static RatePanel ratePanel = new RatePanel();
public static MinutesPanel minutesPanel = new MinutesPanel();

public static void main(String[]args){
createWindow();
populateWindow();
}

public static void createWindow(){
window = new JFrame();
window.setTitle("A8 Long Distance Communications");
window.setSize(RES_X, RES_Y);
window.setEnabled(true);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void populateWindow(){
ratePanel.populatePanel(panel);
minutesPanel.populatePanel(panel);
window.add(panel);
}

}


package A8;

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

import javax.swing.*;

public class MinutesPanel implements ActionListener{

JTextField field = new JTextField();
JLabel minuteLabel = new JLabel();

int callTime;
double chosenRate;

public void populatePanel(JPanel Panel){
createPanelObjects();
Panel.add(minuteLabel);
Panel.add(field);
}

public void createPanelObjects(){
chosenRate = RatePanel.chosenRate;
field.setColumns(10);
minuteLabel = new JLabel("Enter length of call in minutes");
field.addActionListener(new MinutesPanel());
}
public void displayCallPrice(){
JOptionPane dialogBox = new JOptionPane();
double cost = RatePanel.chosenRate * callTime;
dialogBox.showMessageDialog(null, "The cost of your call is $" + cost);

}

@Override
public void actionPerformed(ActionEvent e) {
String callStr;
callStr = field.getText(); //problem is here.
//callStr = "3";
System.out.print(callStr);
callTime = Integer.parseInt(callStr);
System.out.print(callTime);
System.out.print(RatePanel.chosenRate);

displayCallPrice();

}

}


package A8;

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

import javax.swing.*;

public class RatePanel implements ActionListener{

public static final double DAY_RATE = 0.07;
public static final double EVENING_RATE = 0.12;
public static final double OFFPEAK_RATE = 0.05;
public static double chosenRate = DAY_RATE; //Because button defaults to day rate it is init to that rate.

JLabel categoryLabel = new JLabel();
JRadioButton dayButton = new JRadioButton();
JRadioButton eveningButton = new JRadioButton();
JRadioButton offpeakButton = new JRadioButton();
ButtonGroup buttons = new ButtonGroup();

public void populatePanel(JPanel Panel){
createPanelObjects();
Panel.add(categoryLabel);
Panel.add(dayButton);
Panel.add(eveningButton);
Panel.add(offpeakButton);
}

public void createPanelObjects(){
categoryLabel = new JLabel("Select a rate category");
dayButton = new JRadioButton("Day rate: $" + DAY_RATE);
eveningButton = new JRadioButton("Evening rate: $" + EVENING_RATE);
offpeakButton = new JRadioButton("Offpeak rate: $" + OFFPEAK_RATE);
dayButton.setSelected(true); //Used to make it impossible to not select a rate, prevents bugs.
buttons.add(dayButton);
buttons.add(eveningButton);
buttons.add(offpeakButton);
dayButton.addActionListener(new RatePanel());
eveningButton.addActionListener(new RatePanel());
offpeakButton.addActionListener(new RatePanel());

}

@Override
public void actionPerformed(ActionEvent e) {
if(dayButton.isSelected()){
chosenRate = DAY_RATE;
System.out.println(DAY_RATE);
}
if(e.getSource() == eveningButton){
chosenRate = EVENING_RATE;
System.out.println(EVENING_RATE);
}
if(e.getSource() == offpeakButton){
chosenRate = OFFPEAK_RATE;
System.out.println(OFFPEAK_RATE);
}
System.out.println(e.getSource() == eveningButton);
System.out.println(dayButton.isSelected());
}


}

最佳答案

您不应该为每个组件创建一个新的 ActionListener 实例。

由于您的面板实现了 ActionListener,因此您每次调用 addActionListener() 时都会创建它们的新实例。

取而代之的是:

field.addActionListener(new MinutesPanel());

// ...

eveningButton.addActionListener(new RatePanel());
offpeakButton.addActionListener(new RatePanel());

你应该这样做:

field.addActionListener(this);

// ...

eveningButton.addActionListener(this);
offpeakButton.addActionListener(this);

关于java - JTextField.getText() 返回空值,radiobuttons.isSelected() 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915572/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com