我有一个JPanel,可以在其中动态添加和删除不同大小的子面板。因此,我将MigLayout与绝对单元格坐标一起使用。在以下情况下,结果不是我期望的。
说明问题的我的代码的简化版本如下:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutDemo
{
private static final int CELL_WIDTH = 60;
private static final int CELL_HEIGHT = 50;
public static JPanel createBigPanel()
{
JPanel bigSubPanel = new JPanel();
bigSubPanel.setPreferredSize(new Dimension(3 * CELL_WIDTH, 2 * CELL_HEIGHT));
bigSubPanel.setBorder(BorderFactory.createLineBorder(Color.black));
bigSubPanel.setBackground(Color.LIGHT_GRAY);
return bigSubPanel;
}
public static JPanel createSmallPanel()
{
JPanel smallSubPanel = new JPanel();
smallSubPanel.setPreferredSize(new Dimension(2 * CELL_WIDTH, CELL_HEIGHT));
smallSubPanel.setBorder(BorderFactory.createLineBorder(Color.black));
smallSubPanel.setBackground(Color.LIGHT_GRAY);
return smallSubPanel;
}
public static JPanel createCellPanel()
{
JPanel cellSubPanel = new JPanel();
cellSubPanel.setPreferredSize(new Dimension(CELL_WIDTH, CELL_HEIGHT));
cellSubPanel.setBackground(Color.LIGHT_GRAY);
return cellSubPanel;
}
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("MigLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// main panel where the sub-panels are placed in
JPanel mainPanel = new JPanel();
MigLayout mg = new MigLayout("debug, gap 0 0");
mainPanel.setLayout(mg);
mainPanel.add(createBigPanel(), "cell 0 0 3 2"); // [col row [span x [span y]]]
mainPanel.add(createBigPanel(), "cell 3 0 3 2");
mainPanel.add(createSmallPanel(), "cell 6 0 2 1, align left top");
mainPanel.add(createSmallPanel(), "cell 6 1 2 1, align left top");
mainPanel.add(createSmallPanel(), "cell 0 2 2 1, align left top");
mainPanel.add(createBigPanel(), "cell 2 2 3 2");
// problem occurs
mainPanel.add(createBigPanel(), "cell 5 2 3 2");
// mainPanel.add(createCellPanel(), "cell 0 4 1 1");
// mainPanel.add(createCellPanel(), "cell 1 4 1 1");
// mainPanel.add(createCellPanel(), "cell 2 4 1 1");
// mainPanel.add(createCellPanel(), "cell 3 4 1 1");
// mainPanel.add(createCellPanel(), "cell 4 4 1 1");
// mainPanel.add(createCellPanel(), "cell 5 4 1 1");
// mainPanel.add(createCellPanel(), "cell 6 4 1 1");
// mainPanel.add(createCellPanel(), "cell 7 4 1 1");
frame.getContentPane().add(mainPanel);
frame.setResizable(false);
frame.pack();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
如果我删除代码行,则mainPanel.add(createBigPanel(), "cell 5 2 3 2");
一切都与我预期的一样。但是,当我在第5列和第2行中添加面板时,看起来该面板位于第5列和第6列之间的额外列中。
如果我也添加代码行
mainPanel.add(createCellPanel(), "cell 0 4 1 1");
mainPanel.add(createCellPanel(), "cell 1 4 1 1");
mainPanel.add(createCellPanel(), "cell 2 4 1 1");
mainPanel.add(createCellPanel(), "cell 3 4 1 1");
mainPanel.add(createCellPanel(), "cell 4 4 1 1");
mainPanel.add(createCellPanel(), "cell 5 4 1 1");
mainPanel.add(createCellPanel(), "cell 6 4 1 1");
mainPanel.add(createCellPanel(), "cell 7 4 1 1");
该问题似乎已“解决”,并且所有内容都放置在正确的列和行中。
我怀疑我的问题与细胞的跨度有关。我是否缺少一些MigLayout约束或参数?还是MigLayout库中的错误?
我目前在Java 7中使用MigLayout V4.0。我还使用V5.0快照测试了我的代码,但结果是相同的。
对我有用的解决方案是在MigLayout中使用绝对定位,即采用以下形式的组件约束pos x y [x2] [y2]
:
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutDemo4 extends JFrame {
private static final int CELL_WIDTH = 60;
private static final int CELL_HEIGHT = 50;
public MigLayoutDemo4() {
initUI();
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("gap 0 0"));
pnl.add(createLabel("One"), "pos 0 0");
pnl.add(createLabel("Two"), "pos "+(CELL_WIDTH * 3)+" "+(CELL_HEIGHT * 0));
pnl.add(createLabel("Three"), "pos "+(CELL_WIDTH * 6)+" "+(CELL_HEIGHT * 0));
pnl.add(createLabel("Four"), "pos "+(CELL_WIDTH * 6)+" "+(CELL_HEIGHT * 1));
pnl.add(createLabel("Five"), "pos "+(CELL_WIDTH * 0)+" "+(CELL_HEIGHT * 2));
pnl.add(createLabel("Six"),"pos "+(CELL_WIDTH * 2)+" "+(CELL_HEIGHT * 2));
pnl.add(createLabel("Seven"), "pos "+(CELL_WIDTH * 5)+" "+(CELL_HEIGHT * 2));
add(pnl);
pack();
setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JLabel createLabel(String text) {
JLabel lbl = new JLabel(text, JLabel.CENTER);
lbl.setBorder(BorderFactory.createEtchedBorder());
return lbl;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutDemo3 ex = new MigLayoutDemo3();
ex.setVisible(true);
}
});
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句