开发者

java gui help actionlistener

I'm trying to set a combobox in my GUI to print the the information about a student in a JLabel.

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

    if combob开发者_StackOverflow中文版ox1="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";
    }
}


You have to listen for itemstatechange on your combobox, Upon selecting your student , fetch the selected item and operate on to display appropriate messages.

Have a look at this example


If it is a pseudocode, then it's correct. But in java the same code would be:

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

Of course, it works if combobox1 is String, which holds the value of your combobox.


You are on the right track but you need to read more tutorials. Start with the one suggested by Babban Shikaari. Your code should be something similar to this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜