//JNotepadTest.java
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class JNotepadTest extends JPanel
{
//变量定义
JTextArea jta = new JTextArea("", 24, 40);
JScrollPane jsp = new JScrollPane(jta);
//菜单条
JMenuBar jmb = new JMenuBar();
JMenu file = new JMenu("文件");
JMenu edit = new JMenu("编辑");
JMenu format = new JMenu("格式");
JMenu help = new JMenu("帮助");
//工具条
JToolBar toolBar = new JToolBar();
//菜单内容
JMenuItem jmi;
//实例化剪切板
Clipboard clipbd = getToolkit().getSystemClipboard();
//打印功能
PrinterJob prtMe = PrinterJob.getPrinterJob();
String text = "";
//构造函数
public JNotepadTest()
{
class newL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.setDocument(new PlainDocument());
}
}
//打开功能
class openL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(JNotepadTest.this, "打开");
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String file = fc.getSelectedFile().getPath();
if(file == null)
{
return;
}
//读取文件
try
{
Reader in = new FileReader(file);
char[] buff = new char[4096];
int nch;
while((nch = in.read(buff, 0, buff.length)) != -1)
{
jta.setDocument(new PlainDocument());
jta.append(new String(buff, 0, nch));
}
}
catch (IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
}
else
{
return;
}
}
}
//保存文件
class saveL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(JNotepadTest.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String savefile = fc.getSelectedFile().getPath();
if(savefile == null)
{
return;
}
else
{
String docToSave = jta.getText();
if(docToSave != null)
{
FileOutputStream fstrm = null;
BufferedOutputStream ostrm = null;
try
{
fstrm = new FileOutputStream(savefile);
ostrm = new BufferedOutputStream(fstrm);
byte[] bytes = null;
try
{
bytes = docToSave.getBytes();
}
catch(Exception e1)
{
e1.printStackTrace();
}
ostrm.write(bytes);
}
catch(IOException io)
{
System.err.println("IOException: " +
io.getMessage());
}
finally
{
try
{
ostrm.flush();
fstrm.close();
ostrm.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " +
ioe.getMessage());
}
}
}
}
}
else
{
return;
}
}
}
//页面设置
class pageSetupL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
prtMe.printDialog();
}
}
//打印
class printL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
prtMe.print();
}
catch(Exception ew)
{
}
}
}
//退出
class exitL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
//复制
class copyL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = jta.getSelectedText();
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
}
}
//剪切
class cutL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = jta.getSelectedText();
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
jta.replaceRange("", jta.getSelectionStart(),
jta.getSelectionEnd());
}
}
//粘贴
class pasteL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable clipData = clipbd.getContents(JNotepadTest.this);
try
{
String clipString =
(String)clipData.getTransferData(
DataFlavor.stringFlavor);
jta.replaceRange(clipString,
jta.getSelectionStart(), jta.getSelectionEnd());
}
catch(Exception ex)
{
}
}
}
//删除
class deleteL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = jta.getSelectedText();
jta.replaceRange("", jta.getSelectionStart(),
jta.getSelectionEnd());
}
}
//全部选择
class selectAllL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
}
}
//字体设置
class formatL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
/*String find = "";
find = JOptionPane.showInputDialog(
"查找的内容:");*/
}
}
//帮助
/*class helpL extends Dialog
{
} */
//事件监听
class jtaL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//快捷键设置
file.add(jmi = new JMenuItem("新建", KeyEvent.VK_N));
jmi.addActionListener(new newL());
file.add(jmi = new JMenuItem("打开", KeyEvent.VK_O));
jmi.addActionListener(new openL());
file.add(jmi = new JMenuItem("保存", KeyEvent.VK_S));
jmi.addActionListener(new saveL());
file.addSeparator();
file.add(jmi = new JMenuItem("页面设置", KeyEvent.VK_G));
jmi.addActionListener(new pageSetupL());
file.add(jmi = new JMenuItem("打印", KeyEvent.VK_P));
jmi.addActionListener(new printL());
file.addSeparator();
file.add(jmi = new JMenuItem("退出", KeyEvent.VK_X));
jmi.addActionListener(new exitL());
edit.add(jmi = new JMenuItem("复制", KeyEvent.VK_C));
jmi.addActionListener(new copyL());
edit.add(jmi = new JMenuItem("剪切", KeyEvent.VK_T));
jmi.addActionListener(new cutL());
edit.add(jmi = new JMenuItem("粘帖", KeyEvent.VK_P));
jmi.addActionListener(new pasteL());
edit.add(jmi = new JMenuItem("删除", KeyEvent.VK_D));
jmi.addActionListener(new deleteL());
edit.addSeparator();
edit.add(jmi = new JMenuItem("全选", KeyEvent.VK_A));
jmi.addActionListener(new selectAllL());
format.add(jmi = new JMenuItem("字体设置", KeyEvent.VK_F));
jmi.addActionListener(new formatL());
//设置页面布局风格
setLayout(new BorderLayout());
file.setMnemonic(KeyEvent.VK_F);
jmb.add(file);
edit.setMnemonic(KeyEvent.VK_E);
jmb.add(edit);
format.setMnemonic(KeyEvent.VK_S);
jmb.add(format);
jmb.add(Box.createHorizontalGlue());
jmb.add(help);
toolBar.setFloatable(true);
addButtons(toolBar);
add(jmb, BorderLayout.NORTH);
add(toolBar, BorderLayout.CENTER);
add(jsp, BorderLayout.SOUTH);
jta.getCaret().setVisible(true);
jta.setCaretPosition(0);
}
//添加按钮
protected void addButtons(JToolBar toolBar)
{
JButton button = new JButton(new ImageIcon("images/new.gif"));
button.setToolTipText("新建一个文档");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setDocument(new PlainDocument());
}
});
toolBar.add(button);
//打开按钮
JButton button1 = new JButton(new ImageIcon("images/open.gif"));
button1.setToolTipText("打开一个文档");
//添加按钮的事件监听
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(JNotepadTest.this, "打开文件");
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String file = fc.getSelectedFile().getPath();
if(file == null)
{
return;
}
try
{
Reader in = new FileReader(file);
char[] buff = new char[4096];
int nch;
while((nch = in.read(buff, 0, buff.length)) != -1)
{
jta.setDocument(new PlainDocument());
jta.append(new String(buff, 0, nch));
}
}
catch (IOException io)
没有合适的资源?快使用搜索试试~ 我知道了~
Java做的记事本带标准工具栏
共30个文件
class:21个
gif:6个
java:1个
3星 · 超过75%的资源 需积分: 31 103 下载量 110 浏览量
2008-01-05
18:20:19
上传
评论 2
收藏 97KB RAR 举报
温馨提示
带文档,看了就知道,但是有个别的功能未完善,请谅解.仅供参考啊<br>有价值,所以要一分!
资源推荐
资源详情
资源评论
收起资源包目录
Java_200420050231.rar (30个子文件)
Java_姚勇_200420050231
code
IMAGES
PASTE.GIF 1KB
SAVE.GIF 1KB
NEW.GIF 1KB
COPY.GIF 1KB
_desktop.ini 10B
OPEN.GIF 1KB
CUT.GIF 1KB
JNotepadTest$5.class 1KB
JNotepadTest$2.class 2KB
JNotepadTest$1jtaL.class 485B
JNotepadTest$1saveL.class 2KB
JNotepadTest$1pageSetupL.class 621B
JNotepadTest$appCloseL.class 424B
JNotepadTest$1openL.class 2KB
JNotepadTest$1newL.class 673B
JNotepadTest$6.class 1KB
JNotepadTest$1copyL.class 924B
JNotepadTest.java 11KB
JNotepadTest$1deleteL.class 783B
JNotepadTest$3.class 2KB
JNotepadTest$1formatL.class 491B
JNotepadTest$1exitL.class 541B
JNotepadTest.class 5KB
JNotepadTest$1printL.class 645B
JNotepadTest$1.class 711B
JNotepadTest$1pasteL.class 1KB
JNotepadTest$1selectAllL.class 602B
JNotepadTest$4.class 960B
JNotepadTest$1cutL.class 1KB
Java设计_ 姚勇.doc 172KB
共 30 条
- 1
资源评论
- myron_02013-06-07不靠谱 不过可以借鉴
- yyy聪头2014-01-04觉得还行,值得参考
- coolsweet19782014-03-06质量一般,不值1分
yykings
- 粉丝: 34
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功