有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用户界面java gui帮助actionlistener

我试图在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";
    }
}

共 (2) 个答案

  1. # 1 楼答案

    你的思路是正确的,但你需要阅读更多的教程。从Babban Shikaari的建议开始。您的代码应该类似于以下内容:

    if (combobox.getSelectedItem().equals("student1")){
       jlabel.setText("Your new information");
    }
    
  2. # 2 楼答案

    如果它是伪代码,那么它是正确的。但在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是字符串,它就可以工作,该字符串保存组合框的值