Skip to content

基于Java Swing的文本编辑器

很俗的东西了...纯属学习用的...

Anyway...

x1pgoHCJCnTYmHkhFn5H1wKzjXr7UG6fnoGjRv35 Kufpx9FOspSnibrVXkz8v LNcOQPC qsSmC5sERlzKBWZi8c0CW3Ei3TNJooat6o 6UXvhWBUcwCW5pJsz PeRENQsmXQQEqdJ2Og

package uitest;
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;

public class TextEditClass {
  boolean packFrame = false;
/**
  * Construct and show the application.
  */
  public TextEditClass() {
  TextEditFrame frame = new TextEditFrame();
  // Validate frames that have preset sizes
  // Pack frames that have useful preferred size info, e.g. from their layout
  if (packFrame) {
  frame.pack();
  } else {
  frame.validate();
  }

// Center the window
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frameSize = frame.getSize();
  if (frameSize.height > screenSize.height) {
  frameSize.height = screenSize.height;
  }
  if (frameSize.width > screenSize.width) {
  frameSize.width = screenSize.width;
  }
  frame.setLocation((screenSize.width - frameSize.width) / 2,
  (screenSize.height - frameSize.height) / 2);
  frame.setVisible(true);
  }

/**
  * Application entry point.
  *
  * @param args String[]
  */
  public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
  public void run() {
  try {
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception exception) {
  exception.printStackTrace();
  }

new TextEditClass();
  }
  });
  }
}
package uitest;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 *
Title:

 *
 *
Description:

 *
 *
Copyright: Copyright (c) 2006

 *
 *
Company:

 *
 * @author not attributable
 * @version 1.0
 */
public class TextEditFrame_AboutBox extends JDialog implements ActionListener {
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel insetsPanel1 = new JPanel();
  JPanel insetsPanel2 = new JPanel();
  JPanel insetsPanel3 = new JPanel();
  JButton button1 = new JButton();
  JLabel imageLabel = new JLabel();
  JLabel label1 = new JLabel();
  JLabel label2 = new JLabel();
  JLabel label3 = new JLabel();
  JLabel label4 = new JLabel();
  ImageIcon image1 = new ImageIcon();
  BorderLayout borderLayout1 = new BorderLayout();
  BorderLayout borderLayout2 = new BorderLayout();
  FlowLayout flowLayout1 = new FlowLayout();
  GridLayout gridLayout1 = new GridLayout();
  String product = "";
  String version = "1.0";
  String copyright = "Copyright (c) 2006";
  String comments = "";

public TextEditFrame_AboutBox(Frame parent) {
  super(parent);
  try {
  setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  jbInit();
  } catch (Exception exception) {
  exception.printStackTrace();
  }
  }

public TextEditFrame_AboutBox() {
  this(null);
  }

/**
  * Component initialization.
  *
  * @throws java.lang.Exception
  */
  private void jbInit() throws Exception {
  image1 = new ImageIcon(uitest.TextEditFrame.class.getResource(
  "about.png"));
  imageLabel.setIcon(image1);
  setTitle("About");
  panel1.setLayout(borderLayout1);
  panel2.setLayout(borderLayout2);
  insetsPanel1.setLayout(flowLayout1);
  insetsPanel2.setLayout(flowLayout1);
  insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  gridLayout1.setRows(4);
  gridLayout1.setColumns(1);
  label1.setText(product);
  label2.setText(version);
  label3.setText(copyright);
  label4.setText(comments);
  insetsPanel3.setLayout(gridLayout1);
  insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10, 60, 10, 10));
  button1.setText("OK");
  button1.addActionListener(this);
  insetsPanel2.add(imageLabel, null);
  panel2.add(insetsPanel2, BorderLayout.WEST);
  getContentPane().add(panel1, null);
  insetsPanel3.add(label1, null);
  insetsPanel3.add(label2, null);
  insetsPanel3.add(label3, null);
  insetsPanel3.add(label4, null);
  panel2.add(insetsPanel3, BorderLayout.CENTER);
  insetsPanel1.add(button1, null);
  panel1.add(insetsPanel1, BorderLayout.SOUTH);
  panel1.add(panel2, BorderLayout.NORTH);
  setResizable(true);
  }

/**
  * Close the dialog on a button event.
  *
  * @param actionEvent ActionEvent
  */
  public void actionPerformed(ActionEvent actionEvent) {
  if (actionEvent.getSource() == button1) {
  dispose();
  }
  }
}
package uitest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.borland.dbswing.FontChooser;
import javax.swing.JFileChooser;
import com.borland.dbswing.IntlSwingSupport;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.text.Document;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import com.borland.dbswing.DBTextDataBinder;

/**
 *
Title:

 *
 *
Description:

 *
 *
Copyright: Copyright (c) 2006

 *
 *
Company:

 *
 * @author not attributable
 * @version 1.0
 */
public class TextEditFrame extends JFrame {
  IntlSwingSupport intlSwingSupport1 = new IntlSwingSupport();
  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();
  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 jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  ImageIcon image1 = new ImageIcon(uitest.TextEditFrame.class.getResource(
  "openFile.png"));
  ImageIcon image2 = new ImageIcon(uitest.TextEditFrame.class.getResource(
  "closeFile.png"));
  ImageIcon image3 = new ImageIcon(uitest.TextEditFrame.class.getResource(
  "help.png"));
  JLabel statusBar = new JLabel();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();
  JMenuItem jMenuItem1 = new JMenuItem();
  JMenuItem jMenuItem2 = new JMenuItem();
  JMenuItem jMenuItem3 = new JMenuItem();
  JMenuItem jMenuItem4 = new JMenuItem();
  JMenu jMenu1 = new JMenu();
  JMenuItem jMenuItem5 = new JMenuItem();
  JMenuItem jMenuItem6 = new JMenuItem();
  JMenuItem jMenuItem7 = new JMenuItem();
  FontChooser fontChooser1 = new FontChooser();
  JFileChooser jFileChooser1 = new JFileChooser();
  String currFileName = null;
  boolean dirty = false;
  Document document1 = jTextArea1.getDocument();
  DBTextDataBinder dBTextDataBinder1 = new DBTextDataBinder();

public TextEditFrame() {
  try {
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  jbInit();
  updateCaption();
  } catch (Exception exception) {
  exception.printStackTrace();
  }
  }

/**
  * Component initialization.
  *
  * @throws java.lang.Exception
  */
  private void jbInit() throws Exception {
  contentPane = (JPanel) getContentPane();
  contentPane.setLayout(borderLayout1);
  dBTextDataBinder1.setJTextComponent(jTextArea1);
  dBTextDataBinder1.setEnableFileLoading(false);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  setSize(new Dimension(400, 300));
  setTitle("Text Edit");
  this.addWindowListener(new TextEditFrame_this_windowAdapter(this));
  statusBar.setText(" ");
  jMenuFile.setText("File");
  jMenuFileExit.setText("Exit");
  jMenuFileExit.addActionListener(new
  TextEditFrame_jMenuFileExit_ActionAdapter(this));
  jMenuHelp.setText("Help");
  jMenuHelpAbout.setText("About");
  jMenuHelpAbout.addActionListener(new
  TextEditFrame_jMenuHelpAbout_ActionAdapter(this));
  jTextArea1.setLineWrap(true);
  jTextArea1.setWrapStyleWord(true);
  jMenuItem1.setText("New");
  jMenuItem1.addActionListener(new TextEditFrame_jMenuItem1_actionAdapter(this));
  jMenuItem2.setText("Open");
  jMenuItem3.setText("Save");
  jMenuItem3.addActionListener(new TextEditFrame_jMenuItem3_actionAdapter(this));
  jMenuItem4.setText("Save As");
  jMenuItem4.addActionListener(new TextEditFrame_jMenuItem4_actionAdapter(this));
  jMenu1.setText("Edit");
  jMenuItem5.addActionListener(new TextEditFrame_jMenuItem5_actionAdapter(this));
  jMenuItem6.setText("Foreground Color");
  jMenuItem6.addActionListener(new TextEditFrame_jMenuItem6_actionAdapter(this));
  jMenuItem7.setText("Background Color");
  jMenuItem7.addActionListener(new TextEditFrame_jMenuItem7_actionAdapter(this));
  fontChooser1.setFrame(this);
  fontChooser1.setTitle("Font");
  jMenuItem5.setText("Font");
  jMenuItem2.addActionListener(new TextEditFrame_jMenuItem2_actionAdapter(this));
  jButton1.addActionListener(new TextEditFrame_jButton1_actionAdapter(this));
  jButton2.addActionListener(new TextEditFrame_jButton2_actionAdapter(this));
  jButton3.addActionListener(new TextEditFrame_jButton3_actionAdapter(this));
  document1.addDocumentListener(new
  TextEditFrame_document1_documentAdapter(this));
  dBTextDataBinder1.setJTextComponent(jTextArea1);
  dBTextDataBinder1.setEnableFileSaving(false);
  jMenuBar1.add(jMenuFile);
  jMenuBar1.add(jMenu1);
  jMenuFile.add(jMenuItem1);
  jMenuFile.add(jMenuItem2);
  jMenuFile.add(jMenuItem3);
  jMenuFile.add(jMenuItem4);
  jMenuFile.addSeparator();
  jMenuFile.add(jMenuFileExit);
  jMenuBar1.add(jMenuHelp);
  jMenuHelp.add(jMenuHelpAbout);
  setJMenuBar(jMenuBar1);
  jButton1.setIcon(image1);
  jButton1.setToolTipText("Open File");
  jButton2.setIcon(image2);
  jButton2.setToolTipText("Save File");
  jButton3.setIcon(image3);
  jButton3.setToolTipText("About");
  jToolBar.add(jButton1);
  jToolBar.add(jButton2);
  jToolBar.add(jButton3);
  jMenu1.add(jMenuItem5);
  jMenu1.add(jMenuItem6);
  jMenu1.add(jMenuItem7);
  contentPane.add(statusBar, java.awt.BorderLayout.SOUTH);
  contentPane.add(jToolBar, java.awt.BorderLayout.NORTH);
  contentPane.add(jScrollPane1, java.awt.BorderLayout.CENTER);
  jScrollPane1.getViewport().add(jTextArea1);
  }

/**
  * File | Exit action performed.
  *
  * @param actionEvent ActionEvent
  */
  void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
  if (okToAbandon()) {
  System.exit(0);
  }
  }

/**
  * Help | About action performed.
  *
  * @param actionEvent ActionEvent
  */
  void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
  helpAbout();
  }

private void helpAbout() {
  TextEditFrame_AboutBox dlg = new TextEditFrame_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();
  }

public void jMenuItem5_actionPerformed(ActionEvent e) {

fontChooser1.setSelectedFont(jTextArea1.getFont());
  if (fontChooser1.showDialog()) {
  jTextArea1.setFont(fontChooser1.getSelectedFont());
  }
  }

public void jMenuItem6_actionPerformed(ActionEvent e) {
  Color color = javax.swing.JColorChooser.showDialog(this,
  "Foreground Color", jTextArea1.getForeground());
  if (color != null) {
  jTextArea1.setForeground(color);
  }
  }

public void jMenuItem7_actionPerformed(ActionEvent e) {
  Color color = javax.swing.JColorChooser.showDialog(this,
  "Background Color", jTextArea1.getForeground());
  if (color != null) {
  jTextArea1.setBackground(color);
  }
  }

public void jMenuItem1_actionPerformed(ActionEvent e) {
  if (okToAbandon()) {
  jTextArea1.setText("");
  currFileName = null;
  dirty = false;
  updateCaption();
  }
  }

public void jMenuItem2_actionPerformed(ActionEvent e) {
  fileOpen();
  }

private void fileOpen() throws HeadlessException {
  if (!okToAbandon()) {
  return;
  }

if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
  openfile(jFileChooser1.getSelectedFile().getPath());
  this.repaint();
  }
  }
  void openfile(String fileName) {
  try {
  File file = new File(fileName);
  int size = (int) file.length();
  int char_read = 0;
  FileReader in = new FileReader(file);
  char[] data = new char;
  while (in.ready()) {
  char_read += in.read(data, char_read, size - char_read);
  }
  in.close();
  jTextArea1.setText(new String(data, 0, char_read));
  this.currFileName = fileName;
  this.dirty = false;
  statusBar.setText("Opend " + fileName);
  updateCaption();
  } catch (IOException e) {
  statusBar.setText("Error opening " + fileName);
  }
  }

boolean saveFile() {
  if (currFileName == null) {
  return saveAsFile();
  }
  try {
  File file = new File(currFileName);
  FileWriter out = new FileWriter(file);
  String text = jTextArea1.getText();
  out.write(text);
  out.close();
  this.dirty = false;
  statusBar.setText("Saved to " + currFileName);
  updateCaption();
  return true;
  } catch (IOException e) {
  statusBar.setText("Error saving " + currFileName);
  }
  return false;
  }

boolean saveAsFile() {
  if (jFileChooser1.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) {
  currFileName = jFileChooser1.getSelectedFile().getPath();
  this.repaint();
  return saveFile();
  } else {
  this.repaint();
  return false;
  }
  }

public void jMenuItem3_actionPerformed(ActionEvent e) {
  saveFile();
  }

public void jMenuItem4_actionPerformed(ActionEvent e) {
  saveAsFile();
  }

boolean okToAbandon() {
  if (!dirty) {
  return true;
  }
  int value = JOptionPane.showConfirmDialog(this, "Save changes?",
  "Text Edit",
  JOptionPane.
  YES_NO_CANCEL_OPTION);
  switch (value) {
  case JOptionPane.YES_OPTION:
  return saveFile();
  case JOptionPane.NO_OPTION:
  return true;
  default:
  return false;
  }
  }

public void jButton1_actionPerformed(ActionEvent e) {
  fileOpen();
  }

public void jButton2_actionPerformed(ActionEvent e) {
  saveFile();
  }

public void jButton3_actionPerformed(ActionEvent e) {
  helpAbout();
  }

public void document1_changedUpdate(DocumentEvent e) {
  dirty=true;
  updateCaption();
  }

public void document1_insertUpdate(DocumentEvent e) {
  dirty=true;
  updateCaption();
  }

public void document1_removeUpdate(DocumentEvent e) {
  dirty=true;

updateCaption();}
  void updateCaption()
  {
  String caption;
  if(currFileName==null)
  {
  caption="Untitled";
  }
  else
  {
  caption=currFileName;
  }
  if(dirty)
  {
  caption="*"+caption;
  }
  caption="Text Editor - "+caption;
  this.setTitle(caption);
  }

public void this_windowClosing(WindowEvent e) {
  okToAbandon();
  }

}
class TextEditFrame_this_windowAdapter extends WindowAdapter {
  private TextEditFrame adaptee;
  TextEditFrame_this_windowAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void windowClosing(WindowEvent e) {

adaptee.this_windowClosing(e);
  }
}
class TextEditFrame_document1_documentAdapter implements DocumentListener {
  private TextEditFrame adaptee;
  TextEditFrame_document1_documentAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void insertUpdate(DocumentEvent e) {
  adaptee.document1_insertUpdate(e);
  }

public void removeUpdate(DocumentEvent e) {
  adaptee.document1_removeUpdate(e);
  }

public void changedUpdate(DocumentEvent e) {
  adaptee.document1_changedUpdate(e);
  }
}
class TextEditFrame_jButton3_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jButton3_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jButton3_actionPerformed(e);
  }
}
class TextEditFrame_jButton2_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jButton2_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jButton2_actionPerformed(e);
  }
}
class TextEditFrame_jButton1_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jButton1_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jButton1_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem3_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem3_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem3_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem4_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem4_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem4_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem2_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem2_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem2_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem7_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem7_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem7_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem1_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem1_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem1_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem5_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem5_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem5_actionPerformed(e);
  }
}
class TextEditFrame_jMenuItem6_actionAdapter implements ActionListener {
  private TextEditFrame adaptee;
  TextEditFrame_jMenuItem6_actionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent e) {
  adaptee.jMenuItem6_actionPerformed(e);
  }
}
class TextEditFrame_jMenuFileExit_ActionAdapter implements ActionListener {
  TextEditFrame adaptee;

TextEditFrame_jMenuFileExit_ActionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent actionEvent) {
  adaptee.jMenuFileExit_actionPerformed(actionEvent);
  }
}
class TextEditFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
  TextEditFrame adaptee;

TextEditFrame_jMenuHelpAbout_ActionAdapter(TextEditFrame adaptee) {
  this.adaptee = adaptee;
  }

public void actionPerformed(ActionEvent actionEvent) {
  adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
  }
}