1

我有这个非常简单的代码(实际上是直接从 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);
4

1 回答 1

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

于 2013-08-14T03:50:52.127 回答