| 元件 | 屬性設定重點 |
JTextArea | 置於 JScrollPane、lineWrap=true、wrapStyleWord=true、字型 Monospaced 14 |
JMenuBar | 檔案、編輯、格式、說明四級選單;項目設定 accelerator 快捷鍵 |
JToolBar | 新增、開啟、儲存、剪貼、複製、貼上、字型、顏色按鈕;設定 toolTipText |
JStatusBar | 左側顯示游標行/列、右側顯示編碼/修改狀態 |
3. 核心邏輯實作
檔案操作狀態機
private String currFileName = null; // null = 未儲存新檔
private boolean dirty = false; // 內容是否異動
private void newFile() {
if (checkSave()) return;
textArea.setText("");
currFileName = null;
dirty = false;
updateTitle();
}
private void openFile() {
if (checkSave()) return;
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
loadFile(fileChooser.getSelectedFile());
}
}
private void saveFile() {
if (currFileName == null) saveAsFile();
else writeFile(new File(currFileName));
}
private boolean checkSave() {
if (!dirty) return false;
int opt = JOptionPane.showConfirmDialog(this, "檔案已修改,是否儲存?", "確認",
JOptionPane.YES_NO_CANCEL_OPTION);
if (opt == JOptionPane.CANCEL_OPTION) return true;
if (opt == JOptionPane.YES_OPTION) saveFile();
return false;
}
格式設定:字型、顏色對話框
// 字型選擇 (JBuilder dbSwing FontChooser)
FontChooser fc = new FontChooser();
fc.setSelectedFont(textArea.getFont());
if (fc.showDialog(this, "選擇字型") == JDialog.OK_OPTION) {
textArea.setFont(fc.getSelectedFont());
}
// 前景色/背景色
Color fg = JColorChooser.showDialog(this, "文字顏色", textArea.getForeground());
if (fg != null) textArea.setForeground(fg);
Color bg = JColorChooser.showDialog(this, "背景顏色", textArea.getBackground());
if (bg != null) textArea.setBackground(bg);
右鍵選單 (Popup Menu)
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem(cutAction));
popup.add(new JMenuItem(copyAction));
popup.add(new JMenuItem(pasteAction));
popup.addSeparator();
popup.add(new JMenuItem(selectAllAction));
textArea.setComponentPopupMenu(popup); // JDK 5+
// 或滑鼠監聽器手動 show() 相容舊版
4. 事件整合:Action 共用
將「剪下/複製/貼上」等邏輯封裝為 AbstractAction,同時綁定選單項、工具列按鈕、快捷鍵、右鍵選單,避免重複代碼:
Action cutAction = new AbstractAction("剪下", new ImageIcon("cut.png")) {
public void actionPerformed(ActionEvent e) { textArea.cut(); }
};
cutAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
cutAction.putValue(Action.SHORT_DESCRIPTION, "剪下選取文字 Ctrl+X");
5. 狀態列即時更新
{
try {
int pos = textArea.getCaretPosition();
int line = textArea.getLineOfOffset(pos) + 1;
int col = pos - textArea.getLineStartOffset(line - 1) + 1;
statusLeft.setText(String.format("行: %d 列: %d", line, col));
statusRight.setText(dirty ? "● 已修改" : "● 已儲存");
} catch (BadLocationException ignored) {}
});
打包部署:生成可執行 JAR
- Project → Project Properties → Deployment → 新增 JAR 配置
- Main Class:
texteditor.TextEditFrame
- 勾選「Include Manifest」、設定
Class-Path 包含 dbSwing 等依賴 JAR
- Build → Make JAR → 產出
TextEditor.jar
- 執行驗證:
java -jar TextEditor.jar
遷移建議:從 JBuilder 邁向現代化
- 原始碼無縫遷移至 IntelliJ IDEA / Eclipse / VS Code
- 建構腳本改用 Maven/Gradle,依賴管理自動化
- UI 設計可改用 Swing 手寫代碼、JavaFX SceneBuilder 或 Web 技術
- 打包改用
maven-shade-plugin 或 jlink 生成原生映像
雖然 JBuilder 已成歷史,但其視覺化開發流程、事件綁定、Action 架構思想,至今仍是桌面應用開發的寶貴經驗資產。
- 上一篇文章: EJB 開發模式全解析:從手工構建到 IDE 集成再到建模驅動
- 下一篇文章: 大道至簡:Java 23 種設計模式一點就通
Copyright ©
程式碼之家 All Rights Reserved