0

我正在使用带有 Java 的 eclipse 4.2。

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

AppWin.java是带有menu/menu item1.

Form1.java是贵Jframe

我喜欢通过单击来Form1.java拨打AppWin.java电话menu/menu item1。关闭时Form1.java,它又回到AppWin.java.

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

谢谢

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();
                }
            }
        });
    }

...

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

接下来是我最后的demo,在windows 7,eclipse 4.2,java Gui 希望对大家有帮助。

共有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);
            }
        }
    }

}

再次感谢

4

4 回答 4

2

你最好使用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);
                }
            });
        }
    }

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

于 2013-06-18T07:12:14.977 回答
1

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

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

这是 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);
    }

这是您的 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);
    }      
于 2013-06-18T08:02:42.527 回答
0

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

于 2014-10-01T07:03:53.910 回答
0

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

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

在大型机的 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)
}

在你的第二帧:

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();
}

}

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

于 2013-06-18T08:59:34.460 回答