首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MigLayout hidemode无法工作,无法隐藏组件。

MigLayout hidemode无法工作,无法隐藏组件。
EN

Stack Overflow用户
提问于 2022-07-02 10:16:48
回答 1查看 89关注 0票数 0

我使用MigLayout已经有一段时间了,我从来没有遇到过这个问题。由于某些原因,hidemode约束不起作用,我无法让它隐藏一个按钮。

下面是演示它的代码片段:

代码语言:javascript
复制
import java.awt.Container;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;

public class Builder extends JFrame {

    private final MigLayout migLayout = new MigLayout("debug, fillx", "[][]", "");

    public Builder() {
        try {
            createAndShowGUI();
        } catch (Throwable th) {
            th.printStackTrace();
            Builder.this.dispatchEvent(new WindowEvent(Builder.this, WindowEvent.WINDOW_CLOSING));
        }
    }

    private void createAndShowGUI() {
        Container container = getContentPane();
        container.setLayout(migLayout);
        JButton b1 = new JButton("b1");
        JButton b2 = new JButton("b2");
        container.add(b1, "hidemode 1, alignx left");
        container.add(b2, "alignx right");
        pack();
        setResizable(true);
        setTitle("Builder");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Builder();
    }
}

我使用的是11.0版的MigLayout

代码语言:javascript
复制
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout-swing</artifactId>
            <version>11.0</version>
        </dependency>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-10 13:58:37

Hidemode控制组件在component.setVisible(false);运行后如何隐藏。仍然需要component.setVisible(false);来隐藏组件。Hidemode控制布局中的周围组件如何在之后进行调整。

例如,可以通过调用b1来添加按钮操作监听器以隐藏和显示b1.setVisible(...);

代码语言:javascript
复制
import java.awt.event.WindowEvent;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;

public class Builder extends JFrame {

    private final MigLayout migLayout = new MigLayout("debug, fillx", "[][]", "");

    public Builder() {
        try {
            createAndShowGUI();
        } catch (Throwable th) {
            th.printStackTrace();
            Builder.this.dispatchEvent(new WindowEvent(Builder.this, WindowEvent.WINDOW_CLOSING));
        }
    }

    private void createAndShowGUI() {
        Container container = getContentPane();
        container.setLayout(migLayout);
        JButton b1 = new JButton("hide me");
        JButton b2 = new JButton("show it");
        container.add(b1, "hidemode 1, alignx left");
        container.add(b2, "alignx right");

        b1.addActionListener(e -> b1.setVisible(false));
        b2.addActionListener(e -> b1.setVisible(true));

        pack();
        setResizable(true);
        setTitle("Builder");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Builder();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72838278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档