我使用MigLayout已经有一段时间了,我从来没有遇到过这个问题。由于某些原因,hidemode约束不起作用,我无法让它隐藏一个按钮。
下面是演示它的代码片段:
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
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-swing</artifactId>
<version>11.0</version>
</dependency>发布于 2022-07-10 13:58:37
Hidemode控制组件在component.setVisible(false);运行后如何隐藏。仍然需要component.setVisible(false);来隐藏组件。Hidemode控制布局中的周围组件如何在之后进行调整。
例如,可以通过调用b1来添加按钮操作监听器以隐藏和显示b1.setVisible(...);。
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();
}
}https://stackoverflow.com/questions/72838278
复制相似问题