【发布时间】:2014-04-24 05:33:31
【问题描述】:
我正在尝试使用 MigLayout 设置某些元素的高度,以使用表达式“height 100%”来占据其父容器的整个高度,但是 MigLayout 中有某种反馈循环会导致高度加载时跳转到最大值。此问题仅在 MiGPanel 嵌入 JScrollPane 时发生,如果我删除处理 JScrollPane 的代码行并将 MiGPanel 添加到我的 JFrame 中,则程序按预期运行。我在那里添加了布尔语句,以使这个更容易看到。
注意: 我已经看到了这个修复,但我想使用一个解决方案,我的程序底部没有边距。 How to prevent MigLayout from exceeding a container's bounds
代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
public class StackOverFlowQuestion {
public static void main(String[] args) {
JPanel MiGPanel = new JPanel();
MiGPanel.setBackground(Color.red);
MiGPanel.setLayout(new MigLayout());
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.green);
MiGPanel.add(greenPanel, "pos 0 0, width 33%, height 100%");
JPanel yellowPanel = new JPanel();
yellowPanel.setBackground(Color.yellow);
MiGPanel.add(yellowPanel, "pos 33% 0, width 33%, height 100%");
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
MiGPanel.add(bluePanel, "pos 66% 0, width 34%, height 100%");
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
boolean scroll = true;
if(scroll){
JScrollPane scrollPane = new JScrollPane();
scrollPane.add(MiGPanel);
scrollPane.setViewportView(MiGPanel);
mainFrame.add(scrollPane);
}else{
mainFrame.add(MiGPanel);
}
mainFrame.add(scrollPane);
mainFrame.pack();
mainFrame.setSize(900, 600);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
}
【问题讨论】:
-
只有我一个人有这个问题?
标签: java swing jscrollpane miglayout