请Java高手帮助一下,完成如图所示的程序代码。在此先谢谢了哦!

2025-01-04 12:36:22
推荐回答(1个)
回答1:

你直接建一个NoteFrame的类,然后把下面的代码考进去就可以了,一定记得,不要留原来新建时带的代码。别忘了给个满意哦,嘿嘿
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class NoteFrame extends JFrame{
/**
* copyright by 笑面弥勒007
*/
private static final long serialVersionUID = -2418566522146844302L;
//文本框
JTextArea jta=new JTextArea();
//滚动条
JScrollPane jsp=new JScrollPane(jta);
//菜单
JMenuBar jmb=new JMenuBar();
//文件菜单
JMenu fileMenu = new JMenu("文件");
JMenuItem fileMenuItemOpen = new JMenuItem("读取文件");
JMenuItem fileMenuItemSave = new JMenuItem("写入文件");
public NoteFrame() {
//窗体初始化
setWindow();
//组件绑定时间监听器
this.fileMenuItemOpen.addActionListener(new OpenfileListener());
this.fileMenuItemSave.addActionListener(new SavefileListener());
}
//初始化窗体设置
public void setWindow(){
//窗口常规设置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 480, 320);
this.setTitle("文件存取");
//滚动条设置
this.jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.add(this.jsp);
//文件菜单设置
this.fileMenu.add(this.fileMenuItemOpen);
this.fileMenu.add(this.fileMenuItemSave);
this.jmb.add(this.fileMenu);
//
this.setJMenuBar(this.jmb);
}
public static void main(String[] args) {
new NoteFrame().setVisible(true);
}
//打开
class OpenfileListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
jta.setText(null);
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(NoteFrame.this);
String s = null;
if (result==JFileChooser.APPROVE_OPTION) {
s=fc.getSelectedFile().getPath();
setTitle(s);
FileReader fr=null;
BufferedReader br = null;
try {
fr = new FileReader(s);
br = new BufferedReader(fr);
String buffer;
while((buffer=br.readLine())!=null){
jta.setText(jta.getText()+buffer+"\b\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fr != null){
try {
fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
//保存
class SavefileListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (getTitle()=="文件存取") {
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(NoteFrame.this);
String s = null;
if (result==JFileChooser.APPROVE_OPTION) {
s=fc.getSelectedFile().getPath();
setTitle(s);
FileWriter fw=null;
try {
fw=new FileWriter(s);
String buffer;
String string;
buffer=jta.getText();
string=buffer.replaceAll("\n","\r\n");
fw.write(string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}else {
setTitle(getTitle());
FileWriter fw=null;
try {
fw=new FileWriter(getTitle());
String buffer;
String string;
buffer=jta.getText();
string=buffer.replaceAll("\n","\r\n");
fw.write(string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}