/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License.
*/
package examples.texteditor;
import java.awt.FileDialog; import java.io.*;
/** This class is an entry point of the simple text editor. * It creates and shows the main application frame.
*/ publicclass Ted extends javax.swing.JFrame {
/** Ted constructor. * It initializes all GUI components [menu bar, menu items, editor pane, etc.].
*/ public Ted() {
initComponents();
setSize(500,300);
}
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the FormEditor.
*/ privatevoid initComponents() {//GEN-BEGIN:initComponents
textScrollPane = new javax.swing.JScrollPane();
textBox = new javax.swing.JTextArea();
tedMenuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
newMenuItem = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu();
findMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
aboutMenuItem = new javax.swing.JMenuItem();
fileMenu.add(newMenuItem);
newMenuItem.getAccessibleContext().setAccessibleName("New Menu Item");
newMenuItem.getAccessibleContext().setAccessibleDescription("New menu item.");
fileMenu.add(openMenuItem);
openMenuItem.getAccessibleContext().setAccessibleName("Open Menu Item");
openMenuItem.getAccessibleContext().setAccessibleDescription("Open menu item.");
fileMenu.add(saveMenuItem);
saveMenuItem.getAccessibleContext().setAccessibleName("Save Menu Item");
saveMenuItem.getAccessibleContext().setAccessibleDescription("Save menu item.");
fileMenu.add(saveAsMenuItem);
saveAsMenuItem.getAccessibleContext().setAccessibleName("Save As Menu Item");
saveAsMenuItem.getAccessibleContext().setAccessibleDescription("Save As menu item.");
fileMenu.add(exitMenuItem);
exitMenuItem.getAccessibleContext().setAccessibleName("Exit Menu Item");
exitMenuItem.getAccessibleContext().setAccessibleDescription("Exit menu item.");
editMenu.add(findMenuItem);
findMenuItem.getAccessibleContext().setAccessibleName("Find Menu Item");
findMenuItem.getAccessibleContext().setAccessibleDescription("Find menu item.");
helpMenu.add(aboutMenuItem);
aboutMenuItem.getAccessibleContext().setAccessibleName("About Menu Item");
aboutMenuItem.getAccessibleContext().setAccessibleDescription("About menu item.");
setJMenuBar(tedMenuBar);
tedMenuBar.getAccessibleContext().setAccessibleName("Ted Menu Bar");
tedMenuBar.getAccessibleContext().setAccessibleDescription("Ted menu bar.");
}//GEN-END:initComponents
/** This method is called when File -> Save menu item is invoked. * It saves the current opened file. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed if ("".equals(fileName))
doSaveAs(); else
doSave(fileName);
}//GEN-LAST:event_saveMenuItemActionPerformed
/** This method is called when File -> Exit menu item is invoked. * It closes the application. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
System.exit(0);
}//GEN-LAST:event_exitMenuItemActionPerformed
/** This method is called when Edit -> Find menu item is invoked. * It creates and shows the Finder frame to allow the user to search in the text. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid findMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findMenuItemActionPerformed new Finder(this, textBox).show();
}//GEN-LAST:event_findMenuItemActionPerformed
/** This method is called when Help -> About menu item is invoked. * It creates and shows the About dialog. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed new About(this). show();
}//GEN-LAST:event_aboutMenuItemActionPerformed
/** This method is called when File -> Save as menu item is invoked. * It asks for a new file name, then saves the file. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
doSaveAs();
}//GEN-LAST:event_saveAsMenuItemActionPerformed
/** This method is called when File -> Open menu item is invoked. * It displays a dialog to choose the file to be opened and edited. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
FileDialog fileDialog = new FileDialog(this, "Open...", FileDialog.LOAD);
fileDialog.show(); if (fileDialog.getFile() == null) return;
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
if (str != null)
textBox.setText(str);
}//GEN-LAST:event_openMenuItemActionPerformed
/** This method is called when File -> New menu item is invoked. * It clears the editor pane. * @param evt ActionEvent instance passed from actionPerformed event.
*/ privatevoid newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
fileName = "";
textBox.setText("");
}//GEN-LAST:event_newMenuItemActionPerformed
/** This method is called when the application frame is closed. * @param evt WindowEvent instance passed from windowClosing event.
*/ privatevoid exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
/** Saves the current content of editor pane to the file. * @param fileName Name of the file.
*/ privatevoid doSave(String fileName) {
FileOutputStream fos = null;
String str = textBox.getText(); try {
fos = new FileOutputStream(fileName);
fos.write(str.getBytes());
} catch (IOException e) {
} finally { try {
fos.close();
} catch (IOException e2) {
}
}
}
/** Asks for a file name. then saves the current content of editor pane to the file.
*/ privatevoid doSaveAs() {
FileDialog fileDialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
fileDialog.show(); if (fileDialog.getFile() == null) return;
fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
/** Starts the application. * @param args Application arguments.
*/ publicstaticvoid main(java.lang.String[] args) { new Ted().show();
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.14Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.