在OS X上调整JPanel的大小

我有两个Swing组件:JDialog – > JPanel

我想用JPanel填充JDialog中的所有空间。 默认设置正常。 我可以更改对话框的大小,并正确更改JPanel的大小。

但是当我点击“最大化”图标时,内部JPanel会被冻结,直到窗口最大化。

OS X版本10;
Java版本1.7。

代码示例:

final JDialog dialog = new JDialog(mainFrame, true); dialog.setSize(new Dimension(800, 600)); dialog.setLocationRelativeTo(null); final JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 14)); dialog.add(panel); dialog.show(); 

是否存在修复此行为的方法?

调整对话框或最大化对话框时,以下完整示例不会冻结。 以下是一些需要注意的事项:

  • JPanel的默认布局是FlowLayout ; 为了比较,我将框架的布局设置为相同。

  • 调用pack() “使此Window大小适合其子组件的首选大小和布局。” 由于对话框只包含一个空的Jpanel ,因此我重写了getPreferredSize()以显示效果。

  • 应该在事件派发线程上构造和操作Swing GUI对象。

图片

 import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.BorderFactory; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * @see https://stackoverflow.com/a/22450263/230513 */ public class Test { private void display() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new FlowLayout()); frame.add(new JLabel("Frame")); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); JDialog dialog = new JDialog(frame, true); final JPanel panel = new JPanel(){ @Override public Dimension getPreferredSize() { return new Dimension(320, 240); } }; panel.add(new JLabel("Dialog")); panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 14)); dialog.add(panel); dialog.pack(); dialog.setLocationRelativeTo(frame); dialog.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Test().display(); } }); } }