动态添加的 JPanel 未显示在 Swing 中

发布于 2024-12-05 15:31:54 字数 3801 浏览 1 评论 0原文

这是我的代码。显示以前设计的框架(添加了面板)时我没有任何问题。动态将面板添加到空 JFrame 时遇到此问题。

package com.mytunes.controllers;

import com.mytunes.views.*;
import com.mytunes.views.panels.*;
import java.awt.BorderLayout;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


public class GUIController {
    ArrayList<JFrame> displayFrames = new ArrayList<JFrame>();

    JPanel displayPanel;
    HeaderPanel headerPanel = new HeaderPanel();
    FooterPanel footerPanel = new FooterPanel();


    public int showFrame(String frameName, Object controller){
        Class c;
        Constructor ctr;
        int lastFrame = -1;

        try {

           // call a class dynamically
           c = Class.forName("com.mytunes.views.frames." + frameName + "Frame");
           // calll a constructor of a class
           ctr = c.getConstructor(SessionController.class);
           // instantiate dynamically created class
           displayFrames.add((JFrame) ctr.newInstance(controller));

           // get the index of frame just added
           lastFrame = displayFrames.size()-1;

           displayFrames.get(lastFrame).setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
           displayFrames.get(lastFrame).pack();
           displayFrames.get(lastFrame).setLocationRelativeTo(null);

           // hide it by default. 
           displayFrames.get(displayFrames.size()-1).setVisible(false);

        } catch (InstantiationException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error while loading class 'com.mytunes.view.frames." + frameName + "'" );
        }

        System.out.println(lastFrame);
        return lastFrame;

    }

    //-------------show panels --------------//
     public void showOperator(String frame, Object controller){

        // this works fine! this frame is already having Panels
        int f = showFrame(frame, controller);
        displayFrames.get(f).setVisible(true);
     }
    public void showEnterPIN(String frame, Object controller){


        int f = showFrame(frame, controller);
        displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
        displayFrames.get(f).setVisible(true);

        //        Try every following ways to show the "Dynamically added" panels
        //        displayFrames.get(f).getContentPane().removeAll();
        //        displayFrames.get(f).getContentPane().invalidate();
        //        displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.PAGE_START);
        //        displayFrames.get(f).getContentPane().validate();
        //        displayFrames.get(f).pack();
        //        displayFrames.get(f).setVisible(true);

    }

}

如果有人能为我解决这个问题,我将不胜感激。谢谢。

Here's my code. I have no problem when showing up the previously designed frames (with panels added). I get this issue when adding panels to an empty JFrame dynamically.

package com.mytunes.controllers;

import com.mytunes.views.*;
import com.mytunes.views.panels.*;
import java.awt.BorderLayout;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


public class GUIController {
    ArrayList<JFrame> displayFrames = new ArrayList<JFrame>();

    JPanel displayPanel;
    HeaderPanel headerPanel = new HeaderPanel();
    FooterPanel footerPanel = new FooterPanel();


    public int showFrame(String frameName, Object controller){
        Class c;
        Constructor ctr;
        int lastFrame = -1;

        try {

           // call a class dynamically
           c = Class.forName("com.mytunes.views.frames." + frameName + "Frame");
           // calll a constructor of a class
           ctr = c.getConstructor(SessionController.class);
           // instantiate dynamically created class
           displayFrames.add((JFrame) ctr.newInstance(controller));

           // get the index of frame just added
           lastFrame = displayFrames.size()-1;

           displayFrames.get(lastFrame).setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
           displayFrames.get(lastFrame).pack();
           displayFrames.get(lastFrame).setLocationRelativeTo(null);

           // hide it by default. 
           displayFrames.get(displayFrames.size()-1).setVisible(false);

        } catch (InstantiationException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error while loading class 'com.mytunes.view.frames." + frameName + "'" );
        }

        System.out.println(lastFrame);
        return lastFrame;

    }

    //-------------show panels --------------//
     public void showOperator(String frame, Object controller){

        // this works fine! this frame is already having Panels
        int f = showFrame(frame, controller);
        displayFrames.get(f).setVisible(true);
     }
    public void showEnterPIN(String frame, Object controller){


        int f = showFrame(frame, controller);
        displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
        displayFrames.get(f).setVisible(true);

        //        Try every following ways to show the "Dynamically added" panels
        //        displayFrames.get(f).getContentPane().removeAll();
        //        displayFrames.get(f).getContentPane().invalidate();
        //        displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.PAGE_START);
        //        displayFrames.get(f).getContentPane().validate();
        //        displayFrames.get(f).pack();
        //        displayFrames.get(f).setVisible(true);

    }

}

Appreciate if someone can spot this issue out for me. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软的没边 2024-12-12 15:31:54

如果将组件添加到容器中,则需要告诉容器的布局管理器通过调用 revalidate() 以及有时调用 repaint(); 来布局它们包含的所有组件> 在容器上。所以,就像...

JPanel contentPane = (JPanel)displayFrames.get(f).getContentPane();
contentPane.add(new EnterPINPanel(), BorderLayout.CENTER);
contentPane.revalidate();
contentPane.repaint();

话虽如此,您的应用程序设计看起来有点不同。您真的使用 JFrame 的 ArrayList 吗?大多数现实世界的应用程序不使用一堆单独的窗口,而是在一个主窗口中交换显示,您可以通过仅使用一个 JFrame 并通过 CardLayout 交换视图来使用 Swing 执行类似的操作。

编辑2
如果您尝试怎么办:

  int f = showFrame(frame, controller);
  JPanel contentPane = (JPanel)displayFrames.get(f).getContentPane();
  contentPane.removeAll();
  contentPane.setLayout(new BorderLayout()); // just to be sure
  contentPane.add(headerPanel, BorderLayout.PAGE_START);
  contentPane.add(new EnterPINPanel(), BorderLayout.CENTER);
  contentPane.add(footerPanel, BorderLayout.PAGE_END);
  contentPane.revalidate(); // *** note that it's **re**validate
  contentPane.repaint();
  displayFrames.get(f).setVisible(true);

同样,如果这没有帮助,那么您应该创建并发布“简短、独立、正确(可编译) ),示例”或 SSCCE

If you add a component to a container you need to tell the container's layout managers to layout all the components they contain by calling revalidate() and then by sometimes calling repaint(); on the container. So, something like...

JPanel contentPane = (JPanel)displayFrames.get(f).getContentPane();
contentPane.add(new EnterPINPanel(), BorderLayout.CENTER);
contentPane.revalidate();
contentPane.repaint();

Having said this, your application design looks a bit different. Do you really use an ArrayList of JFrames? Most real-world applications don't use a bunch of separate windows but rather swap displays in one main window, and you can do a similar thing with Swing by using just one JFrame and swap views via a CardLayout.

Edit 2
What if you try:

  int f = showFrame(frame, controller);
  JPanel contentPane = (JPanel)displayFrames.get(f).getContentPane();
  contentPane.removeAll();
  contentPane.setLayout(new BorderLayout()); // just to be sure
  contentPane.add(headerPanel, BorderLayout.PAGE_START);
  contentPane.add(new EnterPINPanel(), BorderLayout.CENTER);
  contentPane.add(footerPanel, BorderLayout.PAGE_END);
  contentPane.revalidate(); // *** note that it's **re**validate
  contentPane.repaint();
  displayFrames.get(f).setVisible(true);

Again, if this doesn't help, then you should create and post a "Short, Self Contained, Correct (Compilable), Example" or SSCCE

╰つ倒转 2024-12-12 15:31:54

这可行

    JFrame contentPane = new JFrame();     
    contentPane.getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
    contentPane.setVisible(true);

但不是这个

int f = <ArrayList index of last inserted Frame>
displayFrames.get(f).getContentPane().add(headerPanel, BorderLayout.PAGE_START);
displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
displayFrames.get(f).getContentPane().add(footerPanel, BorderLayout.PAGE_END);
displayFrames.get(f).getContentPane().validate();
displayFrames.get(f).getContentPane().repaint();
displayFrames.get(f).setVisible(true);

This works

    JFrame contentPane = new JFrame();     
    contentPane.getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
    contentPane.setVisible(true);

But not this

int f = <ArrayList index of last inserted Frame>
displayFrames.get(f).getContentPane().add(headerPanel, BorderLayout.PAGE_START);
displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
displayFrames.get(f).getContentPane().add(footerPanel, BorderLayout.PAGE_END);
displayFrames.get(f).getContentPane().validate();
displayFrames.get(f).getContentPane().repaint();
displayFrames.get(f).setVisible(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文