更新java布局管理器

发布于 2024-12-03 13:10:28 字数 1672 浏览 4 评论 0原文

我已经全局初始化了 GridBagLayout,然后在我的类构造函数中实例化了它并向其添加了一些按钮等。

事后我怎样才能向其中添加内容?简单类扩展了 JFrame。每当我在事后尝试 class.add(stuff, gridbagconstraints) (仅在构造函数中使用 add(stuff, gribagconstraints))时,什么也没有发生,也没有任何内容添加到我的布局中。

我需要“刷新”布局管理器什么的吗?它是在全球范围内宣布的。

更新:我已经尝试过 revalidate() 但它似乎不起作用,这是我的代码的简化版本,其中有一个用于概念证明的测试按钮:

public class MainGUI extends JPanel{
    static GridBagConstraints c;
    static MainGUI mainGUIclass;
    static JFrame mainGUIframe;

    public MainGUI() {
    this.setLayout(new GridBagLayout());
    c = new GridBagConstraints();

    saveButton = new JButton("Save and Exit");
    saveButton.setPreferredSize(new Dimension(200, 30));

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 4;
    add(saveButton, c);
    }

public static void main(String[] args) {
    mainGUIframe = new JFrame("Message");

    mainGUIframe.setSize(800,800);

    mainGUIclass = new MainGUI();

    mainGUIframe.add(mainGUIclass);
    mainGUIframe.setVisible(true);


   //now the addition
    JButton newButton = new JButton("New Button");  

    newButton.setPreferredSize(new Dimension(200, 30));


    c.gridx = 5;
    c.gridy = 0;
    c.gridwidth = 4;
    mainGUIclass.add(newButton,c);

    //none of this seems to work
    mainGUIclass.revalidate();//?
    mainGUIclass.repaint();//?

  }
}

更新2:这似乎是 passbyvalue 性质的问题java 和另一个类(画布)我试图添加到我的布局中。如果我找到解决方案,我会更新。

Update3:这是一个线程问题,我调用的类挂起主窗口。

编辑:我提供了代码作为参考,并试图完整地提供完整的图片,而不是轻易地自行编译。感谢所有提供帮助的人。

更新4:成功!关键是 mediaplayer 类执行了“isDisplayable()”检查,如果要添加的框架未添加到 gridbaglayout,则会导致程序挂起。一系列不幸的看起来传递值(JInternalFrames)、将内部框架预先添加到 gridbaglayout 以及从另一种方法远程启动媒体允许我所寻求的工作。

I've initialized a GridBagLayout globally, then in my class constructor have instantiated it and added some buttons etc. to it.

How can I add things to it after the fact? Simple class extends JFrame. Whenever I try class.add(stuff, gridbagconstraints) after the fact (used just add(stuff, gribagconstraints) in constructor) Nothing happens and nothing is added to my layout.

Do I need to "refresh" the layout manager or something? It is declared globally.

Update: I've tried revalidate() but it does not seem to be working, here is a simplified version of my code with a test button in place for proof of concept:

public class MainGUI extends JPanel{
    static GridBagConstraints c;
    static MainGUI mainGUIclass;
    static JFrame mainGUIframe;

    public MainGUI() {
    this.setLayout(new GridBagLayout());
    c = new GridBagConstraints();

    saveButton = new JButton("Save and Exit");
    saveButton.setPreferredSize(new Dimension(200, 30));

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 4;
    add(saveButton, c);
    }

public static void main(String[] args) {
    mainGUIframe = new JFrame("Message");

    mainGUIframe.setSize(800,800);

    mainGUIclass = new MainGUI();

    mainGUIframe.add(mainGUIclass);
    mainGUIframe.setVisible(true);


   //now the addition
    JButton newButton = new JButton("New Button");  

    newButton.setPreferredSize(new Dimension(200, 30));


    c.gridx = 5;
    c.gridy = 0;
    c.gridwidth = 4;
    mainGUIclass.add(newButton,c);

    //none of this seems to work
    mainGUIclass.revalidate();//?
    mainGUIclass.repaint();//?

  }
}

Update2: This appears to be an issue with the passbyvalue nature of java and another class(canvas) I am trying to add to my layout. Will update if I find a solution.

Update3: This is a thread problem, the class I'm calling is hanging the main window.

Edit: I provided the code as a reference, and tried to be complete to provide a complete picture, not to be easily compiled on its own. Thank you to all who offered your assistance.

Update4: Success! They key was that the mediaplayer class did an "isDisplayable()" check, which caused it to hang the program if the frame it was being added to was not get added to the gridbaglayout. A series of unfortunate looking pass by values (of JInternalFrames), preadding the internalframe to the gridbaglayout and a remote start of the media from another method allows what I am seeking to work.

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

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

发布评论

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

评论(2

南巷近海 2024-12-10 13:10:28

您可以对容器(如果它派生自 JComponent,例如 JPanel)调用 revalidate(),该容器使用布局来让布局重新设置它们包含的组件。这应该递归通过该布局所持有的所有容器,并且它们也应该更新其组件布局。据我所知,主要的例外是 JScrollPanes 中保存的组件,为此,您需要在滚动窗格的 JViewport 上调用 revalidate。

另外,有时您需要在 revalidate() 之后调用 repaint(),特别是在您删除了容器所持有的任何组件的情况下。

You would call revalidate() on the container (if it derives from JComponent such as JPanel) that uses the layout to have the layouts re-set the components they contain. This should recurse through all the containers held by this layout, and they too should have their component layouts updated. The main exception to this that I know of would be components held in JScrollPanes, and for that, you'd need to call revalidate on the scrollpane's JViewport.

Also, sometimes you will need to call repaint() after revalidate(), especially if you have removed any components held by the container.

慕烟庭风 2024-12-10 13:10:28

在您的示例中,您将添加一个与前一个按钮具有完全相同的 GridBagConstraints 的按钮。当我尝试运行该代码时,您的按钮会彼此叠置,因此您只会看到其中一个。尝试更改您的 GridBagConstraints ,以便将您添加的第二个按钮放置在另一个位置。建议为每个受约束的组件实例化一个新的 GridBagConstraints ,以消除发生此类编程错误的机会。

另外,关于您的更新,JFrame 没有 revalidate() 函数。

如果您还没有这样做,那么值得您仔细阅读 这个

In your example you are adding a button with exactly the same GridBagConstraints as the previous button. When I try running that code your buttons are laid on top of each other and so you will only see one of them. Try changing your GridBagConstraints so that the second button you add is placed in another location. It is recommended practice to instantiate a new GridBagConstraints for each constrained component to remove the chance of such programming errors occurring.

Also, with regards to your update, JFrame does not have a revalidate() function.

If you haven't already done so, it would be worth you reading thoroughly through this.

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