返回null值的JTextField.getText()和radiobuttons.isSelected()始终返回false

时间:2016-06-20 06:08:15

标签: java jtextfield

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


}

1 个答案:

答案 0 :(得分:2)

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

由于您的小组实施ActionListener,因此每次拨打addActionListener()时都会创建新的实例。

而不是:

field.addActionListener(new MinutesPanel());

// ...

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

你应该这样做:

field.addActionListener(this);

// ...

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