我对 MigLayout 有一些奇怪的行为。我有一个反映我的问题的 SSCCE。基本上,顶部两个面板之间存在间隙(该间隙属于左侧单元格)。其他一切都如我所愿。JFrame
仅当调整大小且足够大时才会出现间隙。
左侧面板(名为Measurement
)应具有固定宽度,而中间面板(名为Config
)是pushx, growx
并且应该填充该行上其他两个组件留下的所有空间。但左面板单元格似乎偷走了剩余的空间。
如何删除该空间(以便配置面板直接接触测量面板并且测量面板正好是 500 像素宽)?
我正在使用 MigLayout 4.0。
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
public class Main {
private static JButton minimizeButton;
private static JPanel configPanel, plotPanel, measPanel;
public static void main(final String[] args) {
final JFrame frame = new JFrame("test");
frame.setLayout(new MigLayout("insets 10, hidemode 3, debug", "", ""));
frame.add(getMeasPanel(), "w 500!");
frame.add(getConfigPanel(), "left, grow, pushx");
frame.add(getMinimizeButton(), "right, top, wrap");
frame.add(getPlotPanel(), "spanx 3, grow, push, wrap");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JPanel getConfigPanel() {
if (configPanel == null) {
configPanel = new JPanel(new MigLayout("insets 10"));
configPanel.add(new JLabel("test123"), "spanx 2, wrap");
configPanel.add(new JLabel("test123"), "h 40!");
configPanel.add(new JLabel("test123"), "right, wrap");
configPanel.setBorder(BorderFactory.createTitledBorder(null,
"Plot", TitledBorder.LEFT, TitledBorder.TOP, new Font(
"null", Font.BOLD, 12), Color.BLUE));
}
return configPanel;
}
private static JButton getMinimizeButton() {
if (minimizeButton == null) {
minimizeButton = new JButton("_");
minimizeButton.setFocusPainted(false);
minimizeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
toggleConfigMinimize();
}
});
}
return minimizeButton;
}
private static JPanel getPlotPanel() {
if (plotPanel == null) {
plotPanel = new JPanel();
plotPanel.setBorder(BorderFactory.createTitledBorder(null,
"Plot Config", TitledBorder.LEFT, TitledBorder.TOP,
new Font("null", Font.BOLD, 12), Color.BLUE));
}
return plotPanel;
}
private static JPanel getMeasPanel() {
if (measPanel == null) {
measPanel = new JPanel(new MigLayout("insets 10"));
measPanel.add(new JLabel("test123"), "spanx 2, wrap");
measPanel.add(new JLabel("test123"), "h 40!");
measPanel.add(new JLabel("test123"), "right, wrap");
measPanel.add(new JLabel("test123"), "spanx 2, wrap");
measPanel.add(new JLabel("test123"), "spanx 2, wrap");
measPanel.setBorder(BorderFactory.createTitledBorder(null,
"Measurement", TitledBorder.LEFT, TitledBorder.TOP,
new Font("null", Font.BOLD, 12), Color.BLUE));
}
return measPanel;
}
private static boolean showConfig = true;
protected static void toggleConfigMinimize() {
showConfig = !showConfig;
getMeasPanel().setVisible(showConfig);
getConfigPanel().setVisible(showConfig);
}
}