我使用MigLayout创建了简单的Jdialog。一旦actionListener刷新屏幕对话框就会增长。在示例代码中,使用“应用”按钮会更改对话框的大小。
我尝试调试此问题,发现pack()从MyDialog的LayoutManager获取首选大小,而不是我为内容窗格创建的MigLayout。我用这段代码做了一些非常愚蠢的事吗?
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
public class MigTest {
public static void main(String[] args) throws InvocationTargetException, InterruptedException {
EventQueue.invokeAndWait(new Runnable() {
@Override public void run () {
MyDialog d = new MyDialog();
d.setVisible(true);
}
});
}
public static class MyDialog extends JDialog {
private JLabel messageLbl;
private JButton applyButton;
public MyDialog () {
super((Frame) null, true);
setupGUI();
applyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed (ActionEvent arg0) {
System.out.println("getPreferredSize()");
System.out.println(" " + getPreferredSize());
System.out.println("getLayout()");
System.out.println(" " + getLayout());
System.out.println(" " + getContentPane().getLayout());
pack();
}
});
pack();
setLocationRelativeTo(null);
}
private void setupGUI () {
setResizable(true);
Container panel = getContentPane();
panel.setLayout(new MigLayout("debug, wrap 2", "[50%][50%]"));
messageLbl = new JLabel("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed posuere interdum sem. Quisque ligula eros ullamcorper quis, lacinia quis facilisis sed sapien.");
panel.add(messageLbl, "width 700px!, center, span, wrap");
panel.add(new JLabel("foo"));panel.add(new JLabel("bar"));
applyButton = new JButton("apply...");
panel.add(applyButton, "center, wrap, span");
}
}
}