您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

番茄工作法計時器:用 Java Swing 動手做一個

編輯:iOS 相關資訊

一、番茄工作法是什麼

如果你讀過關於效率或時間管理的文章,大概聽過番茄工作法(Pomodoro Technique)。它的核心很簡單:

  1. 確定你要做什麼(例如「翻譯一篇外文」)。
  2. 設定一個 25 分鐘的計時器。
  3. 專心工作直到計時器響——這就是一個「番茄鐘」。
  4. 休息 5 分鐘,開始下一個番茄鐘。
  5. 每完成 4 個番茄鐘,做一次較長的休息。

多數人會依自己的節律微調,例如每 2 個番茄鐘就長休一次,剛開始實踐時尤其如此。

它的價值不在計時本身,而在強迫你為「一次專注」設定明確邊界——倒數計時的壓力會讓人自動關掉不相關的分心來源。

二、用 Swing 做出來需要哪些技巧

這個小工具涉及幾個實用的桌面開發技巧:圓角視窗、自訂標題列、系統托盤、以及倒數計時。

1. 視窗圓角

// 圓角
AWTUtilities.setWindowShape(this,
    new RoundRectangle2D.Double(0.0D, 0.0D,
        getWidth(), getHeight(), 26.0D, 26.0D));

2. 隱藏系統標題列

setUndecorated(true);

隱藏之後視窗就沒有標題列了,因此必須自己做一個,並加上拖曳移動的功能。

3. 自訂可拖曳標題列

public class MouseMoveListener extends MouseAdapter implements MouseMotionListener {

    private Component parentComponent;   // 被拖動的視窗
    private Component dragCom;           // 可按下拖曳的元件(標題列)
    private Point offset;                // 按下位置相對元件的偏移

    public synchronized void install(Component comp, Component dragCom) {
        uninstall();
        parentComponent = comp;
        this.dragCom = dragCom;
        dragCom.addMouseListener(this);
        dragCom.addMouseMotionListener(this);
    }

    public synchronized void uninstall() {
        if (dragCom != null) {
            dragCom.removeMouseListener(this);
            dragCom.removeMouseMotionListener(this);
            dragCom = null;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == dragCom) {
            offset = e.getPoint();       // 記下按下時的位置
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (e.getSource() != dragCom) return;

        final int x = parentComponent.getX();
        final int y = parentComponent.getY();
        final Point lastAt = e.getPoint();

        parentComponent.setLocation(x + lastAt.x - offset.x,
                                    y + lastAt.y - offset.y);
    }
}

使用方式:

MouseMoveListener mouseMoveListener = new MouseMoveListener();
mouseMoveListener.install(this, titlePanel);

兩個參數分別是「要移動的視窗」與「可以按住拖曳的元件」。

這裡有個關鍵設計:用 offset 記錄按下瞬間的滑鼠位置,拖動時用差值計算位移。如果直接 setLocation(e.getPoint()),視窗左上角會黏在滑鼠上,拖起來會有跳動感。

4. 系統托盤圖示

private void initTrayIcon() {
    PopupMenu popup = new PopupMenu();

    MenuItem showItem = new MenuItem("顯示視窗");
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(true);
            setExtendedState(Frame.NORMAL);
            SystemTray.getSystemTray().remove(trayIcon);
        }
    };
    showItem.addActionListener(listener);
    popup.add(showItem);

    MenuItem exitItem = new MenuItem("退出");
    exitItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            SystemTray.getSystemTray().remove(trayIcon);
            System.exit(0);
        }
    });
    popup.add(exitItem);

    // 依圖示、提示文字、選單建立 TrayIcon
    this.trayIcon = new TrayIcon(Icon.icon16.getImage(), "番茄時間管理", popup);
    this.trayIcon.addActionListener(listener);
}

public void minimizeToTray() {
    SystemTray tray = SystemTray.getSystemTray();
    try {
        tray.add(this.trayIcon);
    } catch (AWTException ex) {
        ex.printStackTrace();
    }
}

5. 倒數計時

使用 Timer 搭配 TimerTask:每秒觸發一次,減去剩餘秒數並更新顯示,歸零時響鈴並進入休息階段。

6. 版面配置

用多層 JPanel 嵌套搭配 BorderLayout,三個按鈕(開始/暫停/重設)放在使用 CardLayout 的面板中切換顯示。

三、踩過的坑

  • 中文亂碼:在 Eclipse 中執行時,右鍵選單的中文可能變亂碼。到 Run Configuration 的 VM arguments 加上 -Dfile.encoding=GB18030(或 UTF-8)即可。
  • 視窗取消裝飾後無法關閉:記得自己加上關閉按鈕與 setDefaultCloseOperation 的替代處理。
  • 托盤圖示重複加入:tray.add() 同一個圖示兩次會拋 AWTException,切換顯示/最小化時要記得先 remove。

四、可以再加強的地方

  • 把 25 分鐘與 5 分鐘做成可設定參數,並持久化保存。
  • 記錄每天的番茄鐘完成數,做簡單的統計圖表。
  • 計時結束時除了響鈴,加上系統通知或視窗閃爍提示。
  • 加上「這顆番茄在做什麼」的輸入欄位,事後能回顧時間都花去哪了。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved