eclipse 如何通过单击第一个 java windows 应用程序 (Jframe) 中的菜单项启动第二个 Jframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17161861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:45:15  来源:igfitidea点击:

How to start 2nd Jframe by click menu item in 1st java windows application (Jframe)

javaeclipseswingmenujframe

提问by tony5

I am using eclipse 4.2 with Java.

我正在将 Eclipse 4.2 与 Java 一起使用。

I have 2 java program : AppWin.javaForm1.java

我有 2 个 java 程序: AppWin.javaForm1.java

AppWin.javais gui windows application with menu/menu item1.

AppWin.java是带有menu/menu item1.

Form1.javais a Gui Jframe

Form1.java是贵 Jframe

I like to call Form1.javafrom AppWin.javaby click the menu/menu item1. When close Form1.java, it is back to AppWin.java.

我喜欢通过单击Form1.java从呼叫。当关闭时,它返回到。AppWin.javamenu/menu item1Form1.javaAppWin.java

This is something like MDIFORM. I really cannot find answer. Please help , if you know eclipse menu.

这就像MDIFORM. 我真的找不到答案。请帮助,如果你知道日食菜单。

Thanks

谢谢

package top;

import java.awt.EventQueue;

public class AppWin {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AppWin window = new AppWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

...

...

With your help, I made a big step. Thanks to all of you!

在你的帮助下,我迈出了一大步。感谢大家!

Next is my final demo, in windows 7, eclipse 4.2, java Gui Hope it is helpful to others.

接下来是我的最终演示,在 windows 7、eclipse 4.2、java Gui 希望对其他人有所帮助。

There are 3 parts : AppWin, Form1, Form2. AppWin is top main which call Form1 and Form2 with menu/item.

有3个部分:AppWin,Form1,Form2。AppWin 是最重要的,它使用菜单/项目调用 Form1 和 Form2。

//1
package top;

import java.awt.EventQueue;

public class AppWin {

    private JFrame frame;

    private Form1 form1;
    private Form2 form2;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AppWin window = new AppWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public AppWin() {
        initialize();
        form1 = new Form1();
        form2 = new Form2();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("Menu1");
        menuBar.add(mnNewMenu);

        JMenuItem mntmNewMenuItem = new JMenuItem("menu item1");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                form1.setVisible(true);
            }
        });
        mnNewMenu.add(mntmNewMenuItem);

        JMenuItem mntmNewMenuItem_1 = new JMenuItem("menu item2");
        mntmNewMenuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                form2.setVisible(true);
            }
        });
        mnNewMenu.add(mntmNewMenuItem_1);

        JMenu mnNewMenu_1 = new JMenu("Menu2");
        menuBar.add(mnNewMenu_1);

        JMenuItem mntmMenuItem = new JMenuItem("Menu item3");
        mnNewMenu_1.add(mntmMenuItem);
    }

}

//2
package top;

import java.awt.BorderLayout;

public class Form1  extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Form1 frame = new Form1();
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Form1() {
//      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("this Form1");
        contentPane.add(lblNewLabel, BorderLayout.WEST);

        textField = new JTextField();
        contentPane.add(textField, BorderLayout.CENTER);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("New button");
        contentPane.add(btnNewButton, BorderLayout.EAST);
    }

}

//3
package top;

import java.awt.BorderLayout;

public class Form2 extends JDialog {

    private final JPanel contentPanel = new JPanel();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            Form2 dialog = new Form2();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public Form2() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JLabel lblThisForm = new JLabel("This Form2");
            contentPanel.add(lblThisForm);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}

Thanks again

再次感谢

回答by Bnrdo

You better use JDesktopPane+ JInternalFramefor that purpose instead. Here's a quick sample.

你最好使用JDesktopPane+JInternalFrame来代替。这是一个快速示例。

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;

    import javax.swing.AbstractAction;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;

    public class JInternalFrameSample {

        private JPanel pnlMain;
        private JDesktopPane desk;

        public JInternalFrameSample(){
            pnlMain = new JPanel(new BorderLayout()){
                @Override public Dimension getPreferredSize(){
                    return new Dimension(600,600);
                }
            };
            desk = new JDesktopPane();

            JMenuBar bar = new JMenuBar();
            JMenu menu = new JMenu("Internal Frame");
            JMenuItem item = new JMenuItem();

            item.setAction(new AbstractAction("Create New") {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    JInternalFrame iFrame = new JInternalFrame("Created from Menu");
                    iFrame.setResizable(true);
                    iFrame.setClosable(true);
                    iFrame.setIconifiable(true);
                    iFrame.setSize(new Dimension(300, 300));
                    iFrame.setLocation(0, 0);

                    //iFrame.getContentPane().setLayout(new BorderLayout());
                    //iFrame.getContentPane().add( new YourCustomUI().getUI() );

                    iFrame.setVisible(true);
                    desk.add(iFrame);
                }
            });


            menu.add(item);
            bar.add(menu);

            pnlMain.add(bar, BorderLayout.PAGE_START);
            pnlMain.add(desk, BorderLayout.CENTER);
        }

        private JPanel getUI(){
            return pnlMain;
        }

        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame("Demo");
                    frame.getContentPane().setLayout(new BorderLayout());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.getContentPane().add(new JInternalFrameSample().getUI());
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }

See also : How to Use Internal Frames

另请参阅:如何使用内部框架

回答by Costis Aivalis

If you do not like the JDesktopPane and JInternalFramesolution, just use your AppWin JFrame as is, and open modal JDialogs for the rest of the forms, instead of JFrames. Modal dialogs can float around the desktop and do not allow you to click your AppWin, until they are closed.

如果您不喜欢JDesktopPane 和 JInternalFrame解决方案,只需按原样使用 AppWin JFrame,并为其余表单打开模式 JDialogs,而不是 JFrames。模态对话框可以在桌面上浮动并且不允许您单击 AppWin,直到它们关闭。

It is usually better to use just one main JFrame for an application, unless you have some wizard application that moves progressively from one JFrame to the other and back. Even with a wizard app, you can stick with one JFrame and update progressively just the ContentPane with JPanels.

通常最好为应用程序只使用一个主 JFrame,除非您有一些向导应用程序从一个 JFrame 逐渐移动到另一个 JFrame 并返回。即使使用向导应用程序,您也可以坚持使用一个 JFrame 并仅使用 JPanel 逐步更新 ContentPane。

Here is the AppWin JFrame:

这是 AppWin JFrame:

public class AppWin extends javax.swing.JFrame {
    private Form1 form1;
    private Form1 form2;
    ...
    private FormN formN;

    public AppWin() {
        initComponents();
        form1 = new Form1(this, true);
        form2 = new Form2(this, true);
        ...
        formN = new FormN(this, true);
    }
    ...
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        form1.setVisible(true);
    }

And here is your Form1 JDialog:

这是您的 Form1 JDialog:

public class Form1 extends javax.swing.JDialog {
    public Form1(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    ...
    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        setVisible(false);
    }      

回答by user2496226

I only use NetBeans for GUI building because that's more convenient. In the following I can tell you how to achieve what you want to do but I can't tell you how to layout all the components because NetBeans do that for me.

我只使用 NetBeans 来构建 GUI,因为这样更方便。在下文中,我可以告诉您如何实现您想要做的事情,但我无法告诉您如何布局所有组件,因为 NetBeans 为我做了这些。

So basically you want to 1. show secondFrame by clicking a menuitem and then close mainFrame, 2. show mainFrame after closing secondFrame, yes? Then, the key is to pass the reference of mainFrame to secondFrame, and write your own method of formClosing event of secondFrame. Something like this:

所以基本上你想 1. 通过单击菜单项显示 secondFrame 然后关闭 mainFrame, 2. 在关闭 secondFrame 后显示 mainFrame,是吗?然后,关键是将mainFrame的引用传递给secondFrame,自己编写secondFrame的formClosing事件方法。像这样的东西:

In the menuItem method in your mainframe:

在大型机的 menuItem 方法中:

private void menuItemActionPerformed(java.awt.event.ActionEvent evt) {
    //pass 'this' frame's (mainFrame) reference to the secondFrame
    SecondFrame newFrame = new SecondFrame(this);
    newFrame.setVisible(true); //show the secondFrame
    this.setVisible(false);  //hide this frame (mainFrame)
}

In your secondFrame:

在你的第二个框架中:

public class SecondFrame extends javax.swing.JFrame {

private MainFrame mainFrame;

//define your own constructor that can use mainFrame's reference
public SecondFrame(MainFrame mainFrame) {
    initComponents();
    this.mainFrame = mainFrame;
}

private void initComponents(){
    //bind your own event for closing second frame
    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
        }
    });

    /***********your stuff***************/
}

//show mainFrame when closing this frame and then dispose this frame
private void formWindowClosing(java.awt.event.WindowEvent evt) {
    mainFrame.setVisible(true);
    this.dispose();
}

}

}

The codes above is for disposing the secondFrame when closing it. If you just want to hide the secondFrame when closing for future use, then the codes will be slightly different. Let me know what you up to.

上面的代码用于在关闭时处理 secondFrame。如果您只是想在关闭时隐藏 secondFrame 以备将来使用,那么代码会略有不同。让我知道你在做什么。

回答by Rrezart A. Prebreza

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
form1.setVisible(true); dispose(); }

私有无效 jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
form1.setVisible(true); 处置();}