窗口上的 Java Swing pack() 不会最大化它,如何避免这种情况?

发布于 2024-12-01 16:58:41 字数 176 浏览 1 评论 0 原文

我有一个窗口,因为我动态地更改了它的子窗口(有时我交换 JPanel),所以除了在窗口上调用 pack() 来显示新元素之外,我没有找到其他解决方案。否则,只有当我手动调整窗口大小时它才会显示。

这样做的问题是,如果窗口最大化,在 pack() 之后它将不再最大化,这不是我可以给客户端的。

有什么线索吗?

I've a window and since I dinamically change it's children (sometimes I swap JPanels), I found no other solution than calling pack() on the window to get the new element displayed. Otherwise it will show up only if I resize manually the window.

The problem with this is that the if the window is maximized, after pack() it won't be anymore, which is not what I could give to the client.

Any clues?

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

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

发布评论

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

评论(4

书间行客 2024-12-08 16:58:41

首先,我希望您使用 CardLayout 用于面板交换,因为此功能内置于特定的布局管理器中。通常,您需要调用 验证/重新验证repaint 在容器上刷新显示。

另请参阅:

First of all, I hope that you're using CardLayout for panel swapping, since this functionality is built into that particular layout manager. And typically, you'll want to invoke validate/revalidate and repaint on the container to refresh the display.

See also:

半衾梦 2024-12-08 16:58:41

如果你真的必须:

int state = frame.getExtendedState();
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

...否则@mre的解决方案会(好多)更好! :)

If you really have to:

int state = frame.getExtendedState();
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

... Otherwise @mre's solution is (much) better! :)

油饼 2024-12-08 16:58:41

我遇到了和你一样的问题,对我来说,我对我的解决方案很满意,我分享它可能对你的上下文有帮助

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Housseyn
 */
public class JPanelTools {

public static final void ShowPanel(JPanel target, JPanel object) {
    target.removeAll();

    Dimension size = object.getSize();
    size.setSize(size.width, target.getHeight());
    target.setSize(object.getSize());

    GridBagLayout gridBagLayout = new GridBagLayout();

    target.setLayout(gridBagLayout);

    GridBagConstraints gbc = new GridBagConstraints(target.getX(),
            target.getY(),
            target.getWidth(),
            target.getHeight(),
            0, 0,
            GridBagConstraints.ABOVE_BASELINE,
            0,
            new Insets(5, 5, 5, 5),
            0, 0);

    target.add(object, gbc);
    target.invalidate();
    target.revalidate();
    target.validate();
    target.repaint();
    target.show();
    object.validate();
    object.repaint();
    object.show();

    Container Frame = target.getParent();
    Container Current = target.getParent();
    while ((Current != null)) {
        System.out.println("current =" + Current.getClass().getName());
        Frame = Current;
        Current = Current.getParent();
    }
    System.out.println("frame " + Frame.getClass().getName());
    if (Frame != null) {
        System.out.println("pack");
        JFrame MyFrame = (JFrame) Frame;
        int extendedState = MyFrame.getExtendedState();
        if (extendedState != JFrame.MAXIMIZED_BOTH) {

            MyFrame.pack();
            MyFrame.setExtendedState(extendedState);
        }
    }
}

}

我在我的主框架上设计了一个空面板,并在一个按钮中我称之为它

  MyDesignedPanel myPanel = new MyDesignedPanel();
  JPanelTools.ShowPanel(JemptyPanel, myPanel);

,它非常适合我

I got the same problem as you and for me I m satisfied with my solution I share it may be it help you on your context

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Housseyn
 */
public class JPanelTools {

public static final void ShowPanel(JPanel target, JPanel object) {
    target.removeAll();

    Dimension size = object.getSize();
    size.setSize(size.width, target.getHeight());
    target.setSize(object.getSize());

    GridBagLayout gridBagLayout = new GridBagLayout();

    target.setLayout(gridBagLayout);

    GridBagConstraints gbc = new GridBagConstraints(target.getX(),
            target.getY(),
            target.getWidth(),
            target.getHeight(),
            0, 0,
            GridBagConstraints.ABOVE_BASELINE,
            0,
            new Insets(5, 5, 5, 5),
            0, 0);

    target.add(object, gbc);
    target.invalidate();
    target.revalidate();
    target.validate();
    target.repaint();
    target.show();
    object.validate();
    object.repaint();
    object.show();

    Container Frame = target.getParent();
    Container Current = target.getParent();
    while ((Current != null)) {
        System.out.println("current =" + Current.getClass().getName());
        Frame = Current;
        Current = Current.getParent();
    }
    System.out.println("frame " + Frame.getClass().getName());
    if (Frame != null) {
        System.out.println("pack");
        JFrame MyFrame = (JFrame) Frame;
        int extendedState = MyFrame.getExtendedState();
        if (extendedState != JFrame.MAXIMIZED_BOTH) {

            MyFrame.pack();
            MyFrame.setExtendedState(extendedState);
        }
    }
}

}

I designed an empty panel on my main Frame and in a button I call this

  MyDesignedPanel myPanel = new MyDesignedPanel();
  JPanelTools.ShowPanel(JemptyPanel, myPanel);

that works perfectly for me

哎呦我呸! 2024-12-08 16:58:41

这是我处理这个问题的新方法:

public static void showForms(JFrame frame,JPanel[] jPanels){
    for (JPanel jPanel : jPanels) {
        showForms(frame, jPanel,false);
    }
    int extendedState = frame.getExtendedState();
    if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
    frame.pack();
}

public static void showForms(JFrame frame, JPanel jPanel, boolean doPack) {
    jPanel.setVisible(true);
    if (doPack) {
         int extendedState = frame.getExtendedState();
        if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
        frame.pack();
    }
}

public static void hideForms(JFrame frame, JPanel[] jPanel) {
    for (JPanel panel : jPanel) {
        hideForms(frame, panel, false);
    }
     int extendedState = frame.getExtendedState();
    if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
    frame.pack();
}

public static void hideForms(JFrame frame, JPanel jPanel, boolean doPack) {
    jPanel.setVisible(false);
    if (doPack) {
         int extendedState = frame.getExtendedState();
        if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
        frame.pack();
    }
}

我使用这种方法在我的 jframe 上隐藏和显示 jpanels。

按钮代码上的示例

    JFrameTools.showForms(this,searchPanel,false);
    JFrameTools.showForms(this,insertingPanel,true);
    JFrameTools.showForms(this,new jPanel[]{insertingPanel,searchPanel,printingPanel});

与隐藏相同。

That my new way of handling this issue :

public static void showForms(JFrame frame,JPanel[] jPanels){
    for (JPanel jPanel : jPanels) {
        showForms(frame, jPanel,false);
    }
    int extendedState = frame.getExtendedState();
    if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
    frame.pack();
}

public static void showForms(JFrame frame, JPanel jPanel, boolean doPack) {
    jPanel.setVisible(true);
    if (doPack) {
         int extendedState = frame.getExtendedState();
        if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
        frame.pack();
    }
}

public static void hideForms(JFrame frame, JPanel[] jPanel) {
    for (JPanel panel : jPanel) {
        hideForms(frame, panel, false);
    }
     int extendedState = frame.getExtendedState();
    if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
    frame.pack();
}

public static void hideForms(JFrame frame, JPanel jPanel, boolean doPack) {
    jPanel.setVisible(false);
    if (doPack) {
         int extendedState = frame.getExtendedState();
        if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
        frame.pack();
    }
}

I m using this methods to hide and show jpanels on my jframe.

sample on a button code

    JFrameTools.showForms(this,searchPanel,false);
    JFrameTools.showForms(this,insertingPanel,true);
    JFrameTools.showForms(this,new jPanel[]{insertingPanel,searchPanel,printingPanel});

the same for the hiding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文