gpt4 book ai didi

java - Action 监听器不工作

转载 作者:行者123 更新时间:2023-12-01 05:13:07 25 4
gpt4 key购买 nike

我正在尝试为 JComboBoxe 实现一个 ActionListener,以便当选择列表中的项目并单击 ok jbutton 时,我希望它出现在我定义了文本字段的新 gui 中,因此当从组合框中选择项目,它将显示在 GUI 的文本字段中以及所选项目的详细信息。

这个示例显示了一个组合框,但我总共有 6 个。

jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jComboBox4MouseClicked(evt);
}
});

最佳答案

首先添加一个ActionListener到所需的按钮

// When the button is clicked this is called...
public class ButtonActionListener extends ActionListener {
public void actionPerformed(ActionEvent evt) {
Object value = comboBox.getSelectedItem();
// check for null value
// do what ever it is you want to do after that...
}
}

如果你想监听ComboBox的变化,你有很多选择,最简单的是ActionListener

// When the button is clicked this is called...
public class ComboBixActionListener extends ActionListener {
public void actionPerformed(ActionEvent evt) {
Object value = comboBox.getSelectedItem();
// The combo box value has changed, maybe update the text field???
}
}

关于java - Action 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11776296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com