所以我开始进入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%确定代码到达方法但由于某种原因它们不起作用。 也许是明显的错误,但我没有看到它。 任何帮助将不胜感激。
答案 0 :(得分:1)
在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());
}
}
}