【发布时间】:2016-02-20 13:06:49
【问题描述】:
我有一个简单的问题。没看懂……
我有一个 JFrame,我在其中添加了一个 JComboBox:
JComboBox<String> Team_ComboBox = new JComboBox<>();
Team_ComboBox_Handler ComboBox_Listener = new Team_ComboBox_Handler();
Team_ComboBox.addActionListener(ComboBox_Listener);
Team_ComboBox.addItem("Test 1");
Team_ComboBox.addItem("Test 2");
在这个框架上,我有一个打开另一个 JFrame 的按钮。
Play = new JButton();
Play.setText("Play");
Play.setPreferredSize(dimension);
Play.addActionListener(menuhandler);
private class main_menuhandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Play){
teams Team = new teams();
Team.teams();
disposeMainMenue();
}
if(e.getSource()==Close) {
System.exit(DO_NOTHING_ON_CLOSE);
}
}
}
无论如何,我想将 Combobox 的 Selected 值传递给另一个类的方法。我知道如何在方法本身中获取组合框的 itemvalue(使用 getselecteditem)但是如何在 ActionPerformed 方法中做到这一点,因为我无法访问 ActionPerformed 方法中的组合框......我创建了另一个 ActionListener( comboBox_Listener) 但我还没有在里面放任何代码...
有什么想法吗?提前非常感谢
【问题讨论】:
-
请遵循编码标准;与此相关的众多文件之一:geosoft.no/development/javastyle.html
标签: java swing actionlistener jcombobox