java - 点击按钮后隐藏java中的框架

标签 java swing button hide frame

我试图在单击按钮后隐藏框架。 “注册”按钮应该打开一个用户可以注册的框架,这很有效,但我试图隐藏前一个框架,但我不知道该怎么做。

这是我的代码:

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

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。我的建议是使用

public class MainPage extends JFrame

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

JPanel panel = new JPanel();

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

this.setVisible(false);

但如果你使用的话会更好:

this.dispose();

关于java - 点击按钮后隐藏java中的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13278137/

相关文章:

button - android中按钮下方的文字

java - 将 swing GUI 更改为处理 GUI

java - 如何创建可滚动的 JTable

cocoa - 如何将按钮的启用绑定(bind)与 NSArrayController 是否有选择绑定(bind)?

java - 单击鼠标交换按钮图标

java - 创建自定义表模型以提供 ArrayList 中的数据

ios - 删除以编程方式创建的 View iOS

java - 如何用 Java 开发 iPhone 应用程序?

java - 如何使用 codehaus 包类从 POJO 生成 JSON 架构时添加描述属性

java - mongodb + mockito 不能一起工作?