单击按钮后在Java中隐藏框架 [英] Hiding a frame in java upon a button click

查看:99
本文介绍了单击按钮后在Java中隐藏框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮后,我试图隐藏框架. 注册"按钮应打开一个用户可以在其中注册的框架,并且可以正常工作,但是我试图隐藏上一个框架,但我不知道该怎么做.

I am trying to hide a frame once a button is clicked. The "Register" button should open up a frame where a user can register, and that works, but I am trying to hide the previous frame and I can't figure out how to do it.

这是我的代码:

MainPage.java

MainPage.java

package Practice_1;
import java.awt.*;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
 *
 * @author Ivan 
 */
public class MainPage extends JPanel 
{
    JButton regButton, logButton, listButton;
    JLabel homeMessage;
    GridBagConstraints gbc = new GridBagConstraints();

    public MainPage()
    {
        setLayout(new GridBagLayout());

        gbc.insets = new Insets(5,5,5,5);

        homeMessage = new JLabel("Please select an option below:");
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(homeMessage, gbc);

        regButton = new JButton("Register");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        regButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                //System.out.println("clicked");
                RegisterPage regFrame = new RegisterPage();

                JFrame register = new JFrame();
                register.setTitle("Registration");
                register.setSize(300,200);
                register.setVisible(true);
                register.add(regFrame);
                new MainPage().setVisible(false);  / THIS DOES NOT WORK

            }
        });
        add(regButton, gbc);

        logButton = new JButton("Log in");
        gbc.ipadx = 40;
        gbc.gridx = 0;
        gbc.gridy = 2;
        add(logButton, gbc);

        listButton = new JButton("Customer list");
        gbc.ipadx = 40;
        gbc.gridx = 0;
        gbc.gridy = 3;
        add(listButton, gbc);

        JFrame home = new JFrame();

        home.setTitle("Main menu");
        home.setSize(300,200);
        home.setResizable(false);
        home.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        home.setVisible(true);
        home.add (mainFrame);


    }




    public static void main(String[] args) {
        // TODO code application logic here
        MainPage mainFrame = new MainPage();





    }
}

RegisterPage.java

RegisterPage.java

package Practice_1;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Ivan
 */
public class RegisterPage extends JPanel {


    //JButton regButton, logButton, listButton;
    JLabel homeMessage;
    GridBagConstraints gbc = new GridBagConstraints();

    public RegisterPage()
    {
        setLayout(new GridBagLayout());

        gbc.insets = new Insets(5,5,5,5);

        homeMessage = new JLabel("Register new user:");
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(homeMessage, gbc);


    }

}

推荐答案

根据您的代码,您尝试隐藏JPanel而不是JFrame.我的建议是使用

Acording to your code you are trying to hide JPanel instead of JFrame. My suggestion is to use

public class MainPage extends JFrame

而不是像这样实例化您的JPanel:

than instantiate your JPanel like this:

JPanel panel = new JPanel();

并在该面板上添加组件.要隐藏您的主页,您可以调用:

and to add components on that panel. To hide your MainPage you can call:

this.setVisible(false);

但是如果您使用以下方法会更好:

but it would be better if you use:

this.dispose();

这篇关于单击按钮后在Java中隐藏框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆