单击/从组合框中选择项目时打开新JFrame(Java)

单击/从组合框中选择项目时打开新JFrame(Java),java,swing,user-interface,jframe,jcombobox,Java,Swing,User Interface,Jframe,Jcombobox,我想创建一个GUI,其中组合框允许我通过按下组合框中的一个项目来打开一个新的JFrame。有什么想法吗?在组合框中添加一个事件监听器,只需处理该事件即可生成一个新的JFrame在组合框中添加一个事件监听器,只需处理该事件即可生成一个新的JFrame将一个ActionListener添加到JComboBox: JComboBox combo = new ... combo.addActionListener(new ActionListener() { public void actionPe

我想创建一个GUI,其中组合框允许我通过按下组合框中的一个项目来打开一个新的JFrame。有什么想法吗?

在组合框中添加一个事件监听器,只需处理该事件即可生成一个新的JFrame

在组合框中添加一个事件监听器,只需处理该事件即可生成一个新的JFrame

将一个
ActionListener
添加到
JComboBox

JComboBox combo = new ...
combo.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // This code runs when an item is selected in the combo.
    JFrame frm = new ...
    frm.setVisible(true);
  }
});

ActionListener
添加到
JComboBox

JComboBox combo = new ...
combo.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // This code runs when an item is selected in the combo.
    JFrame frm = new ...
    frm.setVisible(true);
  }
});

与其这样,不如使用合适的布局管理器(例如)?这将使您能够轻松地在同一容器中切换视图。

相反,使用适当的布局管理器(例如)如何?这将使您能够轻松地在同一容器中切换视图。

到目前为止,我从未使用过CardLayout。我来看看我能做些什么。例如,请参阅。另请参阅两个答案的链接,其中有许多其他适合不同用例的备选方案。到目前为止,我从未使用过CardLayout。我来看看我能做些什么。例如,请参阅。另请参见两个答案的链接,其中有许多其他适合不同用例的备选方案。