要为JDialog设置固定窗口大小,可以使用setPreferredSize()方法来设置JDialog的首选大小,并使用setResizable(false)方法来禁止用户调整窗口大小。以下是一个示例代码: ```java import javax.swing.*; public class FixedSizeDialog extends JDialog { public FixedSizeDialog(JFrame parent) { super(parent, "Fixed Size Dialog", true); // 设置JDialog的首选大小 setPreferredSize(new Dimension(300, 200)); // 禁止用户调整窗口大小 setResizable(false); // 添加其他组件和布局 pack(); setLocationRelativeTo(parent); } public static void main(String[] args) { JFrame frame = new JFrame("Main Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Open Dialog"); button.addActionListener(e -> { FixedSizeDialog dialog = new FixedSizeDialog(frame); dialog.setVisible(true); }); frame.getContentPane().add(button); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } ``` 在上面的示例中,我们创建了一个名为FixedSizeDialog的自定义JDialog类,它继承自JDialog。在构造函数中,我们使用setPreferredSize()方法设置了JDialog的首选大小为300x200像素,并使用setResizable(false)方法禁止用户调整窗口大小。然后,我们可以根据需要添加其他组件和布局。最后,我们调用pack()方法来调整JDialog的大小以适应其内容,并使用setLocationRelativeTo()方法将JDialog定位在其父窗口的中心位置。 在主方法中,我们创建了一个名为MainFrame的JFrame,并添加了一个按钮。当按钮被点击时,我们创建一个FixedSizeDialog实例,并将其设置为可见。