Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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 如何添加侦听器,以便在更改组合框值时_Java_Swing_Event Handling_Jtextfield_Jcombobox - Fatal编程技术网

Java 如何添加侦听器,以便在更改组合框值时

Java 如何添加侦听器,以便在更改组合框值时,java,swing,event-handling,jtextfield,jcombobox,Java,Swing,Event Handling,Jtextfield,Jcombobox,我尝试使用建议的方法,但无法确定如何在建议的操作侦听器中使用操作侦听器 我想更改第一个组合框的值,并希望下一个组合框在更改时自动更新,类似地,当组合框_1更改时,文本框也会更改 String[] b = a.getCourseCodes(); final List f = new ArrayList(); final JComboBox comboBox = new JComboBox(b); comboBox.addActionListener(new Act

我尝试使用建议的方法,但无法确定如何在建议的操作侦听器中使用操作侦听器

我想更改第一个组合框的值,并希望下一个组合框在更改时自动更新,类似地,当组合框_1更改时,文本框也会更改

    String[] b = a.getCourseCodes();
    final List f = new ArrayList();

    final JComboBox comboBox = new JComboBox(b);
    comboBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            String item = comboBox.getSelectedItem().toString();
         }
    });

    comboBox.setEditable(true);

    comboBox.setBounds(360, 70, 86, 20);
    contentPane.add(comboBox);

    JLabel lblStudentName = new JLabel("Student Name");
    lblStudentName.setBounds(270, 149, 80, 14);
    contentPane.add(lblStudentName);

    String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
    final JComboBox comboBox_1 = new JComboBox(v);
    comboBox_1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            String item = comboBox_1.getSelectedItem().toString();
         }
    });

    comboBox_1.setBounds(360, 108, 86, 20);
    contentPane.add(comboBox_1);

    textField_3 = new JTextField();
    String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
    textField_3.setText(y);
    textField_3.setEditable(false);
    textField_3.setBounds(360, 146, 86, 20);
    contentPane.add(textField_3);
    textField_3.setColumns(10);

请帮助编辑代码,这样可以有一个明确的想法。。。谢谢

而不是添加一个
项目监听器
我只需添加一个
操作监听器
,它将在每次更改所选值时触发。然后您就可以像这样使用
comboBox.getSelectedItem()

JComboBox comboBox_1; //you need to declare the comboBox and textField before the ActionListener.
comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
        comboBox_1.setModel(new DefaultComboBoxModel<String>(v));

        String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
        textField_3.setText(y);
    }
});
JComboBox组合框_1//您需要在ActionListener之前声明comboBox和textField。
comboBox.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
字符串[]v=a.getStudentID(comboBox.getSelectedItem().toString());
comboBox_1.setModel(新的DefaultComboxModel(v));
字符串y=a.getStudentName(组合框_1.getSelectedItem().toString());
textField_3.setText(y);
}
});
添加您可以扩展此功能,以更改
actionPerformed
方法中的
组合框
TextField
的值


我想这就是你的意思,虽然我可能在你的
ActionListener

的意图上是错误的,但我不确定你发布的代码是否足以让我们确定你可能做错了什么,除了
字符串的范围仅限于
itemStateChanged(…)
方法中的if块。如果你在别处需要那根绳子,你就被卡住了。为了更好的帮助,请考虑创建和发布。我们不想看到您的整个程序,但您应该将代码压缩为最小的代码,它仍然可以编译,没有与您的问题无关的额外代码,但仍然可以演示您的问题。Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。此外,像
textField_3=newjtextfield()这样的语句应该更像
textField_3=newjtextfield(10)
建议所需的尺寸(在列中:该尺寸为10)。作为旁白。到目前为止,你提出的每个问题都有答案。为什么没有任何答案?接受答案有助于其他人在将来找到答案,并让回答的人知道他们的答案有帮助。坦率地说,如果这些答案都没有帮助找到问题的解决方案,我无法理解你为什么坚持在这里发布更多的问题。仍然不起作用,这是正确的格式吗???最终JComboBox组合框=新的JComboBox(b);comboBox.addActionListener(新ActionListener(){public void actionPerformed(ActionEvent arg0){String item=comboBox.getSelectedItem().toString();String[]v=a.getStudentID(item);最终的JComboBox comboBox_1=new JComboBox(v);comboBox_1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0){String item1=comboBox.getSelectedItem().toString();String y=a.getStudentName(item1);textField_3=new JTextField(y);}}}})@DanishAli哪部分不起作用?仅此一部分无法完成任何可见的操作。您需要添加自己的代码,以更改
组合框
文本字段
@DanishAli我不确定是否完全知道您正在尝试执行的操作。但是,我认为您不应该在
ActionListener
@DanishAli:请不要在注释中发布代码,因为它会丢失格式,使其无法阅读。相反,请将任何新代码发布到您原始问题的底部。此外,请不要忽略我对您原始问题的注释,因为它提供了一些建议,如果您遵循这些建议,将有助于我们更好地理解您的问题,从而更好地帮助您。@hoverfullofeels注意到。谢谢