似乎 MigLayout 被 JScrollPane 对象中包含的 JTable 对象弄糊涂了。由于在 JScrollPane 中包含 JTable 似乎是一种常见的习惯用法,我认为找出原因很重要。
在随附的示例程序中,创建了一个简化的表格,由 12 行和 4 列组成。所有宽度和高度都设置为百分比。许多组件被添加到表单中,全部按单元格位置。一些跨行或列。最后一列显示单元格行索引和大小百分比。
如果程序以“JLabel”参数启动,则 JSCrollPane 中包含的 JLabel 将添加到第 0 列,所有对齐看起来都很好。如果程序以参数“JTable”启动,所有列的行大小都会被打乱。
这是错误还是功能?
Make: javac -classpath <path to miglayout> migtest.java
Usage: java -classpath <path to miglayout> migtest <JTable|JLabel>
注意:如果下面的代码出现混淆(没有标签),我尝试了各种粘贴方法 - 我无法让预览看起来正确。让我知道是否有什么可以解决的。
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableColumn;
import net.miginfocom.swing.MigLayout;
public class Migtest extends JFrame {
public final static int appWidth = 1200; // default
public final static int appHeight = 800; // default
String[] clientColNames = { "Name", "Address", "Alternate Address" };
JComponent createClientsPane(int numCols, String arg) {
if (arg.equals("jtable")) {
JTable clientsTable = new JTable(1, numCols);
clientsTable.getTableHeader().setReorderingAllowed(false);
clientsTable.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
clientsTable.getSelectionModel().setSelectionInterval(0, 0);
JScrollPane scrollPane = new JScrollPane(clientsTable);
return scrollPane;
} else
return new JScrollPane(new JLabel("bear tenders"));
}
void init(String[] args) {
JPanel topPanel = null, subpanel = null;
String arg;
if (args.length != 1) {
System.out.println("missing arg - s/b 'JLabel' or 'jTable'");
System.exit(0);
}
arg = args[0].trim().toLowerCase();
if (!arg.equals("jlabel") && !arg.equals("jtable")) {
System.out.println("missing arg - s/b 'JLabel' or 'jTable'");
System.exit(0);
}
setSize(appWidth, appHeight);
topPanel = new JPanel(new MigLayout("fill,debug",
"[30%][15%][50%][5%]",
"[5%][7%][5%][10%][7%][7%][7%][7%][6%][7%][2%][30%]"));
JCheckBox dynamicInstrumentCheckbox = new JCheckBox(
"Wood Activities Enabled", false);
topPanel.add(dynamicInstrumentCheckbox, "cell 0 2");
topPanel.add(new JLabel("BEAR TENDERS"), "cell 0 4, alignx center");
topPanel.add(createClientsPane(clientColNames.length, arg),
"cell 0 5, spany 4, grow");
topPanel.add(new JLabel("BEAR FACTS"), "cell 1 4, alignx center");
JRadioButton noneButton = new JRadioButton("None");
topPanel.add(noneButton, "cell 1 5, gapleft 2%");
JRadioButton rangeButton = new JRadioButton("Fact 1");
topPanel.add(rangeButton, "cell 1 6, gapleft 2%");
JRadioButton playbackButton = new JRadioButton("Fact 2");
topPanel.add(playbackButton, "cell 1 7, gapleft 2%");
JButton controlsButton = new JButton("Controls =>");
topPanel.add(controlsButton, "cell 1 8, alignx center");
topPanel.add(new JLabel("GUMMY BEARS"), "cell 2 1, alignx center");
topPanel.add(new JLabel("(gummy bears)"), "cell 2 2, spany 4, grow");
topPanel.add(new JLabel("CHICAGO BEARS"), "cell 2 6, alignx center");
topPanel.add(new JLabel("(chicago bears)"), "cell 2 7, spany 3, grow");
topPanel.add(new JLabel("LOG"), "cell 0 10, alignx left");
JButton clearLogButton = new JButton("Clear Log");
topPanel.add(clearLogButton, "cell 2 10, alignx right");
topPanel.add(new JLabel("(log pane)"), "cell 0 11, spanx 3, grow");
topPanel.add(new JLabel("0-5%"), "cell 3 0, grow");
topPanel.add(new JLabel("1-7%"), "cell 3 1, grow");
topPanel.add(new JLabel("2-5%"), "cell 3 2, grow");
topPanel.add(new JLabel("3-10%"), "cell 3 3, grow");
topPanel.add(new JLabel("4-7%"), "cell 3 4, grow");
topPanel.add(new JLabel("5-7%"), "cell 3 5, grow");
topPanel.add(new JLabel("6-7%"), "cell 3 6, grow");
topPanel.add(new JLabel("7-7%"), "cell 3 7, grow");
topPanel.add(new JLabel("8-6%"), "cell 3 8, grow");
topPanel.add(new JLabel("9-7%"), "cell 3 9, grow");
topPanel.add(new JLabel("10-2%"), "cell 3 10, grow");
topPanel.add(new JLabel("11-30%"), "cell 3 11, grow");
setContentPane(topPanel);
}
public static void main(String[] args) {
try {
Migtest thisTest = new Migtest();
thisText.init(args);
// thisTest.init(new String[] {"jLabel"});
// thisTest.init(new String[] {"jTable"});
// center app window
GraphicsConfiguration gc = thisTest.getGraphicsConfiguration();
Rectangle bounds = gc.getBounds();
thisTest.setLocation(
(int) ((bounds.width - thisTest.appWidth) / 2),
(int) ((bounds.height - thisTest.appHeight) / 2));
thisTest.setVisible(true);
} catch (Exception e) {
System.out.println("runTest caught exception: " + e.getMessage());
e.printStackTrace();
}
}
} // class test