我正在合作开发一个成熟的桌面 Swing 应用程序
客户想要一个对话:
我重新实现了对话框,它曾经是一个
JDialog
,成为一个JFrame
子类。它解决了第一个问题,但现在它完全忽略了任何根框架,因为JFrame
没有父母
他们说向
JDialog
添加最大化按钮是可能的,但不是一个好主意(尽管我不确定为什么)。该应用程序用于医疗机构,我不想冒险
我如何满足客户的期望?
UPD: Abra 建议致电
JDialog.setDefaultLookAndFeelDecorated(true)
。这种方法存在很多问题:
public class Main {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Main Frame");
JButton popupButton = new JButton("Open Popup");
JDialog.setDefaultLookAndFeelDecorated(true);
popupButton.addActionListener(e -> {
JDialog popupDialog = new JDialog(mainFrame, "Popup Dialog");
popupDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupDialog.setSize(200, 200);
popupDialog.setLocationRelativeTo(null);
popupDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
popupDialog.setVisible(true);
});
mainFrame.add(popupButton);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
}
顺便说一句,“不安全”选项(见上面的链接)也有同样的问题
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Main Frame");
JButton popupButton = new JButton("Open Popup");
//JDialog.setDefaultLookAndFeelDecorated(true);
popupButton.addActionListener(e -> {
JDialog popupDialog = new JDialog(mainFrame, "Popup Dialog");
popupDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupDialog.setSize(200, 200);
popupDialog.setLocationRelativeTo(null);
popupDialog.setUndecorated(true);
popupDialog.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
popupDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
popupDialog.setVisible(true);
});
mainFrame.add(popupButton);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
这是一种粗略但有效的方法来最大化
JDialog
。我借鉴了阿布拉的例子。
我很惊讶地发现
JDialog
没有setExtendedState
,所以我手动完成了。 JDialog
有一个最大化/正常 JButton
,它会改变 JDialog
的大小。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MaximizeDialog implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MaximizeDialog());
}
private JDialog popupDialog;
@Override
public void run() {
JFrame mainFrame = new JFrame("Main Frame");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(createButtonPanel(mainFrame), BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setLocationByPlatform(true);
mainFrame.setVisible(true);
}
private JPanel createButtonPanel(JFrame mainFrame) {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(50, 150, 50, 150));
JButton popupButton = new JButton("Open Popup");
panel.add(popupButton);
popupButton.addActionListener(e -> {
createDialog(mainFrame);
});
return panel;
}
private void createDialog(JFrame mainFrame) {
popupDialog = new JDialog(mainFrame, "Popup Dialog");
popupDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupDialog.add(createDialogButtonPanel(), BorderLayout.SOUTH);
popupDialog.setSize(200, 200);
popupDialog.setLocationRelativeTo(mainFrame);
popupDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
popupDialog.setVisible(true);
}
private JPanel createDialogButtonPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton maximizeButton = new JButton("Maximize");
panel.add(maximizeButton);
maximizeButton.addActionListener(e -> {
maximizeDialog(e);
});
return panel;
}
private void maximizeDialog(ActionEvent event) {
JButton button = (JButton) event.getSource();
String text = button.getText();
if (text.equals("Maximize")) {
popupDialog.setSize(1000, 1000);
button.setText("Normal");
} else {
popupDialog.setSize(200, 200);
button.setText("Maximize");
}
}
}