每次我點擊我的代碼上的提交按鈕時,它會給我每小時的字符串值?即使我的JComboBox中的項目具有每週的當前值?我該如何解決這個問題?JComboBox當前值使用
我ComputeWindow
public class ComputeWindow extends JFrame{
private JLabel payRate,employeeID,payType,gWage;
private JTextField empRate,idTxt,gWageTxt;
private JComboBox rateChooser;
private JPanel panel;
private JButton generate;
public ComputeWindow(){
super("Gross Wage");
setSize(300, 150);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] employeeType = {"Hourly","Bi Weekly","Weekly","Base"};
employeeID = new JLabel("Employee ID: ");
payRate = new JLabel("Employee Rate: ");
payType = new JLabel("Pay Type: ");
gWage = new JLabel("Gross Wage");
empRate = new JTextField(10);
idTxt = new JTextField(10);
gWageTxt = new JTextField(10);
generate = new JButton("Compute!");
rateChooser = new JComboBox(employeeType);
panel = new JPanel(new GridLayout(5,1));
panel.add(employeeID);
panel.add(idTxt);
panel.add(payRate);
panel.add(empRate);
panel.add(payType);
panel.add(rateChooser);
panel.add(gWage);
panel.add(gWageTxt);
panel.add(generate);
add(panel, BorderLayout.CENTER);
String Option =(String)rateChooser.getSelectedItem();
GenerateHandler generateHandler = new GenerateHandler();
generate.addActionListener(generateHandler);
generateHandler.setOption(Option);
}
}
和處理程序這裏是
private class GenerateHandler implements ActionListener{
String option;
public void setOption(String option){
this.option = option;
}
public void actionPerformed(ActionEvent e) {
if(option =="Hourly"){
JOptionPane.showMessageDialog(null,option);
}else if(option == "Weekly"){
JOptionPane.showMessageDialog(null,option);
}
}
}
}
任何建議的代碼?關於如何解決這個問題?或者它會返回或顯示我在JComboBox中使用的當前值?
請學習java的命名約定並堅持他們 – kleopatra