简体   繁体   English

Java JFrame根本无法关闭Frame

[英]Java JFrame can't close at all a Frame

I have a problem with JFrame. 我的JFrame有问题。 All I want to do is to create a JFrame for Login with a Button, and when the Button is pressed: it close the Login Frame and opens the Program Frame. 我要做的就是创建一个JButton,以便使用一个按钮登录,并在按下按钮时:关闭登录框架并打开程序框架。

This is my Login Frame: 这是我的登录框架:

public static void main(String[] args) {

    JFrame frame = new JFrame("My Program");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       LoginPanel primary = new LoginPanel(frame);

        frame.setPreferredSize (new Dimension (650, 500));
        frame.getContentPane().add(primary);
        frame.pack();
        frame.setVisible(true);

}

Which opens the Login Panel by passing the Frame in the constructor, the Login Panel: 通过在构造函数“登录面板”中传递框架来打开“登录面板”:

public class LoginPanel extends JPanel {

JFrame fr;

class submitButton implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        ProgramFrame programFrame = new ProgramFrame();
        programFrame.setVisible(true);
        fr.setVisible(false);
        fr.dispose();
    }
  }

public LoginPanel(JFrame frame) {

    fr = frame;

    JButton submit = new JButton("Button Login");
    submit.addActionListener(new submitButton());
    add(submit);    
}

This is the problem: 这就是问题:

When I click on the Button "Button Login" of the LoginPanel, it succesfully opens the new ProgramFrame but it doesn't close at all the old frame (LoginFrame). 当我单击LoginPanel的“按钮登录”按钮时,它成功打开了新的ProgramFrame,但它并没有完全关闭旧框架(LoginFrame)。 The LoginFrame becomes smaller, very little, but remains: LoginFrame变得很小,很小,但是仍然保持:

在此处输入图片说明

Thanks in advance for the help! 先谢谢您的帮助! :) :)

class submitButton implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        ProgramFrame programFrame = new ProgramFrame();
        programFrame.setVisible(true);
        this.dispose(); //changed line
    }
  }

well your panel is closed bt the jframe is still opened without the jpanel i have made some changes to your code now both will disposed at the same time 好吧,您的panel已关闭bt jframe仍然没有jpanel我已经对您的代码进行了一些更改,现在两者都将同时处置

You first initialise JFrame completely, so after inside JButton click event first hide JFrame later dispose it. 您首先要完全初始化JFrame ,所以在JButton单击事件之后,先隐藏JFrame然后再处置它。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class HideLoginPage{

    public static void main(String[] args){
        HideLoginPage loginPage = new HideLoginPage();

        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setBounds(200, 200, 200, 100);
        loginPage.setPane(frame);
        frame.setVisible(true);
    }

    public void setPane(final JFrame frame){
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        JButton submit = new JButton("Login");
        submit.setSize(100, 30);
        panel.add(submit);

        submit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                JFrame newFrame = new JFrame();
                newFrame.setBounds(400, 200, 400, 400);
                newFrame.setVisible(true);

                frame.setVisible(false);
                frame.dispose();
            }
        });

        frame.getContentPane().add(panel);
    }
}

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

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