【Java】實現記事本(完整版)

專注的阿熊 發表於 2022-06-23
Java

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.GraphicsEnvironment;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JColorChooser;

import javax.swing.JComboBox;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JToolBar;

import javax.swing.filechooser.FileNameExtensionFilter;

import chenhao.io.TextTool;

public class TextPad {

private JTextArea contentArea;

private JFrame frame;

private String fileName;

public TextPad() {

frame = new JFrame(" 記事本 ");

frame.setSize(500, 500);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 新增選單

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu(" 檔案 ");

JMenuItem newItem = new JMenuItem(" 新建 ");

newAction(newItem);

menu.add(newItem);

JMenuItem openItem = new JMenuItem(" 開啟 ");

openAction(openItem);

menu.add(openItem);

JMenuItem saveItem = new JMenuItem(" 儲存 ");

saveAction(saveItem);

menu.add(saveItem);

menuBar.add(menu);

frame.setJMenuBar(menuBar);

// 佈局

frame.setLayout(new BorderLayout());

JToolBar toolBar = new JToolBar();

JComboBox<String> fontCom = fontAction();

toolBar.add(fontCom);

JComboBox<String> fontSize = fontSizeAction();

toolBar.add(fontSize);

fontStyleAction(toolBar);

JButton colorbtn = fontColorAction();

toolBar.add(colorbtn);

frame.add(toolBar, BorderLayout.NORTH);

// 檔案編輯區

contentArea = new JTextArea();

JScrollPane pane = new JScrollPane(contentArea);

frame.add(pane);

frame.setVisible(true);

}

private JButton fontColorAction() {

JButton colorbtn = new JButton(" ");

colorbtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Color color = colorbtn.getForeground();

Color co = JColorChooser.showDialog(TextPad.this.frame, " 設定字型顏色 ", color);

                  colorbtn.setForeground(co);

                  contentArea.setForeground(co);

}

});

return colorbtn;

}

// 記事本,字型格式

private void fontStyleAction(JToolBar toolBar) {

JCheckBox boldBox = new JCheckBox(" 粗體 ");

JCheckBox itBox = new JCheckBox(" 斜體 ");

ActionListener actionListener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

boolean bold = boldBox.isSelected();

boolean it = itBox.isSelected();

int style = (bold ? Font.BOLD : Font.PLAIN) | (it ? Font.ITALIC : Font.PLAIN);

Font font = contentArea.getFont();

contentArea.setFont(new Font(font.getName(), style, font.getSize()));

//contentArea.setFont(new Font(font.getName(), style, font.getSize()));

}

};

boldBox.addActionListener(actionListener);

itBox.addActionListener(actionListener);

toolBar.add(boldBox);

toolBar.add(itBox);

}

// 記事本,設定字型大小

private JComboBox<String> fontSizeAction() {

String[] fontSizes = new String[] { "10", "20", "30", "50" };

JComboBox<String> fontSize = new JComboBox<>(fontSizes);

fontSize.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

int size =外匯跟單gendan5.com Integer.valueOf((String) fontSize.getSelectedItem());

Font font = TextPad.this.contentArea.getFont();

TextPad.this.contentArea.setFont(new Font(font.getName(), font.getStyle(), size));

}

});

return fontSize;

}

// 記事本,設定字型

private JComboBox<String> fontAction() {

GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();

String[] fontNames = environment.getAvailableFontFamilyNames();

JComboBox<String> fontCom = new JComboBox<>(fontNames);

fontCom.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String fontName = (String) fontCom.getSelectedItem();

Font font = TextPad.this.contentArea.getFont();

TextPad.this.contentArea.setFont(new Font(fontName, font.getStyle(), font.getSize()));

}

});

return fontCom;

}

// 記事本新建操作

private void newAction(JMenuItem newItem) {

newItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

contentArea.setText("");

frame.setTitle(" 新建 - 記事本 ");

fileName = null;

}

});

}

// 記事本開啟檔案操作

private void openAction(JMenuItem openItem) {

openItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

JFileChooser chooser = new JFileChooser();

FileNameExtensionFilter filter = new FileNameExtensionFilter("Text & dat", "txt", "dat");

chooser.setFileFilter(filter);

int returnVal = chooser.showOpenDialog(frame);

if (returnVal == JFileChooser.APPROVE_OPTION) {

String fileName = chooser.getSelectedFile().getPath();

TextPad.this.fileName = fileName;

String content = TextTool.read(fileName);

contentArea.setText(content);

TextPad.this.frame.setTitle(fileName + "- 記事本 ");

}

}

});

}

// 選單 儲存操作

private void saveAction(JMenuItem saveItem) {

saveItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (TextPad.this.fileName != null) {

String content = TextPad.this.contentArea.getText();

TextTool.write(TextPad.this.fileName, content);

} else {

JFileChooser chooser = new JFileChooser();

FileNameExtensionFilter filter = new FileNameExtensionFilter("Text & dat", "txt", "dat");

chooser.setFileFilter(filter);

int returnVal = chooser.showSaveDialog(frame);

if (returnVal == JFileChooser.APPROVE_OPTION) {

String fileName = chooser.getSelectedFile().getPath();

TextPad.this.fileName = fileName;

String content = TextPad.this.contentArea.getText();

TextTool.write(TextPad.this.fileName, content);

TextPad.this.frame.setTitle(fileName + "- 記事本 ");

}

}

}

});

}

public static void main(String[] args) {

TextPad pad = new TextPad();

}

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2902491/,如需轉載,請註明出處,否則將追究法律責任。