gpt4 book ai didi

java - JComboBox 和 ItemListener/ActionListener

转载 作者:行者123 更新时间:2023-11-30 06:58:24 29 4
gpt4 key购买 nike

I am creating a program for class where you have a JComboBox and when one option is selected it pops up a window with different options.我有一个选项可以弹出一个带有两个按钮的新窗口。

首先,我不确定是否应该为 JComboBox 选项使用 ItemListener 或 ActionListener。现在我有一个 ItemListener,我认为它只适用于“黑客帝国”选项,但它适用于这两个选项,我不知道为什么。我会发布我的所有代码以防万一,但我会在指定问题的上方和下方添加星号。

感谢您的帮助或为我指明正确的方向!

public class MultiForm extends JFrame{


private JComboBox menu;
private JButton bluePill;
private JButton redPill;
private JLabel matrix;
private int matrixSelection;
private static String[] fileName = {"", "The Matrix", "Another Option"};

public MultiForm() {
super("Multi Form Program");
setLayout(new FlowLayout());
menu = new JComboBox(fileName);
add(menu);

*************************************************************************
TheHandler handler = new TheHandler();
menu.addItemListener(handler);
}

private class TheHandler implements ItemListener{
public void itemStateChanged(ItemEvent event) {

if(event.getStateChange() == ItemEvent.SELECTED) {
menu.setSelectedItem("The Matrix");
menu.getSelectedIndex();
*************************************************************************
//Create a new window when "The Matrix" is clicked in the JCB
JFrame newFrame = new JFrame();
JPanel panel = new JPanel();

newFrame.setLayout(new FlowLayout());
newFrame.setSize(500, 300);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
add(panel, BorderLayout.CENTER);

matrix = new JLabel("<html>After this, there is no turning back. "
+ "<br>You take the blue pill—the story ends, you wake up "
+ "<br>in your bed and believe whatever you want to believe."
+ "<br>You take the red pill—you stay in Wonderland, and I show"
+ "<br>you how deep the rabbit hole goes. Remember: all I'm "
+ "<br>offering is the truth. Nothing more.</html>");
newFrame.add(matrix, BorderLayout.NORTH);

Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
bluePill = new JButton("Blue Pill", bp);
newFrame.add(panel.add(bluePill));

Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
redPill = new JButton("Red Pill", rp);
newFrame.add(panel.add(redPill));


newFrame.setVisible(true);
}
}
}

public static void main(String[] args) {
MultiForm go = new MultiForm();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 200);
go.setVisible(true);
}
}

最佳答案

ItemListenerActionListener 将告知组合框的某些内容何时发生更改。然后您需要确定发生了什么变化并采取适当的行动

例如……

private class TheHandler implements ItemListener{
public void itemStateChanged(ItemEvent event) {

if(event.getStateChange() == ItemEvent.SELECTED) {
Object source = event.getSource();
if (source instanceof JComboBox) {
JComboBox cb = (JComboBox)source;
Object selectedItem = cb.getSelectedItem();
if ("The Matrix".equals(selectedItem)) {
// Do the matrix
} else if ("Another Option".equals(selectedItem)) {
// Do another option
}
}
}
}
}

这只是检查 selectedItem 是什么,并根据选择的内容采取适当的操作。您也可以改用 selectedIndex,它将返回表示所选项目的 int,但哪个对您来说更容易。

看看How to Use Combo Boxes了解更多详情

如果您只想知道项目何时被选中,您可能会发现 ActionListener 更简单,因为您不需要检查状态 (SELECTED/UNSELECTED),因为它仅在选定状态更改时触发

关于java - JComboBox 和 ItemListener/ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752072/

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