我想创建一个GUI,如果用户在JComboBox中点击6.AM时间然后在JButton上点击然后打开另一个窗口并且Desired route的图片出现这是我的条件但它不像命令那样工作总是在其他块上。请帮忙!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if("6.A.M.".equals((String)jComboBox1.getSelectedItem())) {
SixAMRoute sam=new SixAMRoute();
sam.setVisible(true);
this.dispose();
}
else
System.out.print("Invalid");
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//jButton1.setText((String)jComboBox1.getSelectedItem());
//jButton1.repaint();
}
根据你的评论:它有不同的值,如12.A.M,3.P.M和6.A.M.这似乎是你的if
语句中的拼写错误,因为你在匹配字符串的末尾有一个尾随点(.
):
---------v
if("6.A.M.".equals((String)jComboBox1.getSelectedItem())){...}
只需删除点,它应该工作:
if("6.A.M".equals((String)jComboBox1.getSelectedItem())){...}