gpt4 book ai didi

java - 在 Swing 应用程序中仅显示一个选定或请求的 jpanel

转载 作者:行者123 更新时间:2023-12-04 04:45:13 25 4
gpt4 key购买 nike

我正在swing申请。我有 20 多个 jpanel。我想显示我想要显示的那个 jpanel :

例如:

WelcomePanel welcomePanel = new WelcomePanel();
BindingDetailsPanel bindingDetailsPanel = new BindingDetailsPanel();
FooterMainPanel footerMainPanel = new FooterMainPanel();
KeyMessageSetupPanel keyMessageSetupPanel = new KeyMessageSetupPanel();
SMSCSetupPanel sMSCSetupPanel = new SMSCSetupPanel();
LiveProcessorDetailsPanel runTimePanel = new LiveProcessorDetailsPanel();
RuntTimeInformationPanel welcomePanel=new RuntTimeInformationPanel();
BlockedSmscDetailsPanel blockedSmscDetailsPanel = new BlockedSmscDetailsPanel();

我正在我的主 jFrame 中制作上面列出的面板的对象。它工作正常

因为 :
public mainFrame(){
requestResponseReportPanel.setVisible(false);
keyMessageSetupPanel.setVisible(false);
sMSCSetupPanel.setVisible(false);
welcomePanel.setVisible(true);
keyMessageDetailsPanel.setVisible(false);
blockedSmscDetailsPanel.setVisible(false);
runTimePanel.setVisible(false);
}

我的应用程序第一次启动时,它只会显示一个面板欢迎面板,我将其设置为可见 welcomePanel.setVisible(true);我有菜单,如果我点击它应该显示 keyMessageSetupPanel以下是显示此面板的代码:
private void keyMessageSetupMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                        
try {
mainFrameContentPanel.add(keyMessageSetupPanel, BorderLayout.CENTER);
keyMessageSetupPanel.setVisible(true);
sMSCSetupPanel.setVisible(false);
welcomePanel.setVisible(false);
homeButton.setVisible(true);
bindingDetailsPanel.setVisible(false);
requestResponseReportPanel.setVisible(false);
keyMessageDetailsPanel.setVisible(false);
blockedSmscDetailsPanel.setVisible(false);
} catch (Exception e) {
printStackTrace();
}
}

我的问题是,如果我这样使用,我的应用程序正在运行。但它在运行时加载所有面板并且需要很长时间才能加载。如果我不喜欢这个,它会显示 welcomePanel如果我通过单击( menubutton )进入另一个面板:

示例 :
  • 欢迎面板显示
  • 我去了keyMessageSetupPanel (这工作正常)
  • 现在我去了bindingDetailsPanel (有效)
  • 但问题是我想再次keyMessageSetupPanel (它不会隐藏 bindingDetailsPanel )。

  • 已编辑 这是显示 bindingDetailsPanel 的代码
    mainFrameContentPanel.add(bindingDetailsPanel, BorderLayout.CENTER);
    bindingDetailsPanel.setVisible(true);
    keyMessageSetupPanel.setVisible(false);
    sMSCSetupPanel.setVisible(false);
    welcomePanel.setVisible(false);
    homeButton.setVisible(true);
    requestResponseReportPanel.setVisible(false);
    keyMessageDetailsPanel.setVisible(false);
    blockedSmscDetailsPanel.setVisible(false);

    请帮帮我。
    备注 如果您在不理解我的问题的地方不理解评论,我会尽力解释。

    谢谢

    最佳答案

    您的代码不起作用的基本原因是您没有调用 revalidate()repaint()在进行交换后保存交换 JPanel 的容器上。这些调用告诉布局管理器重新布局所持有的组件,并告诉重绘管理器重绘容器及其子项。

    话虽如此,在容器中交换可见组件的更好方法是使用为此明确构建的 CardLayout。

    例如,请看这里:https://stackoverflow.com/a/6176115/522444

    对于上述程序的另一个示例,这次有一些更好的关注点分离:

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.List;

    import javax.swing.*;

    class MainClass implements ReceiveAction {
    private JPanel mainPanel = new JPanel();
    private JMenuBar menuBar = new JMenuBar();
    private JMenu chooseViewMenu = new JMenu("Choose View");

    public MainClass() {
    Registration registration = new Registration();
    ButtonPanel buttonPanel = new ButtonPanel();

    ReceiveAction[] recActions = {this, buttonPanel};
    new ViewSwapControl(registration, recActions);

    buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
    registration.setBorder(BorderFactory.createTitledBorder("Registration Panel"));

    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(registration, BorderLayout.CENTER);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    menuBar.add(chooseViewMenu);
    }

    public JComponent getMainPanel() {
    return mainPanel;
    }

    public JMenuBar getMenuBar() {
    return menuBar ;
    }

    @Override
    public void addAction(Action action) {
    chooseViewMenu.add(new JMenuItem(action));
    }

    private static void createAndShowUI() {
    MainClass mainInstance = new MainClass();


    JFrame frame = new JFrame("Registration");
    frame.getContentPane().add(mainInstance.getMainPanel());
    frame.setJMenuBar(mainInstance.getMenuBar());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    createAndShowUI();
    }
    });
    }

    }

    class ViewSwapControl {
    private Registration registration;
    private List<Action> actionList = new ArrayList<>();

    public ViewSwapControl(Registration registration, ReceiveAction... recActions) {
    this.registration = registration;
    for (final String keyText : Registration.KEY_TEXTS) {
    actionList.add(new MyAction(keyText));
    }
    for (ReceiveAction receiveAction : recActions) {
    for (Action action : actionList) {
    receiveAction.addAction(action);
    }
    }
    }

    @SuppressWarnings("serial")
    private class MyAction extends AbstractAction {
    public MyAction(String name) {
    super(name);
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
    if (registration != null) {
    registration.swapView(getValue(NAME).toString());
    }
    }
    }

    }

    interface ReceiveAction {
    public void addAction(Action action);
    }

    @SuppressWarnings("serial")
    class ButtonPanel extends JPanel implements ReceiveAction {

    public ButtonPanel() {
    setLayout(new GridLayout(1, 0, 10, 0));
    }

    @Override
    public void addAction(Action action) {
    add(new JButton(action));
    }
    }

    @SuppressWarnings("serial")
    class Registration extends JPanel {
    // use these same constants as button texts later
    private static final Dimension PREF_SIZE = new Dimension(450, 300);
    public static final String USER_AGREEMENT = "User Agreement";
    public static final String USER_INFO = "User Information";
    public static final String ENROLLMENT = "Enrollment";
    // we'll extract them from this array
    public static final String[] KEY_TEXTS = {USER_AGREEMENT, USER_INFO, ENROLLMENT};
    private CardLayout cardlayout = new CardLayout();
    private JPanel cards = new JPanel(cardlayout);

    public Registration() {
    cards.add(createUserAgreePanel(), USER_AGREEMENT);
    cards.add(createUserInfoPanel(), USER_INFO);
    cards.add(createEnrollmentPanel(), ENROLLMENT);
    setLayout(new BorderLayout());
    add(cards, BorderLayout.CENTER);
    }

    @Override
    public Dimension getPreferredSize() {
    return PREF_SIZE;
    }

    private JPanel createEnrollmentPanel() {
    JPanel enrol = new JPanel();
    enrol.add(new JLabel("Enrollment"));
    return enrol;
    }

    private JPanel createUserAgreePanel() {
    JPanel userAgree = new JPanel();
    userAgree.add(new JLabel("User Agreement"));
    return userAgree;
    }

    private JPanel createUserInfoPanel() {
    JPanel userInfo = new JPanel();
    userInfo.add(new JLabel("User Information"));
    return userInfo;
    }

    public void swapView(String key) {
    cardlayout.show(cards, key);
    }

    }

    关于java - 在 Swing 应用程序中仅显示一个选定或请求的 jpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299327/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com