| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1300 人关注过本帖
标题:[求助]大家来看看我的记事本代码(大家帮忙,问题在后面的帖子中)
只看楼主 加入收藏
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
 问题点数:0 回复次数:16 
[求助]大家来看看我的记事本代码(大家帮忙,问题在后面的帖子中)

剪切,复制和粘帖的代码没弄上来....... 运行出现异常不知道该怎么解决..........
错误嘛说也说不清楚,还请哪位好心人运行下看看帮帮我.....谢了!!!!!!

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编辑过]

搜索更多相关主题的帖子: import 记事本 java awt 代码 
2006-10-30 23:40
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 

我自己做了个记事本的程序功能不全 你看看吧!对你也许有帮助!

bjwQxIHs.zip (47.8 KB)

[此贴子已经被作者于2006-10-31 0:13:31编辑过]


2006-10-31 00:13
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 

......楼上的代码还有JSP什么的.........东西太复杂了........暂时还是不要去研究了....
请哪位好心的朋友帮我分析一下我的代码就好了.....................我真不知道这个异常该怎么办...........不会改啊


开开心心的过&玩每一天!!!!
2006-10-31 10:19
yklsos
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-11-8
收藏
得分:0 
我正在研究,不要着急
2006-11-12 21:04
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
加我QQ32645957 哪部分不清楚问我!

2006-11-12 22:08
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
楼主你的程序我怎么不能编译通过的啊?
theArea.setFont(new Font(null, Font.BOLD,
Integer.valueOf(size)));
有错误!!

2006-11-12 22:23
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 

非常感谢各位的帮忙 由于学校宿舍调整搞得我这一段时间都没能上网....郁闷啊..............以后又能常来了 问题我也解决了 呵呵


开开心心的过&玩每一天!!!!
2006-11-12 23:29
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
以下是引用lgdcky在2006-11-12 22:23:29的发言:
楼主你的程序我怎么不能编译通过的啊?
theArea.setFont(new Font(null, Font.BOLD,
Integer.valueOf(size)));
有错误!!

Font的构造函数是什么,你知道么?
Font(String name, int style, int size)
你最后那个是Integer,所以编译通不过了


可惜不是你,陪我到最后
2006-11-12 23:47
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 
不会啊 我不是能编过嘛 还有用呢
不这版主说的也对呵 确实这个返回的是一个Integer 那为什么我的还有用呢?

[此贴子已经被作者于2006-11-13 0:04:56编辑过]


开开心心的过&玩每一天!!!!
2006-11-12 23:57
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 

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 openfile = null, savefile = 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); //都是菜单

openfile = new FileDialog(this, "打开对话框", FileDialog.LOAD);
openfile.setVisible(false);

savefile = new FileDialog(this,"保存对话框",FileDialog.SAVE);
savefile.setVisible(false);

}

public JMenu buildFileMenu() {
themenu = new JMenu("文件");

JMenuItem newf = new JMenuItem("新建");
newf.addActionListener(this);

JMenuItem open = new JMenuItem("打开");
open.addActionListener(this);

JMenuItem save = new JMenuItem("保存");
save.addActionListener(this);

JMenuItem close = new JMenuItem("关闭");
close.addActionListener(this);

JMenuItem quit = new JMenuItem("退出");
quit.addActionListener(this);

themenu.add(newf);
themenu.add(open);
themenu.add(save);
themenu.add(close);
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没用

//新建事件
str = e.getActionCommand();
if(str == "新建") {
if( !theArea.getText().equals("") ) {
int result = JOptionPane.showConfirmDialog(new JFrame(), "Text" +
"的文字已改变,要保存吗?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
// 显示一个对话框,包括确定,否和取消三个按钮,result记录你点的是那个键,根据你的选择判断下一步
if( result == JOptionPane.YES_OPTION ) //{
saveMethod(); //调用保存事件方法
theArea.setText("");
/*}

else if( result == JOptionPane.NO_OPTION )
theArea.setText(""); //消空笔记本*/
}
}

//打开事件
else if(str == "打开") {
openfile.setVisible(true);
try {
File readfile = new File(openfile.getDirectory(), openfile.getFile());
setTitle(readfile.getName() + "----记事本");
bf = new BufferedReader(new FileReader(readfile));
while(bf.readLine() != null) {

theArea.append(bf.readLine() + "\n");
}
bf.close();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}

else if(str == "保存")
saveMethod(); //调用保存事件方法
else if(str == "关闭" ||str == "退出")
System.exit(0);
}

//保存事件方法
public void saveMethod() {
savefile.setVisible(true);
try {
File writefile = new File(savefile.getDirectory(), savefile.getFile());

bw = new BufferedWriter( new FileWriter(writefile));
bw.write(theArea.getText(), 0, (theArea.getText().length()));

bw.flush();
bw.close();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}

/**
* @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) {
if (!theArea.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(new JFrame(),
"Text" + "的文字已改变,要保存吗?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION); //一个选择对话框,和前面的一样
if( result == JOptionPane.YES_OPTION )
new TextBook().saveMethod(); //调用保存事件方法
}
System.exit(0);
}
});
jf.setVisible(true);

}

}

请那位来帮我改下保存和打开事件的方法...
问题:
1: 打开方法时 总有的地方读不出来 不明白是什么回事.
2: 保存方法不能自动保存为Txt类型,不知道怎么搞..........

[此贴子已经被作者于2006-11-13 0:16:06编辑过]


开开心心的过&玩每一天!!!!
2006-11-13 00:12
快速回复:[求助]大家来看看我的记事本代码(大家帮忙,问题在后面的帖子中)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020091 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved