我正在为一个类创建一个程序,其中有一个JComboBox,当选择一个选项时,它将弹出一个带有不同选项的窗口。我有一个选项可以弹出一个带有两个按钮的新窗口。
首先,我不确定是否应该对JComboBox选项使用ItemListener或ActionListener。现在,我有一个ItemListener,我认为它仅适用于“ Matrix”选项,但同时适用于两个选项,我不知道为什么。我会发布所有代码,以防万一,但是我将在指定问题的上方和下方添加星号。
感谢您的帮助或为我指明正确的方向!
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);
}
}
最佳答案
ItemListener
和ActionListener
会告诉您有关组合框的更改时间。然后,您需要确定发生了什么变化并采取适当的措施
例如...
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/