简体   繁体   English

从JButton问题打开一个新的类(JFrame)

[英]Opening a new class (JFrame) from JButton Issue

just a disclaimer: I already checked most of Stack Overflow and couldn't find an answer on the website. 只是一个免责声明:我已经检查了大多数Stack Overflow,但在网站上找不到答案。 I also asked some nice people on Reddit, but only fixed it halfway through. 我还问了Reddit上的一些好人,但只在中途修复了它。

So, I am trying the create a GUI application and I am new to the Java language (very, very new - so there is a big chance I am making a rookie mistake). 因此,我正在尝试创建GUI应用程序,并且对Java语言还是陌生的(非常非常新-因此,我很可能犯了一个菜鸟错误)。 I already designed my interface for my 'StartScreen' and I wanted to make button1 to open my second class ('NewUsers'). 我已经为“ StartScreen”设计了我的界面,并且想要创建button1来打开第二个类(“ NewUsers”)。

I was using the GUI Editor and I think this is probably the cause of my problems, as an answer to my question online I just saw people add 我使用的是GUI编辑器,我认为这可能是我遇到问题的原因,在网上回答我的问题时,我看到有人在添加

this.dispose();
new JFrame().setVisible(true);

This doesn't work on me however. 但是,这对我不起作用。 I managed to fix it until a certain point and now I am able to close the frame on a click, but I still can't figure out how to open another frame from the button :(. Here is my code for Frame1('StartScreen'). Frame2 is just an empty window for now, but it's for testing purposes. 我设法将其修复到某个点,现在我可以单击一下来关闭框架,但是我仍然不知道如何通过按钮:(。这是我的Frame1('StartScreen ')。目前,Frame2只是一个空窗口,但它仅用于测试目的。

 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class StartScreen { private final JFrame frame; private JButton newUserButton; private JButton existingUserButton; private JButton nonUserBookingButton; private JPanel panell; public StartScreen() { this.frame = new JFrame("StartScreen"); frame.setContentPane(panell); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); newUserButton.addActionListener(this::onNewUserClicked); } public static void newuserscall () { NewUsers newuserscall = new NewUsers(); } private void onNewUserClicked(ActionEvent event) { frame.setVisible(false); } public static void main(String[] args) { new StartScreen(); } { // GUI initializer generated by IntelliJ IDEA GUI Designer // >>> IMPORTANT!! <<< // DO NOT EDIT OR ADD ANY CODE HERE! $$$setupUI$$$(); } /** * Method generated by IntelliJ IDEA GUI Designer * >>> IMPORTANT!! <<< * DO NOT edit this method OR call it in your code! * * @noinspection ALL */ private void $$$setupUI$$$() { panell = new JPanel(); panell.setLayout(new GridBagLayout()); panell.setPreferredSize(new Dimension(600, 400)); final JPanel spacer1 = new JPanel(); GridBagConstraints gbc; gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 4; gbc.fill = GridBagConstraints.VERTICAL; gbc.insets = new Insets(30, 0, 0, 0); panell.add(spacer1, gbc); newUserButton = new JButton(); newUserButton.setText("Button"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.BOTH; panell.add(newUserButton, gbc); existingUserButton = new JButton(); existingUserButton.setText("Button"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 4; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.BOTH; panell.add(existingUserButton, gbc); nonUserBookingButton = new JButton(); nonUserBookingButton.setText("Button"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 6; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.BOTH; panell.add(nonUserBookingButton, gbc); final JPanel spacer2 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.VERTICAL; panell.add(spacer2, gbc); final JPanel spacer3 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 5; gbc.fill = GridBagConstraints.VERTICAL; gbc.insets = new Insets(0, 250, 0, 0); panell.add(spacer3, gbc); final JPanel spacer4 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 5; gbc.fill = GridBagConstraints.VERTICAL; panell.add(spacer4, gbc); final JPanel spacer5 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 1; gbc.fill = GridBagConstraints.VERTICAL; gbc.insets = new Insets(25, 0, 0, 0); panell.add(spacer5, gbc); final JLabel label1 = new JLabel(); label1.setText("Label"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 0; panell.add(label1, gbc); final JPanel spacer6 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 6; gbc.fill = GridBagConstraints.VERTICAL; gbc.insets = new Insets(30, 0, 0, 0); panell.add(spacer6, gbc); final JPanel spacer7 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.fill = GridBagConstraints.VERTICAL; gbc.insets = new Insets(30, 0, 0, 0); panell.add(spacer7, gbc); } /** * @noinspection ALL */ public JComponent $$$getRootComponent$$$() { return panell; } } 

Thanks in advance! 提前致谢! I hope some of you can figure out a way I can fix my code :( 我希望你们中的一些人可以找出一种方法来修复我的代码:(

Regards 问候

Well what I'll start out saying is that you are correct: never use a GUI Builder. 好吧,我首先要说的是您是正确的:永远不要使用GUI Builder。 I don't even mean when you're trying to learn, I mean never use it. 我什至不是要当您尝试学习时,就是不要使用它。 It's mainly there for prototyping a concept and not meant for you to use it for learning or production code. 它主要用于为概念创建原型,并不意味着您可以将其用于学习或生产代码。 Below is a cleaned up more precise example for you, but in your particular example, you need to add your 以下是为您整理的更精确的示例,但是在您的特定示例中,您需要添加

new JFrame().setVisible(true);

inside of your onNewUserClicked function. onNewUserClicked函数内部。 Also, note that when you do that, you're creating an empty frame, that will be the absolute minimum amount of size that it takes for your OS to create it because there's nothing in it, you never packed the frame, and never manually and explicitly set the size, so check around your top left area of your monitor - it's there. 另外,请注意,执行此操作时,您正在创建一个空框架,这将是操作系统创建该框架所需的绝对最小大小,因为其中没有任何内容,您从未打包框架,也从未手动进行过打包并明确设置尺寸,因此请检查一下显示器左上角的区域-该位置在那。

Here is probably closer to what you want in terms of cleanliness. 就清洁度而言,这可能更接近您想要的东西。

public class Foo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame jf = new JFrame();
                JPanel jp = new JPanel();
                JButton jb = new JButton("New Frame");
                jb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jf.setVisible(false);
                        new JFrame().setVisible(true);
                    }
                });
                jp.setLayout(new FlowLayout());
                jp.add(jb);
                jf.setContentPane(jp);
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jf.setVisible(true);
                jf.pack();
            }
        }
    }
}

The reason why I say cleaner at all, is because of the control of a LayoutManager. 我之所以说整洁,是因为LayoutManager的控制。 GUI builders like the one you use always tend to use GridBagLayout because it's easier for it to dynamically (from your user input) place things precisely. 像您使用的GUI生成器一样,总是会使用GridBagLayout,因为它可以更轻松地动态地(根据用户输入)精确地放置事物。 But the code it generates is unmanageable. 但是它生成的代码难以管理。 And things get worse when you consider that if you expand or contract your GUI, that the GridBagLayout is not flexible with this so now your Components are stuck exactly where you placed them when dragging them from the GUI Builder. 当您考虑到如果扩展或收缩GUI时,GridBagLayout并不灵活,因此情况会变得更糟,因此,当从GUI Builder中拖动它们时,您的组件正好卡在放置它们的位置。 Proper use of the correct LayoutManager will give you a flexible and manageable GUI. 正确使用正确的LayoutManager将为您提供灵活且易于管理的GUI。 Check out this Oracle guide for more info and help: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 查阅此Oracle指南以获取更多信息和帮助: https : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

That is because you are disposing the parent frame before showing the new one. 那是因为您要在显示新框架之前布置父框架。

In this scenario your parent frame, from which you are calling 在这种情况下,您从中调用的父框架

new JavaFrame2().SetVisibility(true);

contains the handle to the new JavaFrame. 包含新JavaFrame的句柄。 By disposing the parent frame you technically delete the parent -> you cant do something with it any more. 通过放置父框架,您从技术上删除了父框架->您无法再对其进行任何操作。

Try hiding the startscreen by calling .SetVisibility(false) instead of this.dispose(); 尝试通过调用.SetVisibility(false)而不是this.dispose();隐藏启动屏幕this.dispose(); .

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

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