2016-06-20 5 views
2

2種類のバグがあり、どちらが原因か分かりません。どちらもactionPerformedメソッド内にあるようです。これらは3つの異なるファイルです。フォーマットが悪いのは残念ですが、正しく動作することはありません。null値を返すJTextField.getText()とradiobuttons.isSelected()は常にfalseを返します

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


} 

答えて

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); 
+0

おかげで、愚かな質問には申し訳ありません。これが初めてのUI作成です。 –

関連する問題