当前位置: 首页 > 知识库问答 >
问题:

Java JFrame删除并重新绘制不工作的组件

东郭源
2023-03-14

所以我开始学习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%确定代码到达了方法,但由于某种原因,它们不起作用。也许这是明显的错误,但我没有看到。任何帮助都将不胜感激。

共有1个答案

皇甫乐
2023-03-14

在您的操作执行方法中,您正在创建另一个BankGUI实例,这与显示给用户的实例没有任何联系...

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

有几种方法可以“纠正”这一点,其中大多数都不漂亮。

您可以将BankGUI的引用和按钮一起传递给ButtonListener。我不喜欢这个想法,因为它为ButtonListener提供了开始对BankGUI做事情的方法,它真的没有责任这样做。

你可以使用SwingUtilities。GetWindowSenator,传递已触发按钮的引用,以获取对窗口的引用。这与前面的想法一样存在sam问题,但它也对UI的结构进行了假设,ButtonListener不应该关心它。

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

一个更好的解决方案是提供某种“导航管理器”。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());
        }

    }

}
 类似资料:
  • 问题内容: 我已经建立了俄罗斯方块游戏。现在,我已经使用JPanel来显示内容和块(使用paintComponents()方法)。 问题是,当我尝试从另一个JFrame调用tetris程序时,它根本无法绘制。 我的俄罗斯方块主菜单的代码是: 当调用MatrixBoard的构造函数时,俄罗斯方块游戏会在新窗口中开始。但是,这些块在屏幕上不可见。MatrixBoard的代码是: 请帮忙。我怀疑问题出在

  • 正如标题所示,我无法使用delete()选项。我在网上发了很多帖子,但都找不到正确的答案。我正在Homestead上使用Laravel5.5(一周前安装,最新版本左右)。 让我给你一些代码,我真的希望有人能帮助我。 这让我头痛,奥氮平快用完了。请告诉我我做错了什么,如果遗漏了什么,请告诉我! 这是我的控制器: 这是我的索引页(索引与后端的管理索引相同: 然后是路线: 然后政策: 最后是中间件: 在

  • 我试图绘制一个矩形,它的位置每秒钟更新一次,因为我有一个类,它扩展了JPanel,在它里面我重写了油漆(或油漆组件)function_我已经尝试了这两个_但显然这个函数只被调用一次,正如下面的代码所示,当我试图用reaint函数在无限循环中调用它时,它没有被调用,我能做什么? 上面的代码是图形部分,下面是主函数,它位于另一个类中: 在上面的代码中,食物。加法器是矩形位置更新的地方,我已经检查过了,

  • 我想重新绘制我的屏幕。到目前为止,它所做的只是在第一个屏幕上的头部应该在的地方显示一个点。这很好,但是我在代码中写了我想每秒将头部向下移动10个像素。我正在打印头部应该在的位置,在命令提示符中它显示y值确实在增加。但是在我的屏幕上,头部没有移动。 我尝试过使用revalidate方法,尝试扩展canvas类而不是jframe,我尝试过只为paint方法使用不同的类,我尝试过用paintCompon

  • 我计划使用python库“Plotly”构建甘特图。具体而言:https://plotly.com/python/gantt/#group-一起完成任务。 然而,每个“作业”可以有多个任务,并且这些任务可以并行运行。从我观察到的情况来看,Plotly并没有将并行运行的任务堆叠在一起,这使得读取图表非常困难。下面是一个示例,“作业A”有两个并行运行的任务,但只有一个可见: 我想要的是“作业A”任务都

  • 我一直在尝试运行一些单元测试,这些测试使用to_char函数,该函数适用于我们的oracle数据库,但对于我们的h2内存数据库,用于测试的函数似乎不起作用。我尝试将别名删除到_char并覆盖它以使其生效,但当我尝试将别名删除到_char并创建它时,它似乎不起作用。 结果:"org.h2.jdbc.JdbcSQLException:函数别名"TO_CHAR"已经存在;SQL语句:" 结果:"org.