【问题标题】:Displaying value in combo box in a text field upon button click单击按钮时在文本字段中的组合框中显示值
【发布时间】:2017-05-07 17:55:38
【问题描述】:

单击按钮(jButton1)时执行以下方法!单击 jbutton1 时,我想在文本字段 (jTextField3) 的组合框 (jComboBox1) 中显示所选项目!

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (evt.getSource() == jComboBox1 )

     type = (String)jComboBox1.getSelectedItem();

     jTextField3.setText(type);

}

但是虽然运行程序时没有编译错误,但 jTextField3 中没有任何显示!我该如何解决这个问题?

【问题讨论】:

  • 作为旁注,请使用{ } 所有时间。虽然我并不是说这会解决问题,而是作为一种良好做法并防止未来出现意外结果。
  • 我用过还是不行
  • 你的按钮叫什么?换句话说,你给它的变量名是什么?
  • @KasunKumara 那么你的 if 语句不应该是 --> if (evt.getSource() == jButton1 )
  • @Aominè:好眼睛。确实应该。更好 - 避免需要此类结构的侦听器。

标签: java combobox click jbutton textfield


【解决方案1】:

它适用于

type = jComboBox1.getSelectedItem().toString();

【讨论】:

  • 请告诉我这与 OP 已经尝试过的区别?
  • 你的问题是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
相关资源
最近更新 更多