英文:
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);
});
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论