【发布时间】:2014-04-29 07:17:23
【问题描述】:
我正在尝试将我的 Swing GUI 与我的实际代码分开。简而言之,我希望用户启动一个进程(基于用户的选择);在这种情况下,将不再需要 JFrame。
我想不通的是如何将用户从 GUI.class 中的选择与 Main.class 共享。
你对我有什么建议吗?
这是我的代码:
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI(templates);
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
public GUI(Object[] objects) {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox<String> comboBox = new JComboBox<String>(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
}
});
cp.add(comboBox);
}
}
【问题讨论】:
-
保留框架;用
CardLayout更改内容。
标签: java swing actionlistener