Java の AWT(Abstract Window Toolkit)を使った GUI アプリケーションを Mac OS 8.1 で作っています。Mac OS 8.1 では、Java のバージョンは JDK 1.1.3 になります。
この章を始める前に Java Essential と Java AWT と Java AWT Menu の章を見ておいた方が良いでしょう。
任意のファイルを開けて、任意の場所に保存できるテキストエディタを目指しています。
まだ作成途中です。途中経過をじょじょに掲載していこうと思っています。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class TextEditor extends WindowAdapter implements ActionListener, TextListener{
static Frame frame;
static TextArea textArea;
static Dialog dialog;
String path = "";
String name = "";
FileDialog fd;
boolean edited = false;
boolean state = true;
boolean opened = false;
public static void main(String[] args) {
// Frame
frame = new Frame("TextEditor");
frame.setSize(500, 400);
textArea = new TextArea();
textArea.setFont(new Font("Monospaced", 0, 12));
frame.add(textArea);
// Dialog
dialog = new Dialog(frame, "TextEditor", true);
dialog.setSize(250, 100);
dialog.setResizable(false);
dialog.setLayout(new FlowLayout());
dialog.add(new Label("This file has changed."));
dialog.add(new Label("Do you want to save your changes?"));
Button yes = (Button)dialog.add(new Button("Yes"));
Button no = (Button)dialog.add(new Button("No" ));
Button cancel = (Button)dialog.add(new Button("Cancel"));
// Menu
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
MenuItem menuNew = new MenuItem("New" , new MenuShortcut('N'));
MenuItem menuOpen = new MenuItem("Open", new MenuShortcut('O'));
MenuItem menuSave = new MenuItem("Save", new MenuShortcut('S'));
MenuItem menuSaveAs=new MenuItem("SaveAs");
frame .setMenuBar(menuBar);
menuBar .add(menuFile);
menuFile.add(menuNew );
menuFile.add(menuOpen);
menuFile.addSeparator();
menuFile.add(menuSave);
menuFile.add(menuSaveAs);
// Evnet Listener
TextEditor textEditor = new TextEditor();
menuFile.addActionListener(textEditor);
yes .addActionListener(textEditor);
no .addActionListener(textEditor);
cancel .addActionListener(textEditor);
frame .addWindowListener(textEditor);
textArea.addTextListener (textEditor);
frame.show();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "New" ) {
if (edited) {
dialog.show();
}
if (state) {
textArea.setText("");
edited = false;
path = "";
name = "";
}
}
if (e.getActionCommand() == "Open") {
if (edited) {
dialog.show();
}
fd = new FileDialog(frame, "Open", FileDialog.LOAD);
fd.setVisible(true);
path = fd.getDirectory();
name = fd.getFile();
try {
File file = new File(path + name);
BufferedReader br =
new BufferedReader(new FileReader(file));
String line, text = "";
while ((line = br.readLine()) != null) {
text += line;
text += System.getProperty("line.separator");
}
textArea.setText(text);
br.close();
edited = false;
} catch (IOException err ) {
System.out.println(err);
}
}
if (e.getActionCommand() == "Save") {
if (name == "") {
fd = new FileDialog(frame, "Save", FileDialog.SAVE);
fd.setVisible(true);
path = fd.getDirectory();
name = fd.getFile();
}
try {
File file = new File(path + name);
BufferedWriter bw =
new BufferedWriter(new FileWriter(file));
bw.write(textArea.getText());
bw.close();
edited = false;
} catch (IOException err) {
System.out.println(err);
}
}
if (e.getActionCommand() == "SaveAs") {
fd = new FileDialog(frame, "Save", FileDialog.SAVE);
fd.setVisible(true);
path = fd.getDirectory();
name = fd.getFile();
try {
File file = new File(path + name);
BufferedWriter bw =
new BufferedWriter(new FileWriter(file));
bw.write(textArea.getText());
bw.close();
edited = false;
} catch (IOException err) {
System.out.println(err);
}
}
if (e.getActionCommand() == "Yes") {
state = true;
dialog.setVisible(false);
if (name == "") {
fd = new FileDialog(frame, "Save", FileDialog.SAVE);
fd.setVisible(true);
path = fd.getDirectory();
name = fd.getFile();
}
try {
File file = new File(path + name);
BufferedWriter bw =
new BufferedWriter(new FileWriter(file));
bw.write(textArea.getText());
bw.close();
} catch (IOException err) {
System.out.println(err);
}
}
if (e.getActionCommand() == "No" ) {
state = true;
dialog.setVisible(false);
}
if (e.getActionCommand() == "Cancel") {
state = false;
dialog.setVisible(false);
}
}
public void textValueChanged(TextEvent e) {
if (opened) {
edited = true;
}
opened = true;
}
public void windowClosing(WindowEvent e) {
if (edited) {
dialog.show();
}
if (state) {
System.exit(0);
}
}
}