如何关闭JFrame并因此打开它,而不退出JVM / Application?

时间:2012-07-06 00:43:59

标签: java swing jframe jbutton

如何在不退出JVM /整个应用程序的情况下关闭JFrame并打开它?我希望只退出JFrame并立即打开,而不关闭JFrame1,也不主要退出JVM /整个应用程序。我想用JButton做。

我想用这样的JButton:JButton(“我关闭这个JFrame并立即打开它,以便再次执行构造函数,而不退出JVM /应用程序”);

我想要但我不知道该怎么做:我需要一种方法,当你点击一个窗口的“x”时,以同样的方式关闭JFrame窗口。如下:JFrame.close(); //喜欢窗口的“x”JFrame.setVisible(true)

感谢。

JFrame功能的打印在这里:
enter image description here

我自我解答

3 个答案:

答案 0 :(得分:2)

不要显示“sub”JFrame,因为应用程序通常只显示一个JFrame。您正在谈论的工作,依赖于父窗口的窗口的工作是模态或非模态JDialog的用途,如果关闭则永远不会导致应用程序退出的窗口。虽然说过这个也考虑其他替代方案,比如使用CardLayout交换JComponents或JTabbedPane。

请理解,JDialog可以显示与任何JFrame上显示的GUI一样复杂和丰富的GUI。

编辑,关于:

  

我在JFrame上有一个JTable,是的,我希望再次实现JFrame以显示不同的GUI状态,关闭并打开JFrame,因为代码在其构造函数上。

代码可以移出构造函数。对于GUI中的状态更改,我强烈建议您使用CardLayout。将JTable放入JPanel,然后交换JFrame的contentPane持有的JPanel。如果您没有窗口进出视图,用户界面会更加顺畅。

编辑2
好吧,如果你想显示一个编辑JTable行的窗口,我会做的是创建一个JPanel来保存JTextFields中的行数据,然后在JOptionPane.showConfirmDialog中显示JPanel(... ),然后使用JTextFields保存的数据编辑JTable行。我不会为此创建一个单独的JFrame。

编辑3 例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class Foo1 extends JPanel {
   private static final String[] COLUMN_NAMES = { "Foo", "Bar", "Licious" };
   private static final Object[][] DATA = { { 1, "Abaddon", "A" },
         { 2, "Beelzebub", "B" }, { 3, "Chuck", "C" }, { 4, "Dagon", "D" },
         { 5, "Eisheth", "E" }, {6, "Forneus", "F"}};
   private DefaultTableModel tableModel = new DefaultTableModel(DATA,
         COLUMN_NAMES);
   private JTable table = new JTable(tableModel);

   public Foo1() {
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(new JButton(new EditRowAction()));

      setLayout(new BorderLayout());
      add(new JScrollPane(table));
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   private class EditRowAction extends AbstractAction {
      public EditRowAction() {
         super("Edit Row");
         putValue(MNEMONIC_KEY, KeyEvent.VK_E);
      }

      @Override
      public void actionPerformed(ActionEvent arg0) {
         int rowIndex = table.getSelectedRow();
         if (rowIndex != -1) {
            Object[] row = new Object[table.getColumnCount()];
            for (int column = 0; column < row.length; column++) {
               row[column] = tableModel.getValueAt(rowIndex, column);
            }
            MyRowPanel myRowPanel = new MyRowPanel(row);
            int result = JOptionPane.showConfirmDialog(Foo1.this, myRowPanel,
                  "Edit Row Information and Press \"OK\"", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
               for (int col = 0; col < row.length; col++) {
                  tableModel.setValueAt(myRowPanel.getFieldText(col), rowIndex,
                        col);
               }
            }
         }
      }
   }

   private class MyRowPanel extends JPanel {
      private JTextField[] textFields;

      public MyRowPanel(Object[] row) {
         textFields = new JTextField[row.length];
         for (int i = 0; i < row.length; i++) {
            textFields[i] = new JTextField(String.valueOf(row[i]), 10);
            add(textFields[i]);
         }
      }

      public String getFieldText(int index) {
         // check first that index is not out of range and throw exception if it
         // is
         if (index < 0 || index > textFields.length) {
            throw new ArrayIndexOutOfBoundsException("for index = " + index);
         }

         return textFields[index].getText();
      }

   }

   private static void createAndShowGui() {
      Foo1 mainPanel = new Foo1();

      JFrame frame = new JFrame("Foo1");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

编辑4 关于:

  

是的,我明白你的意思,它确实有效,这是,我会识别编辑的行并用更新的值替换。但实际上,我想刷新以保持数据库中的真实数据,而不是化妆“编辑”JTable。

好的,那么在我上面的代码示例中,我会在显示JOptionPane并在if块内部后立即创建PreparedStatement,并在PreparedStatement上调用executeUpdate(当然在后台线程中)。

答案 1 :(得分:2)

我google了,我找到了解决方案,就是这样:

       WindowEvent closingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
       Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);
       frame.setVisible(true);

它使用WindowEvent执行与“x”点击相同的操作。

无论如何,谢谢你们的帮助!!!

答案 2 :(得分:0)

如果您只想在不退出JVM /整个应用程序的情况下关闭并重新打开嵌套框架,则应更改嵌套框架的“ defaultCloseOperation ”属性。

请参阅我的简单示例并注意setDefaultCloseOperation:

的用法
    JFrame mainFrame = new JFrame("Main Frame");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JFrame nestedframe = new JFrame("Nested Frame");
    nestedframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JButton btn = new JButton("Close & Reopen Nested Frame");
    btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            nestedframe.dispose();
            nestedframe.setVisible(true);
        }
    });

    mainFrame.getContentPane().add(btn);
    mainFrame.setSize(350, 200);
    nestedframe.setSize(250,150);

    mainFrame.setLocation(0,0);
    nestedframe.setLocation(400,0);

    mainFrame.setVisible(true);
    nestedframe.setVisible(true);
相关问题