java gui help actionlistener

时间:2010-12-05 18:33:04

标签: java user-interface

我正在尝试在我的GUI中设置一个组合框,以便在JLabel中打印有关学生的信息。

private void studentComboBoxMouseClicked(java.awt.event.MouseEvent evt) {

    if combobox1="student1"{
        println.jlabel."name:a";
        println.jlabel.""age:12";
        println.jlabel."course:english";
    }

    if combobox1="student2"{
        println.jlabel."name:b";
        println.jlabel.""age:11";
        println.jlabel."course:maths";
    }

    if combobox1="student3"{
        println.jlabel."name:c";
        println.jlabel.""age:10";
        println.jlabel."course:science";
    }
}

3 个答案:

答案 0 :(得分:1)

你必须在你的组合框上听itemstatechange, 选择学生后,获取所选项目并继续操作以显示相应的消息。

Have a look at this example

答案 1 :(得分:0)

如果它是伪代码,那么它是正确的。但在java中,相同的代码是:

  if ("student1".equals(combobox1)) {
    jlabel.setText("name:a age:12 course:english");
  } else if ("student2".equals(combobox1)) {
    jlabel.setText(...);
  } else if ("student3".equals(combobox1)) {
    jlabel.setText(...);
  }

当然,如果combobox1是String,它可以保存你的组合框的值。

答案 2 :(得分:0)

您走在正确的轨道上,但您需要阅读更多教程。从Babban Shikaari建议的那个开始。您的代码应该与此类似:

if (combobox.getSelectedItem().equals("student1")){
   jlabel.setText("Your new information");
}