根据操作系统的默认行为,如何关闭/隐藏/还原一个Swing应用程序

huangapple go评论78阅读模式
英文:

How to close/hide/restore a Swing application according to the OS default behaviour

问题

On Mac, apps are expected to "hide" into the dock when clicking the "x" button on the window's title bar, and restore when being clicked on again in the dock.

On Windows and Linux, apps simply terminate upon clicking the "x" button.

How do I replicate this behavior in a Java Swing application, depending on which operating system the user is on?

英文:

On Mac, apps are expected to "hide" into the dock when clicking the "x" button on the window's title bar, and restore when being clicked on again in the dock.

On Windows and Linux, apps simply terminate upon clicking the "x" button.

How do I replicate this behaviour in a Java Swing application, depending on which operating system the user is on?

答案1

得分: 1

You can get the OS system by using:

System.getProperty(...);

Check out the System.getProperties() method for a list of valid properties to query, including the OS.

Then once you have the OS you can use:

frame.setDefaultCloseOperation(...)
英文:

Don't know if there is a better way but you can get the OS system by using:

System.getProperty(...);

Check out the System.getProperties() method for a list of valid properties to query, including the OS.

Then once you have the OS you can use:

frame.setDefaultCloseOperation(...)

答案2

得分: 1

很简单,只需使用WindowListener接口来处理窗口关闭事件,并根据操作系统控制行为。让我用一个示例来解释,我的示例中,MainFrame类扩展了JFrame,并将默认关闭操作设置为DO_NOTHING_ON_CLOSE,并添加了WindowListener来处理窗口关闭事件!

看一下这个示例:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class MainFrame extends JFrame {

    public MainFrame() {
        setTitle("YOUR APP");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                handleWindowClosing();
            }
        });
    }

    private void handleWindowClosing() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("mac")) {
            // 在Mac上最小化窗口
            setExtendedState(JFrame.ICONIFIED);
        } else {
            // 在其他平台上关闭窗口
            dispose();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setSize(110, 110);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}
英文:

it's easy, just use the WindowListener interface to handle the window closing event and control the behavior based on the operating system, let me explain it with an example, in my example,the MainFrame class extends JFrame and sets the default close operation to DO_NOTHING_ON_CLOSE and the WindowListener is added to handle the window closing event!

check this out:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class MainFrame extends JFrame {

    public MainFrame() {
        setTitle("YOUR APP");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                handleWindowClosing();
            }
        });
    }

    private void handleWindowClosing() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("mac")) {
            // Minimize the window on Mac
            setExtendedState(JFrame.ICONIFIED);
        } else {
            // Close the window on other platforms
            dispose();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setSize(110, 110);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

huangapple
  • 本文由 发表于 2023年7月24日 19:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定