简体   繁体   English

使用JButton访问另一个类中的另一个框架

[英]Using JButton to access Another Frame in another class

Hi everyone I'm new to java. 大家好,我是Java新手。 I'm trying to redirect my "Next" JButton to another frame (DriversLicenseApplicant) in another class. 我正在尝试将我的“下一个” JButton重定向到另一个类中的另一个框架(DriversLicenseApplicant)。 The problem is when I click "Next" button it kept on opening the frame which my program is currently executing which is MainInfo. 问题是,当我单击“下一步”按钮时,它将持续打开我的程序当前正在执行的框架MainInfo。 Can you help me with my code? 您能帮我提供我的代码吗? thank you all. 谢谢你们。 :) :)

    final JButton btnNext = new JButton("Next");
    btnNext.setBounds(824, 623, 89, 51);
    contentPane.add(btnNext);
        btnNext.addActionListener(
                new ActionListener(){


        public void actionPerformed(ActionEvent e) {
            if (btnNext.isSelected()) {
            DriversLicenseApplicant frame = new DriversLicenseApplicant();
            MainInfo objMain = new MainInfo();
                    frame.setVisible(true);
                        objMain.setVisible(false);



        }
        }
    });

What is happening 怎么了

The key lines are here 关键线在这里

1  DriversLicenseApplicant frame = new DriversLicenseApplicant();
2  MainInfo objMain = new MainInfo();
3  frame.setVisible(true);
4  objMain.setVisible(false);

Before this block, you have one frame this . 在此块之前,您有一帧this (In Java, you refer to the object you're in as this .) this is open. (在Java中,你指的是你所处的对象this )。 this是开放的。

At line 1, you create a new frame frame , bringing your total frames to two. 在第1行,您创建了一个新的frame ,将总框架增加到两个。 frame is hidden. frame被隐藏。

At line 2, you create a third frame objMain . 在第2行,创建第三个框架objMain It is in the same class as this but it's a different instance. 它是在同一个班this ,但它是一个不同的实例。 (Explanations of the difference between Class and Instance can be found on Stack and on Oracle's website ). (有关类和实例之间的区别的说明,请参见StackOracle网站 )。 objMain is hidden, but this is open. objMain是隐藏的,但是this是开放的。

At line 3, you open frame . 在第3行,打开frame Now both this and frame are open, while objMain is hidden. 现在, thisframe都打开了,而objMain被隐藏了。 this keeps focus because of Java's focus rules . 由于Java的关注规则, this可以保持关注。

At line 4, you hide objMain . 在第4行,隐藏objMain This has no effect because objMain was never open in the first place. 这没有效果,因为objMain从来没有打开过。 You still have 3 frames, both this and frame are open, and this still has focus. 你还有3张,双方thisframe是开放的, this仍然具有焦点。

How to fix it 如何修复

Replacing those lines with 将这些行替换为

DriversLicenseApplicant frame = new DriversLicenseApplicant();
frame.setVisible(true);
this.setVisible(false);

will open the new frame and hide to current one. 将打开新框架并隐藏到当前框架。

But I think there might be an underlying design issue. 但我认为可能存在潜在的设计问题。 Each program should normally have only one frame. 每个程序通常应该只有一帧。 You might want to look into using a JDialog for the second window. 您可能要考虑在第二个窗口中使用JDialog You could also keep them both in the same frame and switch between them using a CardLayout . 您也可以将它们放在同一帧中,并使用CardLayout在它们之间切换。 Also change your classes to extend JPanel , so you can put them in the content pane of your windows. 还可以更改类以扩展JPanel ,因此可以将它们放在窗口的内容窗格中。 It adds flexibility, and it's useful for whichever method you choose. 它增加了灵活性,并且对于您选择的任何一种方法都很有用。

One more thing 还有一件事

I can't help mentioning the absolute positioning of the JButton. 我不禁提到JButton的绝对位置。 Please use layouts . 请使用布局 They are your friends. 他们是你的朋友。

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

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