剪切,复制和粘帖的代码没弄上来....... 运行出现异常不知道该怎么解决..........
错误嘛说也说不清楚,还请哪位好心人运行下看看帮帮我.....谢了!!!!!!
package io;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextBook extends JFrame implements ActionListener {
static JTextArea theArea = null; //文本输入的地方
JMenu themenu = null; //菜单
boolean colorchanged = true; // 控制自动换行的,因为不会像windows的那样打个勾
BufferedReader bf = null; //读
BufferedWriter bw = null; //写
String str = null; //好像没什么用
static FileDialog open = null, save = null;
public TextBook() {
super("记事本----しΟν∈→鱈制作");
theArea = new JTextArea();
theArea.setEditable(true);
Container cp = getContentPane();
cp.add(new JScrollPane(theArea));
// cp.setLayout(new BorderLayout());
JMenuBar MBar = new JMenuBar();
MBar.setOpaque(true);
JMenu mfile = buildFileMenu();
// mfile.setOpaque(true);
MBar.add(mfile);
JMenu medite = buildEditeMenu();
// medite.setOpaque(true);
MBar.add(medite);
JMenu mformat = buildFormatMenu();
// mformat.setOpaque(true);
MBar.add(mformat);
setJMenuBar(MBar);
}
public JMenu buildFileMenu() {
themenu = new JMenu("文件");
JMenuItem newf = new JMenuItem("新建");
newf.addActionListener(this);
open = new FileDialog(this, "打开对话框", FileDialog.LOAD);
open.setVisible(false);
save = new FileDialog(this,"保存对话框",FileDialog.SAVE);
save.setVisible(false);
JMenuItem close = new JMenuItem("关闭");
close.addActionListener(this);
JMenuItem quit = new JMenuItem("退出");
quit.addActionListener(this);
themenu.add(newf);
themenu.add(open);
themenu.add(close);
themenu.add(save);
themenu.addSeparator();
themenu.add(quit);
return themenu;
}
public JMenu buildEditeMenu() {
themenu = new JMenu("编辑");
JMenuItem copy = new JMenuItem("复制");
JMenuItem cut = new JMenuItem("剪切");
JMenuItem paste = new JMenuItem("粘帖");
themenu.add(copy);
themenu.add(cut);
themenu.add(paste);
return themenu;
}
public JMenu buildFormatMenu() {
themenu = new JMenu("格式");
JMenuItem format = new JMenuItem("自动换行");
format.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// theArea.setLineWrap(true);
// theArea.setWrapStyleWord(true);
// boolean colorchanged = true;
if (colorchanged) {
((JMenuItem) e.getSource()).setForeground(Color.red);
theArea.setLineWrap(true);
theArea.setWrapStyleWord(true);
} else {
((JMenuItem) e.getSource()).setForeground(Color.black);
theArea.setLineWrap(false);
theArea.setWrapStyleWord(false);
}
colorchanged = !colorchanged;
}
});
JMenuItem fontSize = new JMenuItem("字体");
fontSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String size = JOptionPane.showInputDialog(new JFrame(),
"输入你要的字体大小", "字体选择", JOptionPane.YES_NO_OPTION);
theArea.setFont(new Font(null, Font.BOLD,
Integer.valueOf(size)));
}
});
themenu.add(format);
themenu.add(fontSize);
return themenu;
}
public void actionPerformed(ActionEvent e) {
//if(e.getSource.equals("new"))
str = e.getActionCommand(); //所以说str没用
if(str == "新建") {
if(!theArea.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(new JFrame(), "Text" +
"的文字已改变,要保存吗?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
// 显示一个对话框,包括确定,否和取消三个按钮,result记录你点的是那个键
if( result == JOptionPane.YES_OPTION )
open.setVisible(true); //这边可能有问题,但是我运行不了..所以不能调试......
else if( result == JOptionPane.NO_OPTION )
theArea.setText("");
}//根据你的选择判断下一步
}
else if(str == "打开") {
open.setVisible(true);
try {
File readfile = new File(open.getDirectory(), open.getFile());
bf = new BufferedReader(new FileReader(readfile));
while(bf.readLine() != null) {
theArea.append(bf.readLine() + "\n");
}
bf.close();
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
// open.addActionListener(this);
}
else if(str == "保存") {
save.setVisible(true);
try {
//File readf = new File(save.getDirectory(),save.getFile());
//bf = new BufferedReader(new FileReader(readf));
File writefile = new File(save.getDirectory(), save.getFile());
bw = new BufferedWriter( new FileWriter(writefile));
bw.write(theArea.getText(), 0, (theArea.getText().length()));
bw.flush();
bw.close();
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
else if(str == "关闭" ||str == "退出") {
System.exit(0);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
JFrame jf = new TextBook();
jf.setSize(new Dimension(800, 600));
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// System.exit(0);
if (!theArea.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(new JFrame(),
"Text" + "的文字已改变,要保存吗?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
if( result == JOptionPane.YES_OPTION )
open.setVisible(true); //这边可能有问题,但是我运行不了..所以不能调试......
System.exit(0);
}
}
});
jf.setVisible(true);
}
}
[此贴子已经被作者于2006-11-13 18:06:40编辑过]