您需要将面板添加到 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