简体   繁体   English

一个JFrame打开另一个

[英]One JFrame opening another

I have a JFrame and JPanel full of Jsomethings with an actionlistener. 我有一个JFrame和JPanel, 其中包含一个带有actionlistener的Jsomethings When the user clicks an object I want to open another JFrame. 当用户单击一个对象时,我想打开另一个JFrame。 Here is what I did: 这是我做的:

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (source == rejectionbutton){
        RejectApp ra = new RejectApp();
        ra.main(null);

    }

}

(RejectApp calls a new JFrame.) So another JFrame opens on the screen with more options. (RejectApp调用一个新的JFrame。)所以另一个JFrame在屏幕上打开,有更多选项。 It works OK (so far), but I want to know is this standard? 它工作正常(到目前为止),但我想知道这个标准吗? I mean calling the main method like this? 我的意思是调用这样的主要方法? Another question is, without using a cardlayout (which I don't want to use), is the best way to handle multiple panels, by doing this sort of thing? 另一个问题是,不使用cardlayout(我不想使用),是处理多个面板的最好方法,做这种事情?

I would change a few things. 我会改变一些事情。 First off, usually an application has one JFrame and then if it needs to show another window does so as a modal or non-modal dialog such as can be obtained with a JDialog or JOptionPane. 首先,通常一个应用程序有一个JFrame然后如果它需要显示另一个窗口那么作为模态或非模态对话框,例如可以使用JDialog或JOptionPane获得。 Having said that, it's even more common to have one JFrame and swap "views" in the JFrame -- swap contentPanes or other large panels via a CardLayout as this would mimic the behavior of many gui programs we all currently use. 话虽如此,在JFrame中使用一个JFrame和交换“视图”更为常见 - 通过CardLayout交换contentPanes或其他大型面板,因为这会模仿我们目前使用的许多gui程序的行为。

Personally, I also try to gear my GUI creation towards creating a JPanel or JComponent rather than towards creating a top-level window. 就个人而言,我也尝试将GUI创建用于创建JPanel或JComponent而不是创建顶级窗口。 This way if I want to display the GUI as a stand alone app, a dialog, or an applet I can pop it into the contentPane of a JFrame or JDialog or JApplet respectively, or if as an inner panel of a more complex GUI, then insert it there, or in an application with a swapping view, then as a card in a CardLayout as noted above. 这样,如果我想将GUI显示为独立应用程序,对话框或applet,我可以将它分别弹出到JFrame或JDialog或JApplet的contentPane中,或者如果作为更复杂的GUI的内部面板,则将其插入那里,或者在具有交换视图的应用程序中插入,然后将其作为CardLayout中的卡片插入,如上所述。 The bottom line is I feel that this structure gives you the developer a lot more options in how you can use this GUI. 最重要的是,我认为这种结构为开发人员提供了更多关于如何使用此GUI的选项。

Also, I would avoid calling another class's main as you're doing (assuming this is the public static void main method) as you lose all benefits of OOPs. 另外,我会避免在你正在做的时候调用另一个类的main(假设这是public static void main方法),因为你失去了OOP的所有好处。 You also seem to be trying to call a static method in a non-static way (assuming I understand your program structure correctly). 您似乎也试图以非静态方式调用静态方法(假设我正确理解您的程序结构)。

For your second question, it begs a question of my own: why do you not want to use CardLayout? 对于你的第二个问题,它引出了一个我自己的问题:为什么你不想使用CardLayout?

edit: an example of what I meant is as follows: 编辑:我的意思的一个例子如下:

import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SwingEg {
    private static void createAndShowUI() {
        JFrame frame = new JFrame("Main JFrame");
        frame.getContentPane().add(new MainGUI().getMainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

class MainGUI {
    private static final Dimension MAIN_PANEL_SIZE = new Dimension(450, 300);
    private JPanel mainPanel = new JPanel();
    private JDialog modalDialog;
    private JDialog nonModalDialog;

    public MainGUI() {
        JButton openModalDialogBtn = new JButton("Open Modal Dialog Window");
        openModalDialogBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openModalDialogBtnActionPerformed(e);
            }
        });
        JButton openNonModalDialogBtn = new JButton("Open Non-Modal Dialog Window");
        openNonModalDialogBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openNonModalDialogBtnActionPerformed(e);
            }
        });
        mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
        mainPanel.add(openModalDialogBtn);
        mainPanel.add(openNonModalDialogBtn);
    }

    private void openModalDialogBtnActionPerformed(ActionEvent e) {
        if (modalDialog == null) {
            Window topWindow = SwingUtilities.getWindowAncestor(mainPanel);
            modalDialog = new JDialog(topWindow, "Modal Dialog", ModalityType.APPLICATION_MODAL);
            modalDialog.getContentPane().add(new DialogPanel().getMainPanel());
            modalDialog.pack();
            modalDialog.setLocationRelativeTo(topWindow);
            modalDialog.setVisible(true);
        } else {
            modalDialog.setVisible(true);
        }
    }

    private void openNonModalDialogBtnActionPerformed(ActionEvent e) {
        if (nonModalDialog == null) {
            Window topWindow = SwingUtilities.getWindowAncestor(mainPanel);
            nonModalDialog = new JDialog(topWindow, "Non-Modal Dialog", ModalityType.MODELESS);
            nonModalDialog.getContentPane().add(new DialogPanel().getMainPanel());
            nonModalDialog.pack();
            nonModalDialog.setLocationRelativeTo(topWindow);
            nonModalDialog.setVisible(true);
        } else {
            nonModalDialog.setVisible(true);
        }
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }
}

class DialogPanel {
    private static final Dimension DIALOG_SIZE = new Dimension(300, 200);
    private JPanel dialogPanel = new JPanel();

    public DialogPanel() {
        dialogPanel.add(new JLabel("Hello from a dialog", SwingConstants.CENTER));
        dialogPanel.setPreferredSize(DIALOG_SIZE);
    }

    public JPanel getMainPanel() {
        return dialogPanel;
    }
}

I would rather make a new instance of JFrame or a subclass, or call a new method who makes a new JFrame: 我宁愿创建一个JFrame或子类的新实例,或者调用一个创建新JFrame的新方法:

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();

if (source == rejectionbutton){
    JFrame frame = new JFrame("New Frame");
   //or
   makeNewFrame();
}

} }

Another simple Layout-Manager is the BorderLayout, it´s the default Layout-Manager of the JFrame class. 另一个简单的布局管理器是BorderLayout,它是JFrame类的默认布局管理器。

new YourJFrameNameHere().setVisible(true);

Replace YourJFrameNameHere with the JFrame name. 将YourJFrameNameHere替换为JFrame名称。

Simple, no? 简单,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM