我有这个非常简单的代码(实际上直接从miglayout白皮书中复制过来:http://www.miglayout.com/whitepaper.html)。我在最后添加了panel.setVisible(true)。问题是,无论有没有最后一行,都没有显示出来。
MigLayout layout = new MigLayout("fillx", "[right]rel[grow,fill]", "[]10[]");
JPanel panel = new JPanel(layout);
panel.add(new JLabel("Enter size:"), "");
panel.add(new JTextField(""), "wrap");
panel.add(new JLabel("Enter weight:"), "");
panel.add(new JTextField(""), "");
panel.setVisible(true);
答案 0 :(得分:0)
您需要将面板添加到JFrame:
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;
public class MigLayoutTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
MigLayout layout = new MigLayout("fillx", "[right]rel[grow,fill]", "[]10[]");
JPanel panel = new JPanel(layout);
panel.add(new JLabel("Enter size:"), "");
panel.add(new JTextField(""), "wrap");
panel.add(new JLabel("Enter weight:"), "");
panel.add(new JTextField(""), "");
//panel.setVisible(true);
JFrame frame = new JFrame("MigLayoutTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300,200));
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
JPanel不是顶级框架,但是JFrame,JDialog和JApplet都是。请参阅顶级容器上的本教程:http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html