另外这个Java记事本实现了下打开、编辑、保存、设置字体和颜色等功能
和windows的记事本比起来的话。。。没有撤销和重做,也没有搜索,考虑以后再加
反正本来是拿来交作业的。。。。 萌 约等于 LOLI?
还是处的意思? 萌就是萌的意思 具体请请教半仙 他是专家。。。
当然不是处的意思。。。。 。。。。。顶了混混 可以写CopyLeft CopyLeft???!!!
====================
我用C#做过个记事本...有撤消,搜索(支持正则表达式)替换和跳转...
个人倾向于.net 再仔细一看,感觉楼主的代码很像是j#.net
到底是java 还是.net呢???? java...而且代码不是我的,是Anson的... 我发誓是java。。。如果我不小心写的和.net一样呢也无所谓=。=
另外现在撤销、重做、搜索以及复制剪切粘贴删除的菜单项我也都做上去了
就不放上来了
但是我现在有个问题无法解决
不知道为什么在有时候将字体大小设置的比较大的时候程序就会开始卡
我找不到原因 希望高人帮忙=。=
Anson的Java Notepad
由于某人极度缺乏RP,导致发帖功力尽失,于是在下大发慈悲的为其施法,发帖一篇http://www.eternity3.com.cn/blog/images/smilies/Face_27.gifhttp://www.eternity3.com.cn/blog/images/smilies/Face_27.gifhttp://www.eternity3.com.cn/blog/images/smilies/Face_27.gif源代码及程序下载:
为了让这个帖子看上去比较壮观,把代码贴上来
主类
//Java文本编辑器 by Anson
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.filechooser.FileFilter;
public class TestEditor extends JFrame {
private FileReader input;
private FileWriter output;
private JMenuBar menuBar;
private JMenu fileMenu, formatMenu, helpMenu;
private JMenuItem creat, open, save, saveAs, exit, color, font, about;
private JTextArea editor;
private JScrollPane scroll;
private JFileChooser fileChooser;
private String text;
private File file;
private boolean needSave;
private MyAboutDialog aboutDialog;
private MyColorChooser colorChooser;
private MyFontChooser fontChooser;
private Color foreColor;
private Font editorFont;
public TestEditor() {
super();
needSave = false;
foreColor = Color.BLACK;
editorFont = new Font("宋体", Font.PLAIN, 14);
aboutDialog = new MyAboutDialog(this, "JavaNotePad v 0.1", "Made by Anson", "Copyright 2006(C)", "yuki.gif");
colorChooser = new MyColorChooser(this, Color.BLACK);
fontChooser = new MyFontChooser(this, editorFont);
FileFilter txtFilter = new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory())
return true;
String tmp = file.getName().toLowerCase();
if (tmp.endsWith(".txt"))
return true;
return false;
}
public String getDescription() {
return "文本文档(*.txt)";
}
};
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(txtFilter);
editor = new JTextArea();
editor.setFont(editorFont);
scroll = new JScrollPane(editor);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
menuBar = new JMenuBar();
fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic('f');
creat = new JMenuItem("新建(N)");
creat.setMnemonic('n');
open = new JMenuItem("打开(O)...");
open.setMnemonic('o');
save = new JMenuItem("保存(S)");
save.setMnemonic('s');
saveAs = new JMenuItem("另存为(A)...");
saveAs.setMnemonic('a');
exit = new JMenuItem("退出(X)");
exit.setMnemonic('x');
formatMenu = new JMenu("格式(O)");
formatMenu.setMnemonic('o');
color = new JMenuItem("颜色(C)...");
color.setMnemonic('c');
font = new JMenuItem("字体(F)...");
font.setMnemonic('f');
helpMenu = new JMenu("帮助(H)");
helpMenu.setMnemonic('h');
about = new JMenuItem("关于(A)");
about.setMnemonic('a');
creat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = isNeedSave();
if (i==JOptionPane.YES_OPTION) {
saveFile();
newFile();
} else if (i==JOptionPane.NO_OPTION)
newFile();
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = isNeedSave();
if (i==JOptionPane.YES_OPTION) {
saveFile();
openFile();
} else if (i==JOptionPane.NO_OPTION)
openFile();
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
saveFile();
}
});
saveAs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
saveFile();
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = isNeedSave();
if (i==JOptionPane.YES_OPTION) {
saveFile();
System.exit(0);
} else if (i==JOptionPane.NO_OPTION)
System.exit(0);
}
});
color.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
Point p = colorChooser.getOwner().getLocation();
colorChooser.setLocation(p.x+60, p.y+100);
colorChooser.setChoose(foreColor);
colorChooser.setVisible(true);
foreColor = colorChooser.getChoose();
editor.setForeground(foreColor);
}
});
font.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
Point p = colorChooser.getOwner().getLocation();
fontChooser.setLocation(p.x+60, p.y+100);
fontChooser.setChoose(editorFont);
fontChooser.setVisible(true);
editorFont = fontChooser.getChoose();
editor.setFont(editorFont);
}
});
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Point p = aboutDialog.getOwner().getLocation();
aboutDialog.setLocation(p.x+60, p.y+100);
aboutDialog.setVisible(true);
}
});
editor.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent de) {
needSave = true;
updataTitleAndMenu();
}
public void insertUpdate(DocumentEvent de) {
needSave = true;
updataTitleAndMenu();
}
public void removeUpdate(DocumentEvent de) {
needSave = true;
updataTitleAndMenu();
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
int i = isNeedSave();
if (i==JOptionPane.YES_OPTION) {
saveFile();
System.exit(0);
} else if (i==JOptionPane.NO_OPTION)
System.exit(0);
}
});
fileMenu.add(creat);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(saveAs);
fileMenu.addSeparator();
fileMenu.add(exit);
formatMenu.add(color);
formatMenu.add(font);
helpMenu.add(about);
menuBar.add(fileMenu);
menuBar.add(formatMenu);
menuBar.add(helpMenu);
this.setJMenuBar(menuBar);
this.add(scroll);
this.setPreferredSize(new Dimension(600, 700));
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.pack();
this.updataTitleAndMenu();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screen.width-this.getWidth())/2, (screen.height-this.getHeight())/2);
this.setVisible(true);
}
private void updataTitleAndMenu() {
String s;
if (file==null)
s = "无标题.txt";
else
s = file.getName();
if (needSave)
s = "*"+s;
save.setEnabled(needSave);
this.setTitle(s+" - 我的记事本");
}
private void newFile() {
editor.setText("");
file = null;
needSave = false;
this.updataTitleAndMenu();
}
private void openFile() {
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
file = fileChooser.getSelectedFile();
if (file == null || file.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
else {
int size = (int)file.length();
char[] in = new char;
int charsReaded = 0;
try {
input = new FileReader(file);
while (input.ready()) {
charsReaded += input.read(in, charsReaded, size-charsReaded);
}
input.close();
editor.setText(new String(in, 0, charsReaded));
needSave = false;
this.updataTitleAndMenu();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
private void saveFile() {
text = editor.getText();
if (file==null) {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
file = fileChooser.getSelectedFile();
}
if (file == null || file.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
else {
if (!file.getName().endsWith(".txt"))
file = new File(file.getPath()+".txt");
try {
output = new FileWriter(file);
for (int i=0; i<text.length(); i++) {
if (text.charAt(i)==(char)10)
if (i==0)
output.write(13);
else if (text.charAt(i-1)!=(char)13)
output.write(13);
output.write(text, i, 1);
}
output.close();
needSave = false;
this.updataTitleAndMenu();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "Error Saving File", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
private int isNeedSave() {
if (!needSave)
return JOptionPane.NO_OPTION;
return JOptionPane.showConfirmDialog(this, "文件内容已经更改\n想保存文件吗?", "文件已更改", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
TestEditor te = new TestEditor();
}
}
接下来是3个对话框类,简单封装了下,可以直接拿去用
//关于对话框 by Anson
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.*;
public class MyAboutDialog extends JDialog {
private JLabel icon, label1, label2, label3;
private JButton ok;
private JPanel info;
private Box center, button;
public MyAboutDialog(JFrame owner, String info1, String info2, String info3, String logo) {
super(owner, "关于", true);
icon = new JLabel();
label1 = new JLabel(info1);
label2 = new JLabel(info2);
label3 = new JLabel(info3);
ok = new JButton("确定");
if (logo!=null)
icon.setIcon(new ImageIcon(logo));
info = new JPanel();
info.setLayout(new BoxLayout(info, BoxLayout.Y_AXIS));
info.add(label1);
info.add(label2);
info.add(label3);
center = new Box(BoxLayout.X_AXIS);
center.add(Box.createHorizontalStrut(10));
center.add(icon);
center.add(Box.createHorizontalStrut(10));
center.add(info);
center.add(Box.createHorizontalStrut(10));
button = new Box(BoxLayout.X_AXIS);
button.add(Box.createHorizontalGlue());
button.add(ok);
this.setLayout(new BorderLayout());
this.add(center, BorderLayout.CENTER);
this.add(button, BorderLayout.SOUTH);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setVisible(false);
}
});
this.pack();
this.setResizable(false);
}
}
//颜色选择对话框 by Anson
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyColorChooser extends JDialog implements ActionListener {
private JColorChooser colorChooser;
private JPanel box;
private JButton ok, cancel, reset;
private Color color;
public MyColorChooser(JFrame owner, Color c) {
super(owner, "颜色选择", true);
color = c;
colorChooser = new JColorChooser(color);
box = new JPanel();
ok = new JButton("确定");
cancel = new JButton("取消");
reset = new JButton("重置");
ok.addActionListener(this);
cancel.addActionListener(this);
reset.addActionListener(this);
box.add(ok);
box.add(cancel);
box.add(reset);
this.setLayout(new BorderLayout());
this.add(colorChooser, BorderLayout.CENTER);
this.add(box, BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
}
public void setChoose(Color c) {
colorChooser.setColor(c);
}
public Color getChoose() {
return color;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == ok) {
color = colorChooser.getColor();
this.setVisible(false);
}else if (ae.getSource() == cancel) {
this.setVisible(false);
}else if (ae.getSource() == reset) {
color = Color.BLACK;
colorChooser.setColor(color);
}
}
}
//字体选择对话框 by Anson
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyFontChooser extends JDialog implements ActionListener, ListSelectionListener {
private JPanel top, center, bottom, top1, top2, top3;
private JLabel previewLabel;
private JTextField typeField, styleField, sizeField;
private JList typeList, styleList, sizeList;
private JScrollPane typeScroll, styleScroll, sizeScroll;
private JButton ok, cancel;
private String[] fontName, fontStyle, fontSize;
private Font font;
private String name;
private int style, size;
public MyFontChooser(JFrame owner, Font f) {
super(owner, "字体选择", true);
font = f;
name = f.getFontName();
style = f.getStyle();
size = f.getSize();
fontName = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontStyle = new String[] {"普通", "粗体", "斜体", "粗斜体"};
fontSize = new String[] { "8", "9", "10", "11", "12", "14", "16",
"18", "20", "22", "24", "26", "28", "36", "48", "72"};
typeField = new JTextField(10);
typeField.setEditable(false);
typeList = new JList(fontName);
typeList.setVisibleRowCount(6);
typeScroll = new JScrollPane(typeList);
styleField = new JTextField(10);
styleField.setEditable(false);
styleList = new JList(fontStyle);
styleList.setVisibleRowCount(6);
styleScroll = new JScrollPane(styleList);
sizeField = new JTextField(10);
sizeField.setEditable(false);
sizeList = new JList(fontSize);
sizeList.setVisibleRowCount(6);
sizeScroll = new JScrollPane(sizeList);
typeList.addListSelectionListener(this);
styleList.addListSelectionListener(this);
sizeList.addListSelectionListener(this);
top1 = new JPanel();
top2 = new JPanel();
top3 = new JPanel();
top1.setLayout(new BoxLayout(top1, BoxLayout.Y_AXIS));
top2.setLayout(new BoxLayout(top2, BoxLayout.Y_AXIS));
top3.setLayout(new BoxLayout(top3, BoxLayout.Y_AXIS));
top1.add(typeField);
top1.add(typeScroll);
top2.add(styleField);
top2.add(styleScroll);
top3.add(sizeField);
top3.add(sizeScroll);
top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
top.setBorder(new TitledBorder(new EtchedBorder(), "字体/样式/大小"));
top.add(top1);
top.add(top2);
top.add(top3);
previewLabel = new JLabel("TestText 测试文本");
previewLabel.setHorizontalAlignment(JLabel.CENTER);
previewLabel.setBackground(Color.WHITE);
previewLabel.setOpaque(true);
previewLabel.setBorder(new LineBorder(Color.black));
previewLabel.setPreferredSize(new Dimension(400, 80));
center = new JPanel();
center.setBorder(new TitledBorder(new EtchedBorder(), "预览"));
center.add(previewLabel);
ok = new JButton("确定");
cancel =new JButton("取消");
ok.addActionListener(this);
cancel.addActionListener(this);
bottom = new JPanel();
bottom.add(ok);
bottom.add(cancel);
this.setLayout(new BorderLayout());
this.add(top, BorderLayout.NORTH);
this.add(center, BorderLayout.CENTER);
this.add(bottom, BorderLayout.SOUTH);
this.setResizable(false);
this.pack();
}
public void setChoose(Font f) {
font = f;
name = f.getFontName();
style = f.getStyle();
size = f.getSize();
typeField.setText(name);
typeList.setSelectedValue(name, true);
styleField.setText(fontStyle);
styleList.setSelectedIndex(style);
sizeField.setText(String.valueOf(size));
sizeList.setSelectedValue(String.valueOf(size), true);
}
public Font getChoose() {
return font;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource()==ok) {
font = new Font(name, style, size);
this.setVisible(false);
} else if (ae.getSource()==cancel) {
this.setVisible(false);
}
}
public void valueChanged(ListSelectionEvent lse) {
if (lse.getSource()==typeList) {
name = (String)typeList.getSelectedValue();
typeField.setText((String)typeList.getSelectedValue());
} else if (lse.getSource()==styleList) {
style = styleList.getSelectedIndex();
styleField.setText(fontStyle);
} else if (lse.getSource()==sizeList) {
size = Integer.parseInt((String)sizeList.getSelectedValue());
sizeField.setText((String)sizeList.getSelectedValue());
}
previewLabel.setFont(new Font(name, style, size));
}
}
页:
[1]