谁能给我一个JAVA编写记事本的源代码?
答案:3 悬赏:40 手机版
解决时间 2021-07-29 12:09
- 提问者网友:欺烟
- 2021-07-28 16:27
谁能给我一个JAVA编写记事本的源代码?
最佳答案
- 五星知识达人网友:孤老序
- 2021-07-28 17:00
给你 你也看不懂,还是从基础学起好。
全部回答
- 1楼网友:枭雄戏美人
- 2021-07-28 19:00
你个鸡鸡,连这个都不会做啊,活着吧!!!
- 2楼网友:玩家
- 2021-07-28 18:29
看一下吧,功能比较简单
//MyTextPad.java
//javac -d ../classes MyTextPad.java
//java -classpath ../classes MyTextPad
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
//import MyWindowListener;
public class MyTextPad extends JFrame
implements ActionListener
{
private Container c;
JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu;
private JTextArea ta=new JTextArea();
private JMenuItem newMenu,openMenu,saveMenu,saveasMenu,exitMenu;
private static int opencount=0;
JFileChooser fc =new JFileChooser(".");
public MyTextPad()
{
super("MyTextPad");
c=getContentPane();
setJMenuBar(menuBar);
fileMenu=new JMenu("文件");
newMenu=new JMenuItem("新建");
openMenu=new JMenuItem("打开");
saveMenu=new JMenuItem("保存");
saveasMenu=new JMenuItem("另存为");
exitMenu=new JMenuItem("退出");
c.add(ta,BorderLayout.NORTH);
c.add(new JScrollPane(ta));
menuBar.add(fileMenu);
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(saveasMenu);
fileMenu.add(exitMenu);
newMenu.addActionListener(this);
openMenu.addActionListener(this);
saveMenu.addActionListener(this);
saveasMenu.addActionListener(this);
exitMenu.addActionListener(this);
opencount++;
setSize(480,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==newMenu)
{
MyTextPad mtpnew = new MyTextPad();
mtpnew.addWindowListener(new MyWindowListener() );
}
else if(e.getSource()==openMenu)
{
int returnVal = fc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try{
BufferedReader in=new BufferedReader(new FileReader(fc.getSelectedFile()));
String s=in.readLine();
while(s!=null){
ta.append(s);
s=in.readLine();
ta.append("\n");
}
in.close();
}catch(IOException ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage(),"对话框",JOptionPane.PLAIN_MESSAGE);
}
}
}
else if(e.getSource()==saveMenu)
{
int returnval=fc.showSaveDialog(this);
if(returnval==JFileChooser.APPROVE_OPTION)
try{
BufferedWriter out=new BufferedWriter(new FileWriter(fc.getSelectedFile()));
out.write(ta.getText());
out.close();
}catch(IOException ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage(),"对话框",JOptionPane.PLAIN_MESSAGE);
}
}
else if(e.getSource()==saveasMenu)
{
//String title;
//title =mtp.gettitle()+".txt";
int returnval=fc.showSaveDialog(this);
if(returnval==JFileChooser.APPROVE_OPTION)
try{
BufferedWriter out=new BufferedWriter(new FileWriter(fc.getSelectedFile()));
out.write(ta.getText());
out.close();
}catch(IOException ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage(),"对话框",JOptionPane.PLAIN_MESSAGE);
}
}
else if(e.getSource()==exitMenu)
{
if(opencount>0)
{
JOptionPane.showMessageDialog(null,"您将退出整个程序","提示",1);
System.exit(1);
}
else
System.exit(1);
}
}
public static void main(String[] args)
{
MyTextPad mtp = new MyTextPad();
mtp.addWindowListener(new MyWindowListener() );
}
}
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯