Wie zu Beginn der 2. Jframe durch klicken auf Menüpunkt 1. java-windows-Anwendung (Jframe)

Ich bin mit dem eclipse 4.2 mit Java.

Ich habe 2 java-Programm : AppWin.java Form1.java

AppWin.java ist die gui-windows-Anwendung mit menu/menu item1.

Form1.java ist ein Gui - Jframe

Ich Sie gerne nenne Form1.java aus AppWin.java von klicken Sie auf die menu/menu item1.
Wenn in der Nähe Form1.java ist es wieder zu AppWin.java.

Ist dies so etwas wie MDIFORM. Ich kann mir wirklich nicht beantworten.
Bitte helfen Sie , wenn Sie wissen, eclipse-Menü.

Dank

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

...

Mit Ihrer Hilfe habe ich einen großen Schritt gemacht.
Vielen Dank an alle von Euch!

Nächste ist meine Letzte demo, windows 7, eclipse 4.2, java Gui
Hoffe, es ist hilfreich für andere.

Gibt es 3 Teile : AppWin, Form1, Form2. AppWin ist top, main, die call Form1 und Form2 mit Menü/Element.

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

}

Nochmals vielen Dank

  • Anstelle von mehreren frames verwenden CardLayout
  • Obligatorische Beratung: Die Verwendung von Mehreren JFrames, Gute/Schlechte Praxis?
  • Verwenden JInternalFrame statt. Siehe Verwendung der Internen Frames
  • Vielen Dank an alle Eure Antworten. CardLayout? kannst du ein einfaches Beispiel. Für JInternalFrame, ich weiß, es ist für Netbean. Eclipse unterstützt? Ich bin neu in der java-GUI, deshalb brauchen Sie eine einfache Möglichkeit, es zu tun. Warum muss ich im Menü Anwendung? Ich habe viele java-Programme. Aber ich weiß nie, wie die Gruppe Sie zusammen in Java. Also bitte sagen Sie mir eine einfache Möglichkeit zum aufrufen 2. Formular aus dem Menü/Element. Dank
InformationsquelleAutor tony5 | 2013-06-18
Schreibe einen Kommentar