addWindowListener(this); } public void windowClosing(WindowEvent we) { System.exit(0); } public void windowIconified(WindowEvent we) { } public void windowDeiconified(WindowEvent we) { } public void windowClosed(WindowEvent we) { } public void windowOpened(WindowEvent we) { } public void windowDeactivated(WindowEvent we) { } public void windowActivated(WindowEvent we) { } public void actionPerformed(ActionEvent e) { area.setForeground(Color.black); area.setEditable(true);
FileDialog fdopen = new FileDialog(this,"Open",FileDialog.LOAD); FileDialog fdsave = new FileDialog(this,"Save",FileDialog.SAVE); if(e.getSource() == about) { JOptionPane.showMessageDialog(null,"Note Vs.1.0 Developed By Lixin Zhang" + "\n" + "Email me at - [email protected]"); } if (e.getSource() == bzzt) { String msg = "WELCOME TO NOTE DEVELOPED BY ZHANG LIXIN" + "\n" + "\n" + "NOTE is exactly identical to the 'Notepad' provided by 'WINDOWS'" + "\n" + "\n" + "Email all queries to : [email protected]";
package com.borland.samples.swing.simpletexteditor;import javax.swing.UIManager;
import java.awt.*;public class SimpleTextEditClass {
boolean packFrame = false; //Construct the application
public SimpleTextEditClass() {
SimpleTextEditFrame frame = new SimpleTextEditFrame();
//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);
} //Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new SimpleTextEditClass();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class SimpleTextEditFrame_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 imageControl1 = new JLabel();
ImageIcon imageIcon;
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
GridLayout gridLayout1 = new GridLayout();
String product = "SimpleTextEdit Tutorial";
String version = "Version 1.0";
String copyright = "Copyright (c) 2001";
String comments = "Creates a simple Java text editor application.";
public SimpleTextEditFrame_AboutBox(Frame parent) {
super(parent);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
//imageControl1.setIcon(imageIcon);
pack();
} private void jbInit() throws Exception {
//imageIcon = new ImageIcon(getClass().getResource("[Your Image]"));
this.setTitle("About");
setResizable(false);
panel1.setLayout(borderLayout1);
panel2.setLayout(borderLayout2);
insetsPanel1.setLayout(flowLayout1);
insetsPanel2.setLayout(flowLayout1);
insetsPanel2.setBorder(new EmptyBorder(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(new EmptyBorder(10, 60, 10, 10));
button1.setText("Ok");
button1.addActionListener(this);
insetsPanel2.add(imageControl1, null);
panel2.add(insetsPanel2, BorderLayout.WEST);
this.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);
} protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
cancel();
}
super.processWindowEvent(e);
} void cancel() {
dispose();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
cancel();
}
}
}package com.borland.samples.swing.simpletexteditor;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;public class SimpleTextEditFrame extends JFrame {
JPanel contentPane;
JMenuBar menuBar1 = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
JMenu menuHelp = new JMenu();
JMenuItem menuHelpAbout = new JMenuItem();
JToolBar toolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
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 jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
JFileChooser jFileChooser1 = new JFileChooser();
String currFileName = null; // Full path with filename. null means new/untitled.
boolean dirty = false;
Document document1; //Construct the frame
public SimpleTextEditFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
updateCaption();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
image1 = new ImageIcon(SimpleTextEditFrame.class.getResource("openFile.gif"));
image2 = new ImageIcon(SimpleTextEditFrame.class.getResource("closeFile.gif"));
image3 = new ImageIcon(SimpleTextEditFrame.class.getResource("help.gif"));
contentPane = (JPanel) this.getContentPane();
document1 = jTextArea1.getDocument();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Text Editor");
statusBar.setText(" ");
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new TextEditFrame_menuFileExit_ActionAdapter(this));
menuHelp.setText("Help");
menuHelpAbout.setText("About");
menuHelpAbout.addActionListener(new TextEditFrame_menuHelpAbout_ActionAdapter(this));
jButton1.setIcon(image1);
jButton1.addActionListener(new TextEditFrame_jButton1_actionAdapter(this));
jButton1.setToolTipText("Open File");
jButton2.setIcon(image2);
jButton2.addActionListener(new TextEditFrame_jButton2_actionAdapter(this));
jButton2.setToolTipText("Save File");
jButton3.setIcon(image3);
jButton3.addActionListener(new TextEditFrame_jButton3_actionAdapter(this));
jButton3.setToolTipText("About");
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setBackground(Color.white);
jMenuItem1.setText("New");
jMenuItem1.addActionListener(new TextEditFrame_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("Open");
jMenuItem2.addActionListener(new TextEditFrame_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("Save");
jMenuItem3.addActionListener(new TextEditFrame_jMenuItem3_actionAdapter(this));
jMenuItem4.setText("Save As");
jMenuItem4.addActionListener(new TextEditFrame_jMenuItem4_actionAdapter(this));
jMenu1.setText("Edit");
jMenuItem6.setText("Foreground Color");
jMenuItem6.addActionListener(new TextEditFrame_jMenuItem6_actionAdapter(this));
jMenuItem7.setText("Background Color");
jMenuItem7.addActionListener(new TextEditFrame_jMenuItem7_actionAdapter(this));
document1.addDocumentListener(new TextEditFrame_document1_documentAdapter(this));
toolBar.add(jButton1);
toolBar.add(jButton2);
toolBar.add(jButton3);
menuFile.add(jMenuItem1);
menuFile.add(jMenuItem2);
menuFile.add(jMenuItem3);
menuFile.add(jMenuItem4);
menuFile.addSeparator();
menuFile.add(menuFileExit);
menuHelp.add(menuHelpAbout);
menuBar1.add(menuFile);
menuBar1.add(jMenu1);
menuBar1.add(menuHelp);
this.setJMenuBar(menuBar1);
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
jMenu1.add(jMenuItem6);
jMenu1.add(jMenuItem7); } // Display the About box.
void helpAbout() {
SimpleTextEditFrame_AboutBox dlg = new SimpleTextEditFrame_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.show();
}
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 chars_read = 0;
FileReader in = new FileReader(file);
char[] data = new char[size];
while(in.ready()) {
chars_read += in.read(data, chars_read, size - chars_read);
}
in.close();
jTextArea1.setText(new String(data, 0, chars_read));
this.currFileName = fileName;
this.dirty = false;
statusBar.setText("Opened "+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() {
this.repaint();
if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) {
currFileName = jFileChooser1.getSelectedFile().getPath();
this.repaint();
return saveFile();
}
else {
this.repaint();
return false;
}
}
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;
case JOptionPane.CANCEL_OPTION:
default:
return false;
}
}
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 fileExit_actionPerformed(ActionEvent e) {
if (okToAbandon()) {
System.exit(0);
}
}
public void helpAbout_actionPerformed(ActionEvent e) {
helpAbout();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
fileExit_actionPerformed(null);
}
}
void jMenuItem6_actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(this,"Foreground Color",jTextArea1.getForeground());
if (color != null) {
jTextArea1.setForeground(color);
}
this.repaint();
}
void jMenuItem7_actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(this,"Background Color",jTextArea1.getBackground());
if (color != null) {
jTextArea1.setBackground(color);
}
this.repaint();
}
void jMenuItem1_actionPerformed(ActionEvent e) {
if (okToAbandon()) {
jTextArea1.setText("");
currFileName = null;
dirty = false;
updateCaption();
}
}
void jMenuItem2_actionPerformed(ActionEvent e) {
fileOpen();
}
void jMenuItem3_actionPerformed(ActionEvent e) {
saveFile();
}
void jMenuItem4_actionPerformed(ActionEvent e) {
saveAsFile();
}
void jButton1_actionPerformed(ActionEvent e) {
fileOpen();
}
void jButton2_actionPerformed(ActionEvent e) {
saveFile();
}
void jButton3_actionPerformed(ActionEvent e) {
helpAbout();
}
void document1_changedUpdate(DocumentEvent e) {
if (!dirty) {
dirty = true;
updateCaption();
}
}
void document1_insertUpdate(DocumentEvent e) {
if (!dirty) {
dirty = true;
updateCaption();
}
}
void document1_removeUpdate(DocumentEvent e) {
if (!dirty) {
dirty = true;
updateCaption();
}
}
}
class TextEditFrame_menuFileExit_ActionAdapter implements ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_menuFileExit_ActionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.fileExit_actionPerformed(e);
}
}
class TextEditFrame_menuHelpAbout_ActionAdapter implements ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_menuHelpAbout_ActionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.helpAbout_actionPerformed(e);
}
}
class TextEditFrame_jMenuItem6_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jMenuItem6_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem6_actionPerformed(e);
}
}
class TextEditFrame_jMenuItem7_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jMenuItem7_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem7_actionPerformed(e);
}
}
class TextEditFrame_jMenuItem1_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jMenuItem1_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class TextEditFrame_jMenuItem2_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jMenuItem2_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem2_actionPerformed(e);
}
}
class TextEditFrame_jMenuItem3_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jMenuItem3_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem3_actionPerformed(e);
}
}
class TextEditFrame_jMenuItem4_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jMenuItem4_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem4_actionPerformed(e);
}
}
SimpleTextEditFrame adaptee;
TextEditFrame_jButton1_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class TextEditFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jButton2_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class TextEditFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {
SimpleTextEditFrame adaptee;
TextEditFrame_jButton3_actionAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class TextEditFrame_document1_documentAdapter implements javax.swing.event.DocumentListener {
SimpleTextEditFrame adaptee;
TextEditFrame_document1_documentAdapter(SimpleTextEditFrame adaptee) {
this.adaptee = adaptee;
}
public void changedUpdate(DocumentEvent e) {
adaptee.document1_changedUpdate(e);
}
public void insertUpdate(DocumentEvent e) {
adaptee.document1_insertUpdate(e);
}
public void removeUpdate(DocumentEvent e) {
adaptee.document1_removeUpdate(e);
}
}
我用1.4.1
编译时有一点小问题,已解决下面是我改编的(简陋),希望有人能提一些意见!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.print.*;
import java.util.*;
public class Note extends JFrame implements ActionListener,WindowListener
{
String flag = "n";
String filee = "";
String directory = ""; MenuBar mb=new MenuBar() ;
Menu file = new Menu("file") ;
Menu help = new Menu("help") ;
MenuItem news =new MenuItem("new") ;
MenuItem save =new MenuItem("save") ;
MenuItem open =new MenuItem("open") ;
MenuItem exit =new MenuItem("exit") ;
MenuItem about =new MenuItem("about") ;
MenuItem bzzt =new MenuItem("bzzt") ;
JPanel pn =new JPanel();
TextArea area=new TextArea(50,50);
public Note()
{ setTitle("This is lx's Note program");
setSize(800,600);
setVisible(true);
file.add(news);
file.add(open);
file.add(save);
file.add(exit);
news.addActionListener(this);
save.addActionListener(this);
open.addActionListener(this);
exit.addActionListener(this);
help.add(about);
help.add(bzzt);
about.addActionListener(this);
bzzt.addActionListener(this); mb.add(file);
mb.add(help); setMenuBar(mb);
Container c = getContentPane();
c.add(area);
addWindowListener(this);
}
public void windowClosing(WindowEvent we)
{ System.exit(0);
}
public void windowIconified(WindowEvent we)
{
}
public void windowDeiconified(WindowEvent we)
{
}
public void windowClosed(WindowEvent we)
{
}
public void windowOpened(WindowEvent we)
{
}
public void windowDeactivated(WindowEvent we)
{
} public void windowActivated(WindowEvent we)
{
}
public void actionPerformed(ActionEvent e)
{
area.setForeground(Color.black);
area.setEditable(true);
FileDialog fdopen = new FileDialog(this,"Open",FileDialog.LOAD);
FileDialog fdsave = new FileDialog(this,"Save",FileDialog.SAVE); if(e.getSource() == about)
{
JOptionPane.showMessageDialog(null,"Note Vs.1.0 Developed By Lixin Zhang" + "\n" + "Email me at - [email protected]");
}
if (e.getSource() == bzzt)
{
String msg = "WELCOME TO NOTE DEVELOPED BY ZHANG LIXIN" + "\n" + "\n" + "NOTE is exactly identical to the 'Notepad' provided by 'WINDOWS'" + "\n" + "\n" + "Email all queries to : [email protected]";
area.setText(msg);
area.setForeground(Color.blue);
area.setEditable(false);
}
if(e.getSource() == news)
{
area.setText("");
area.requestFocus();
}
if(e.getSource() == exit)
{
System.exit(0);
}
if (e.getSource()==open)
{
flag = "y";
fdopen.setVisible(true);
filee = fdopen.getFile();
directory = fdopen.getDirectory();
File openfile = new File(directory,filee);
try
{
RandomAccessFile ram2 = new RandomAccessFile(openfile,"rw");
long size = ram2.length();
String msg = "";
for(int counter=0;counter<size;counter++)
{
char words = (char)ram2.read();
msg = msg + words;
}
area.setText(msg);
}
catch(Exception e1)
{
System.out.println(e1);
}
}
if (e.getSource() == save)
{
if (flag.equals("y"))
{
File delme = new File(directory,filee);
String a = directory;
String b = filee;
try
{
delme.delete();
}
catch(SecurityException eeme)
{
System.out.println(eeme);
}
File filesaveit = new File(a,b);
try
{
RandomAccessFile ram = new RandomAccessFile(a+b,"rw");
//ram.seek(ram.length());
String msh = area.getText();
ram.writeBytes(msh);
ram.close();
}
catch(Exception rt)
{
System.out.println(rt);
}
}
if (flag.equals("n"))
{
fdsave.setVisible(true);
String dir = fdsave.getDirectory();
String fil = fdsave.getFile();
File filesave = new File(dir,fil);
try
{
RandomAccessFile ram = new RandomAccessFile(fdsave.getDirectory()+fdsave.getFile()+".txt","rw");
ram.seek(ram.length());
String msh = area.getText();
ram.writeBytes(msh);
ram.close();
}
catch(Exception rt)
{
System.out.println(rt);
}
}
}
}
public static void main(String arg[])
{ Note nn=new Note();
}
}sgr_kk(丝瓜) 大侠的还没有试!