Java JFrame 删除和重绘组件不起作用

标签 java jframe

所以我开始进入 OOP 并开始学习 swing 库,但我遇到了麻烦。当我尝试删除所有 JFrame 组件时,它不起作用。我想要做的是,当用户单击按钮时,我必须删除所有 JFrame 组件并添加新组件,但尽管我使用了removeAll() repait()、revalidate() 等,但它不起作用。这是我的代码对于 BankApp 类:

import javax.swing.*;

public class BankApp{

public static void main(String[] args) {
    BankGUI object1;
    object1 = new BankGUI();
    object1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    object1.setSize(700, 500);
    object1.setLocationRelativeTo(null);
    object1.setResizable(false);
    object1.setVisible(true);

}

}

这是 BankGUI:

import javax.swing.*;
import java.awt.*;

public class BankGUI extends JFrame{

private JButton CreateAccount;
private JButton LoginAccount;
private JButton Exit;
private JButton AboutButton;
private JButton ExitButton;
private JLabel IntroText;


public BankGUI(){
    super("Banking App");
    createMainMenu();

}

public void createMainMenu(){
    add(Box.createRigidArea(new Dimension(0,40)));
    IntroText = new JLabel("Banking Application");
    IntroText.setMaximumSize(new Dimension(280,60));
    IntroText.setFont(new Font("Serif", Font.PLAIN, 34));
    IntroText.setAlignmentX(CENTER_ALIGNMENT);
    add(IntroText);

    add(Box.createRigidArea(new Dimension(0,40)));

    setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
    CreateAccount = new JButton("Register");
    CreateAccount.setMaximumSize(new Dimension(200,50));
    CreateAccount.setFont(new Font("Serif", Font.PLAIN, 24));
    CreateAccount.setAlignmentX(CENTER_ALIGNMENT);
    CreateAccount.setFocusable(false);
    add(CreateAccount);

    add(Box.createRigidArea(new Dimension(0,20)));

    LoginAccount = new JButton("Login");
    LoginAccount.setMaximumSize(new Dimension(200,50));
    LoginAccount.setFont(new Font("Serif", Font.PLAIN, 24));
    LoginAccount.setAlignmentX(CENTER_ALIGNMENT);
    LoginAccount.setFocusable(false);
    add(LoginAccount);

    add(Box.createRigidArea(new Dimension(0,20)));

    AboutButton = new JButton("About");
    AboutButton.setMaximumSize(new Dimension(200,50));
    AboutButton.setFont(new Font("Serif", Font.PLAIN, 24));
    AboutButton.setAlignmentX(CENTER_ALIGNMENT);
    AboutButton.setFocusable(false);
    add(AboutButton);

    add(Box.createRigidArea(new Dimension(0,20)));

    ExitButton = new JButton("Exit");
    ExitButton.setMaximumSize(new Dimension(200,50));
    ExitButton.setFont(new Font("Serif", Font.PLAIN, 24));
    ExitButton.setAlignmentX(CENTER_ALIGNMENT);
    ExitButton.setFocusable(false);
    add(ExitButton);

    ButtonListener actionListener = new ButtonListener(CreateAccount, LoginAccount, AboutButton, ExitButton);
    CreateAccount.addActionListener(actionListener);
    LoginAccount.addActionListener(actionListener);
    AboutButton.addActionListener(actionListener);
    ExitButton.addActionListener(actionListener);
}
}

这是我的 ButtonListener 类:

import java.awt.*;
import javax.swing.*;

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

public class ButtonListener implements ActionListener{
private JButton CreateAccount;
private JButton LoginAccount;
private JButton AboutButton;
private JButton ExitButton;


public ButtonListener(JButton button1,JButton button2,JButton button3,JButton button4){
    CreateAccount = button1;
    LoginAccount = button2;
    AboutButton = button3;
    ExitButton = button4;
}
@Override
public void actionPerformed(ActionEvent e) {
    BankGUI bankgui = new BankGUI();
    if(e.getSource() == CreateAccount){
        System.out.println("This prints out");
    }else if(e.getSource() == ExitButton){
        bankgui.getContentPane().removeAll();
        bankgui.removeAll();
        bankgui.validate();
        bankgui.repaint();
        System.out.println("Code reaches here but it doesnt clear the screen");
    }
}


}

当我尝试这样做时,尽管我重新验证并重新绘制,但什么也没有发生,我不确定为什么。我只是想清除 JFrame。我 100% 确定代码到达了方法,但由于某种原因它们不起作用。 也许这是明显的错误,但我没有看到它。 任何帮助将不胜感激。

最佳答案

在您的 actionPerformed 方法中,您正在创建 BankGUI另一个实例,这绝不是连接到的实例已显示给用户...

@Override
public void actionPerformed(ActionEvent e) {
    BankGUI bankgui = new BankGUI();
    //...
}

有几种方法“可以”纠正这个问题,但大多数都不太好。

您可以将 BankGUI 的引用与按钮一起传递给 ButtonListener。我不喜欢这个想法,因为它为 ButtonListener 提供了开始对 BankGUI 执行操作的方法,它实际上没有责任这样做。

您可以使用SwingUtilities.getWindowAncestor,传递被触发的按钮的引用,以获取对窗口的引用。这与之前的想法有同样的问题,但它也对 UI 的结构做出了假设,而 ButtonListener 不应该关心这一点。

在这两种情况下,都会将 ButtonListener 耦合到 BankGUI

更好的解决方案是提供某种“导航管理器”。 ButtonListener 将获取它的引用,并且当其中一个按钮被操作时,将通知“导航管理器”,要求其执行实际任务。

这将 ButtonListener 与 UI 的实现分离。

这都是关于“责任隔离”、“代码可重用性”和“模型- View - Controller ”的概念

总的来说,一个更简单、更实用的解决方案是使用 CardLayout ,这将进一步解耦 UI,更容易将功能与其自己的类/组件隔离,并允许主 UI 在它们之间切换

import java.awt.CardLayout;
import static java.awt.Component.CENTER_ALIGNMENT;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JavaApplication101 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                BankGUI object1;
                object1 = new BankGUI();
                object1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                object1.pack();
                object1.setLocationRelativeTo(null);
                object1.setVisible(true);
            }
        });
    }

    public interface BankNavigationController {

        public void setView(String command);
    }

    public static class CardLayoutBankNavigationController implements BankNavigationController {

        private Container parent;
        private CardLayout layout;

        public CardLayoutBankNavigationController(Container parent, CardLayout layout) {
            this.parent = parent;
            this.layout = layout;
        }

        @Override
        public void setView(String command) {
            layout.show(parent, command);
        }

    }

    public static class BankGUI extends JFrame {

        private CardLayoutBankNavigationController controller;

        public BankGUI() {
            super("Banking App");
            CardLayout layout = new CardLayout();
            setLayout(layout);
            controller = new CardLayoutBankNavigationController(getContentPane(), layout);

            add("Main Menu", createMainMenu());
            add("Register", otherView("Register"));
            add("Login", otherView("Login"));
            add("About", otherView("About"));
            add("Exit", otherView("Exit"));

            controller.setView("Main Menu");
        }

        public JPanel otherView(String named) {
            JPanel view = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            view.add(new JLabel(named), gbc);

            JButton mainMenu = new JButton("Main Menu");
            view.add(mainMenu, gbc);

            ButtonListener actionListener = new ButtonListener(controller);
            mainMenu.addActionListener(actionListener);

            return view;
        }

        public JPanel createMainMenu() {
            JPanel menu = new JPanel();
            menu.setLayout(new BoxLayout(menu, BoxLayout.PAGE_AXIS));

            menu.add(Box.createRigidArea(new Dimension(0, 40)));
            JLabel IntroText = new JLabel("Banking Application");
            IntroText.setMaximumSize(new Dimension(280, 60));
            IntroText.setFont(new Font("Serif", Font.PLAIN, 34));
            IntroText.setAlignmentX(CENTER_ALIGNMENT);
            menu.add(IntroText);

            menu.add(Box.createRigidArea(new Dimension(0, 40)));

            JButton CreateAccount = new JButton("Register");
            CreateAccount.setMaximumSize(new Dimension(200, 50));
            CreateAccount.setFont(new Font("Serif", Font.PLAIN, 24));
            CreateAccount.setAlignmentX(CENTER_ALIGNMENT);
            CreateAccount.setFocusable(false);
            menu.add(CreateAccount);

            menu.add(Box.createRigidArea(new Dimension(0, 20)));

            JButton LoginAccount = new JButton("Login");
            LoginAccount.setMaximumSize(new Dimension(200, 50));
            LoginAccount.setFont(new Font("Serif", Font.PLAIN, 24));
            LoginAccount.setAlignmentX(CENTER_ALIGNMENT);
            LoginAccount.setFocusable(false);
            menu.add(LoginAccount);

            menu.add(Box.createRigidArea(new Dimension(0, 20)));

            JButton AboutButton = new JButton("About");
            AboutButton.setMaximumSize(new Dimension(200, 50));
            AboutButton.setFont(new Font("Serif", Font.PLAIN, 24));
            AboutButton.setAlignmentX(CENTER_ALIGNMENT);
            AboutButton.setFocusable(false);
            menu.add(AboutButton);

            menu.add(Box.createRigidArea(new Dimension(0, 20)));

            JButton ExitButton = new JButton("Exit");
            ExitButton.setMaximumSize(new Dimension(200, 50));
            ExitButton.setFont(new Font("Serif", Font.PLAIN, 24));
            ExitButton.setAlignmentX(CENTER_ALIGNMENT);
            ExitButton.setFocusable(false);
            menu.add(ExitButton);

            ButtonListener actionListener = new ButtonListener(controller);
            CreateAccount.addActionListener(actionListener);
            LoginAccount.addActionListener(actionListener);
            AboutButton.addActionListener(actionListener);
            ExitButton.addActionListener(actionListener);

            return menu;
        }
    }

    public static class ButtonListener implements ActionListener {

        private BankNavigationController controller;

        public ButtonListener(BankNavigationController controller) {
            this.controller = controller;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            controller.setView(e.getActionCommand());
        }

    }

}

关于Java JFrame 删除和重绘组件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816130/

相关文章:

java - 如何自定义 JFileChooser

java - Findbugs Java 忽略字段或行

java - JMS 消耗多个主题

java - 如何从 Phonegap 插件更改 native 元素?

java - Java GUI 中的图片更改

java - 带有单选按钮的 JFrame 布局

c# - C# 中的单例模式

java - Hibernate 在插入子对象时插入重复的父对象

java - 使用一个类创建并运行多个 Java 窗口

java - 使用 e.getsource 打开一个新窗口(JFrame)