练习-在上次关闭位置启动窗口

比如这次使用这个窗口,导致窗口被移动到了右下角。 关闭这个窗口,下一次再启动的时候,就会自动出现在右下角。

要在Java中实现在上次关闭位置启动窗口,您需要执行以下步骤:

  1. 保存窗口位置:在窗口关闭时,获取窗口的位置信息(x和y坐标)并将其保存到持久存储中,例如配置文件或数据库。您可以使用Window类的getLocation()方法获取窗口的位置信息。

  2. 加载窗口位置:在窗口启动时,从持久存储中加载上次保存的窗口位置信息。

  3. 设置窗口位置:在窗口启动后,使用加载的位置信息通过Window类的setLocation()方法将窗口放置在正确的位置。

    package com.jcli.graphic_user_interface;
    
    import javax.swing.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.*;
    import java.util.Properties;
    
    /**
     在上次关闭位置启动窗口
     */
    public class _02_MainWindow extends JFrame {
        private static final String CONFIG_FILE = "window.config";
    
        public _02_MainWindow() {
            // 设置窗口标题和大小
            setTitle("Main Window");
            setSize(400, 300);
    
            // 添加窗口关闭事件监听器
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    saveWindowPosition();
                    dispose();
                    System.exit(0);
                }
            });
    
            // 加载上次保存的窗口位置
            loadWindowPosition();
        }
    
        private void saveWindowPosition() {
            try {
                Properties properties = new Properties();
                properties.setProperty("x", String.valueOf(getX()));
                properties.setProperty("y", String.valueOf(getY()));
    
                FileOutputStream fileOut = new FileOutputStream(CONFIG_FILE);
                properties.store(fileOut, "Window Position");
                fileOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void loadWindowPosition() {
            File configFile = new File(CONFIG_FILE);
            if (configFile.exists()) {
                try {
                    Properties properties = new Properties();
                    FileInputStream fileIn = new FileInputStream(CONFIG_FILE);
                    properties.load(fileIn);
                    fileIn.close();
    
                    int x = Integer.parseInt(properties.getProperty("x"));
                    int y = Integer.parseInt(properties.getProperty("y"));
                    setLocation(x, y);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                _02_MainWindow window = new _02_MainWindow();
                window.setVisible(true);
            });
        }
    }
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值