gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:45:19 25 4
gpt4 key购买 nike

我正在使用 Eclipse 4.2 和 Java。

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

AppWin.java 是带有 menu/menu item1 的 GUI Windows 应用程序。

Form1.java 是一个 Gui Jframe

我喜欢通过单击menu/menu item1AppWin.java调用Form1.java。关闭Form1.java后,又回到AppWin.java

这类似于MDIFORM。我实在找不到答案。如果您知道 eclipse 菜单,请帮忙。

谢谢

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

...

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

接下来是我的最终演示,在 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);
}
}
}

}

再次感谢

最佳答案

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

另请参阅:How to Use Internal Frames

关于java - 如何通过单击第一个java Windows应用程序(Jframe)中的菜单项启动第二个Jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161861/

25 4 0