所以我开始进入OOP,也开始学习秋千库,但是遇到了麻烦。当我尝试删除所有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,传递已触发按钮的引用,以获得对窗口的引用。与先前的想法一样,这有一个sam问题,但它也对UI的结构进行了假设,而ButtonListener则不必在意。

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

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

这将ButtonListener与UI的实现分离。

这都是关于“责任隔离”,“代码可重用性”和“模型-视图-控制器”的概念

总体而言,更简单,更实用的解决方案是使用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());
        }

    }

}

10-06 06:13