Swing 中带有父级的可最大化对话框

问题描述 投票:0回答:1

我正在合作开发一个成熟的桌面 Swing 应用程序

客户想要一个对话:

  1. 角落里有一个最大化按钮(我想其他标准框架按钮也是如此,最小化和关闭)
  2. 与“根框架”有关系。例如,当对话框仍然可见时,根框架不应该最小化

我重新实现了对话框,它曾经是一个

JDialog
,成为一个
JFrame
子类。它解决了第一个问题,但现在它完全忽略了任何根框架,因为
JFrame
没有父母

他们

JDialog
添加最大化按钮是可能的,但不是一个好主意(尽管我不确定为什么)。该应用程序用于医疗机构,我不想冒险

我如何满足客户的期望?

UPD: Abra 建议致电

JDialog.setDefaultLookAndFeelDecorated(true)
。这种方法存在很多问题:

  1. 仍然没有最大化按钮
  2. 外观与全局设置不匹配
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);
    }
}

enter image description here

顺便说一句,“不安全”选项(见上面的链接)也有同样的问题

    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);
    }
java swing
1个回答
0
投票

这是一种粗略但有效的方法来最大化

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");
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.