Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java gui帮助actionlistener_Java_User Interface - Fatal编程技术网

java gui帮助actionlistener

java gui帮助actionlistener,java,user-interface,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";

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

如果它是一个伪代码,那么它是正确的。但在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(...);
  }

当然,如果
combobox 1
是字符串,它可以保存combobox的值。

您必须侦听combobox上的itemstatechange, 选择学生后,获取所选项目并继续操作以显示相应的消息


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

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

你只是在猜测,是吗?是的,有人能给我指出正确的方向吗?在你最初的问题中,我想包括一个指向Swing教程()的链接。嗯,链接被包括在内,但它是包括在我的答复开始。无论如何,这个想法是让你先阅读教程,因为教程中有大量的工作示例。ActionListener使用起来更简单,因为您不必担心ItemListener生成的两个事件。还有一个问题,我如何让它在新行上打印每个事件?我认为将“setText”替换为“println”是否正确,例如jlabel.println(“名称:a”);jlabel.println(“年龄:12岁”);然后,您可以为每一行创建几个jlabel。或者在settext方法中包含换行符。即jlabel.setText(“诸如此类”\n诸如此类);