现有java编辑记事本代码里边有错误,请各位大神修改
发布网友
发布时间:2022-05-26 21:29
我来回答
共2个回答
热心网友
时间:2023-11-03 19:19
提问者,你好:
我试了一下,你这个项目中新建文件功能没实现,其他的都OK了!原因:缺少括号!
请叫我雷锋!
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;
public class ff extends Frame implements ActionListener{
MenuBar mainmenubar=new MenuBar();
Menu file;
MenuItem nw;
MenuItem op;
MenuItem cl;
MenuItem sf;
MenuItem ex;
TextArea tx;
public ff(String title){
super(title);
CloseHandler handler=new CloseHandler();
this.addWindowListener(handler);
setSize(400,400);
setLocationRelativeTo(null);
menuinit();
tx=new TextArea();
this.add(tx);
setVisible(true);
}
void menuinit(){
mainmenubar=new MenuBar();
file= new Menu(" 文件");
nw=new MenuItem("新建文件");
op=new MenuItem("打开文件");
cl=new MenuItem("关闭文件");
sf=new MenuItem("保存文件");
ex=new MenuItem("退 出");
file.add(nw);
file.add(op);
file.add(cl);
file.add(sf);
file.add(ex);
mainmenubar.add(file);
setMenuBar(mainmenubar);
nw.addActionListener(this);
op.addActionListener(this);
cl.addActionListener(this);
sf.addActionListener(this);
ex.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
System.out.println();
Object ob=e.getSource();
javax.swing.JFileChooser f=new JFileChooser();
if((ob==nw)||(ob==cl)){
tx.setText("");
}else if(ob==op){
f.showOpenDialog(this);
try{
StringBuffer s=new StringBuffer();
FileReader in=new FileReader(f.getSelectedFile());
while(true){
int b=in.read();
if(b==-1){ //此处加括号
break;
}
s.append((char)b);
}
tx.setText(s.toString());
in.close();
}catch(Exception ee){
}//此处你少括号了!
}else if(ob==sf){
f.showSaveDialog(this);
try{
FileWriter out=new FileWriter(f.getSelectedFile());
out.write(tx.getText());
out.close();
}catch(Exception ee){
}
}else if(ob==ex);
System.exit(0);
}
//程序的入口
public static void main(String[] args){
new ff("简易文本编辑器");
}
}
class CloseHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
热心网友
时间:2023-11-03 19:19
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;
public class ff extends Frame implements ActionListener {
MenuBar mainmenubar = new MenuBar();
Menu file;
MenuItem nw;
MenuItem op;
MenuItem cl;
MenuItem sf;
MenuItem ex;
TextArea tx;
public ff(String title) {
super(title);
CloseHandler handler = new CloseHandler();
this.addWindowListener(handler);
setSize(400, 400);
setLocationRelativeTo(null);
menuinit();
tx = new TextArea();
this.add(tx);
setVisible(true);
}
void menuinit() {
mainmenubar = new MenuBar();
file = new Menu(" 文件");
nw = new MenuItem("新建文件");
op = new MenuItem("打开文件");
cl = new MenuItem("关闭文件");
sf = new MenuItem("保存文件");
ex = new MenuItem("退 出");
file.add(nw);
file.add(op);
file.add(cl);
file.add(sf);
file.add(ex);
mainmenubar.add(file);
setMenuBar(mainmenubar);
nw.addActionListener(this);
op.addActionListener(this);
cl.addActionListener(this);
sf.addActionListener(this);
ex.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object ob = e.getSource();
javax.swing.JFileChooser f = new JFileChooser();
if ((ob == nw) || (ob == cl)) {
tx.setText("");
} else if (ob == op) {
f.showOpenDialog(this);
try {
StringBuffer s = new StringBuffer();
FileReader in = new FileReader(f.getSelectedFile());
while (true) {
int b = in.read();
if (b == -1) break;
s.append((char) b);
}
tx.setText(s.toString());
in.close();
} catch (Exception ee) {
}
} else if (ob == sf) {
f.showSaveDialog(this);
try {
FileWriter out = new FileWriter(f.getSelectedFile());
out.write(tx.getText());
out.close();
} catch (Exception ee) {
}
} else if (ob == ex)
;
System.exit(0);
}
public static void main(String[] args) {
new ff("简易文本编辑器");
}
}
class CloseHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}