📄 texteditorframe.java
字号:
package texteditor;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.dbswing.*;
import javax.swing.text.*;
import javax.swing.event.*;
/**
* <p>Title: Text Editor</p>
* <p>Description: This is a Java text editor developed with Borland JBuilder X.</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: bearStudio.com</p>
* @author bear golden
* @version 1.0
*/
public class TextEditorFrame
extends JFrame
{
IntlSwingSupport intlSwingSupport = new IntlSwingSupport(); // Internationalizing Swing component
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButtonOpenFile = new JButton();
JButton jButtonCloseFile = new JButton();
JButton jButtonHelp = new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane scrollPane = new JScrollPane();
JTextArea jTextArea = new JTextArea();
JMenuItem jMenuFileNew = new JMenuItem();
JMenuItem jMenuFileOpen = new JMenuItem();
JMenuItem jMenuFileSave = new JMenuItem();
JMenuItem jMenuFileSaveAs = new JMenuItem();
JMenu jMenuEdit = new JMenu();
JMenuItem jMenuEditFont = new JMenuItem();
JMenuItem jMenuEditForeColor = new JMenuItem();
JMenuItem jMenuEditBackColor = new JMenuItem();
FontChooser jFontChooser = new FontChooser();
JFileChooser jFileChooser = new JFileChooser();
String currentFileName = null; // Full path and filename, null means new/untitled.
boolean dirty = false; // false means the file was not modified initially.
Document document;
DBTextDataBinder dBTextDataBinder1 = new DBTextDataBinder();
//Construct the frame
public TextEditorFrame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
updateCaption();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit()
throws Exception
{
image1 = new ImageIcon(texteditor.TextEditorFrame.class.getResource(
"openFile.png"));
image2 = new ImageIcon(texteditor.TextEditorFrame.class.getResource(
"closeFile.png"));
image3 = new ImageIcon(texteditor.TextEditorFrame.class.getResource(
"help.png"));
contentPane = (JPanel)this.getContentPane();
document = jTextArea.getDocument();
contentPane.setLayout(borderLayout1);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setSize(new Dimension(400, 300));
this.setTitle("Text Editor");
statusBar.setText(" ");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new
TextEditorFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new
TextEditorFrame_jMenuHelpAbout_ActionAdapter(this));
jButtonOpenFile.setIcon(image1);
jButtonOpenFile.addActionListener(new TextEditorFrame_jButtonOpenFile_actionAdapter(this));
jButtonOpenFile.setToolTipText("Open File");
jButtonCloseFile.setIcon(image2);
jButtonCloseFile.addActionListener(new TextEditorFrame_jButtonCloseFile_actionAdapter(this));
jButtonCloseFile.setToolTipText("Close File");
jButtonHelp.setIcon(image3);
jButtonHelp.addActionListener(new TextEditorFrame_jButtonHelp_actionAdapter(this));
jButtonHelp.setToolTipText("Help");
jTextArea.setLineWrap(true);
jTextArea.setWrapStyleWord(true);
jMenuFileNew.setText("New");
jMenuFileNew.addActionListener(new TextEditorFrame_jMenuFileNew_actionAdapter(this));
jMenuFileOpen.setText("Open ...");
jMenuFileOpen.addActionListener(new TextEditorFrame_jMenuFileOpen_actionAdapter(this));
jMenuFileSave.setText("Save");
jMenuFileSave.addActionListener(new TextEditorFrame_jMenuFileSave_actionAdapter(this));
jMenuFileSaveAs.setText("Save as ...");
jMenuFileSaveAs.addActionListener(new TextEditorFrame_jMenuFileSaveAs_actionAdapter(this));
jMenuEdit.setText("Edit");
jMenuEditFont.setText("Font ...");
jMenuEditFont.addActionListener(new TextEditorFrame_jMenuEditFont_actionAdapter(this));
jMenuEditForeColor.setText("Foreground Color ...");
jMenuEditForeColor.addActionListener(new TextEditorFrame_jMenuEditForeColor_actionAdapter(this));
jMenuEditBackColor.setText("Background Color ...");
jMenuEditBackColor.addActionListener(new TextEditorFrame_jMenuEditBackColor_actionAdapter(this));
jFontChooser.setFrame(this);
jFontChooser.setTitle("Font");
jFontChooser.setSampleText("Sample Text");
document.addDocumentListener(new TextEditorFrame_document_documentAdapter(this));
dBTextDataBinder1.setJTextComponent(jTextArea);
dBTextDataBinder1.setEnableFileLoading(false);
dBTextDataBinder1.setEnableFileSaving(false);
jToolBar.add(jButtonOpenFile);
jToolBar.add(jButtonCloseFile);
jToolBar.add(jButtonHelp);
jMenuFile.add(jMenuFileNew);
jMenuFile.add(jMenuFileOpen);
jMenuFile.add(jMenuFileSave);
jMenuFile.add(jMenuFileSaveAs);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuEdit);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
scrollPane.getViewport().add(jTextArea, null);
jMenuEdit.add(jMenuEditFont);
jMenuEdit.add(jMenuEditForeColor);
jMenuEdit.add(jMenuEditBackColor);
}
// Display the About box.
void helpAbout()
{
TextEditorFrame_AboutBox dlg = new TextEditorFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
// Handle the File|Open menu or button, invoking
// okToAbandon and openFile as needed.
void fileOpen()
{
// Handle the File|Open menu item.
if (!okToAbandon())
return;
// Use the OPEN version of the dialog, test return for Approve/Cancel
if (JFileChooser.APPROVE_OPTION == jFileChooser.showOpenDialog(this))
{
// Call openFile() method to attempt to load the text from file into JTextArea
openFile(jFileChooser.getSelectedFile().getPath());
//repaints menu after item is selected
this.repaint();
}
}
// the implementation of openFile() method
// Open named file; read text from file into jTextArea1; report to statusBar.
void openFile(String fileName)
{
try
{
// Open a file of the given name.
File file = new File(fileName);
// Get the size of the opened file.
int size = (int) file.length();
// Set to zero a counter for counting the number of
// characters that have been read from the file.
int chars_read = 0;
// Create an input reader based on the file, so we can read its data.
// FileReader handles international character encoding conversions.
FileReader in = new FileReader(file);
// Create a character array of the size of the file,
// to use as a data buffer, into which we will read the text data.
char[] data = new char[size];
// Read all available characters into the buffer.
while (in.ready())
{
// Increment the count for each character read,
// and accumulate them in the data buffer.
chars_read += in.read(data, chars_read, size - chars_read);
}
in.close();
// Create a temporary string containing the data,
// and set the string into the JTextArea.
jTextArea.setText(new String(data, 0, chars_read));
// Cache the currently opened filename for use at save time...
this.currentFileName = fileName;
// ...and mark the edit session as being clean
this.dirty = false;
// Display the name of the opened directory + file in the statusBar.
statusBar.setText("Opened " + fileName);
updateCaption();
}
catch (IOException e)
{
statusBar.setText("Error opening " + fileName);
}
}
// the implementation of saveFile() method
// Save current file; handle not yet having a filename; report to statusBar.
boolean saveFile()
{
// Handle the case where we don't have a file name yet.
if (currentFileName == null)
{
return saveAsFile();
}
try
{
// Open a file of the current name.
File file = new File(currentFileName);
// Create an output writer that will write to that file.
// FileWriter handles international characters encoding conversions.
FileWriter out = new FileWriter(file);
String text = jTextArea.getText();
out.write(text);
out.close();
this.dirty = false;
// Display the name of the saved directory+file in the statusBar.
statusBar.setText("Saved to " + currentFileName);
updateCaption();
return true;
}
catch (IOException e)
{
statusBar.setText("Error saving " + currentFileName);
}
return false;
}
// the implementation of saveAsFile() method
// Save current file, asking user for new destination name.
// Report to statusBar.
boolean saveAsFile()
{
// Use the SAVE version of the dialog, test return for Approve/Cancel
if (JFileChooser.APPROVE_OPTION == jFileChooser.showSaveDialog(this))
{
// Set the current file name to the user's selection,
// then do a regular saveFile
currentFileName = jFileChooser.getSelectedFile().getPath();
//repaints menu after item is selected
this.repaint();
return saveFile();
}
else
{
this.repaint();
return false;
}
}
// Check if file is dirty.
// (when a file has been modified since the last save, it's called a dirty file,)
// If so, prompt for save/don't save/cancel save decision.
boolean okToAbandon()
{
if (!dirty)
return true;
int value = JOptionPane.showConfirmDialog(this, "Save changes?", "Text Editor",
JOptionPane.YES_NO_CANCEL_OPTION);
switch (value)
{
case JOptionPane.YES_OPTION:
// Yes, please save changes
return saveFile();
case JOptionPane.NO_OPTION:
// No, abandon edits; that is, return true without saving
return true;
case JOptionPane.CANCEL_OPTION:
default:
// Cancel the dialog without saving or closing
return false;
}
}
// Update the title bar of the application to show the filename and its dirty state.
void updateCaption()
{
String caption;
if (currentFileName == null)
{
// synthesize the "Untitled" name if no name yet.
caption = "Untitled";
}
else
{
caption = currentFileName;
}
// add a "* " in the caption if the file is dirty.
if (dirty)
{
caption += " *";
}
caption += " - Text Editor";
this.setTitle(caption);
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e)
{
if (okToAbandon())
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -