java 高手请进

需要一个记事本代码 另请附上解释
2024-12-04 09:00:57
推荐回答(4个)
回答(1):

创建个NotePad的类,填写如下内容:
package com.notePad;

import javax.swing.*;

public class NotePad {
JFrame f = new JFrame("记事本");

JScrollPane sp = new JScrollPane();

static JTextArea ta = new JTextArea();

JMenuBar mb = new JMenuBar();

JMenu m1 = new JMenu("文件");

JMenu m2 = new JMenu("编辑");

JMenu m3 = new JMenu("格式");

JMenu m4 = new JMenu("查看");

JMenu m5 = new JMenu("帮助");

JMenuItem mi1 = new JMenuItem("新建");

JMenuItem mi2 = new JMenuItem("打开");

JMenuItem mi3 = new JMenuItem("保存");

JMenuItem mi4 = new JMenuItem("另存为");

JMenuItem mi5 = new JMenuItem("剪切");

JMenuItem mi6 = new JMenuItem("复制");

JMenuItem mi7 = new JMenuItem("粘贴");

JMenuItem mi8 = new JMenuItem("删除");

JMenuItem mi9 = new JMenuItem("自动换行");

JMenuItem mi10 = new JMenuItem("字体");

JMenuItem mi11 = new JMenuItem("状态栏");

JMenuItem mi12 = new JMenuItem("帮助主题");

JMenuItem mi13 = new JMenuItem("关于记事本");
//以上为新建多种菜单

public void menu() {

mi2.addActionListener(new ActionOpen());
mi3.addActionListener(new ActionSave());
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
m2.add(mi7);
m2.add(mi8);
m3.add(mi9);
m3.add(mi10);
m4.add(mi11);
m5.add(mi12);
m5.add(mi13);
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
mb.add(m5);

}
//以上把菜单填入合适的菜单栏中
public void frame() {

sp.setViewportView(ta);
f.setJMenuBar(mb);
f.add(sp);
f.setSize(800, 600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//以上设置窗口
public static void main(String[] args) {

NotePad jsb = new NotePad();
jsb.menu();
jsb.frame();

}
//主函数中调用方法
}

在新建个ActionOpen类,填入一下代码.

package com.notePad;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class ActionOpen implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

ActionOpen ao = new ActionOpen();
ao.show(ao.shiXian(ao.open()));

}

public String open() {

JFrame f = new JFrame();
FileDialog fd = new FileDialog(f, "打开");
fd.setVisible(true);
String str = fd.getDirectory() + fd.getFile();

return str;

}

public Vector shiXian(String str1) {
Vector v = new Vector();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(str1)));
int i = 0;
String a = br.readLine();
while (a != null) {
v.add(a);
a = br.readLine();
i++;

}
br.close();

} catch (Exception e) {
e.printStackTrace();
}
return v;
}

public void show(Vector r) {
for (int i = 0; i < r.size(); i++) {

NotePad.ta.append(r.get(i) + "\n");

}
}
}
//以上为实现打开功能.

新建个ActionSave类,填写如何代码:

package com.notePad;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import javax.swing.JFrame;

public class ActionSave implements ActionListener {

public void actionPerformed(ActionEvent arg0) {
System.out.println("Save");

}

public String save() {

JFrame f = new JFrame();
FileDialog fd = new FileDialog(f, "另存为", 1);

fd.setVisible(true);
String str = fd.getDirectory() + fd.getFile();

System.out.println(str);
return str;

}

public String[] shiXian(String path) {
String[] str = new String[100];
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path)));
bw.wait();
} catch (Exception e) {

e.printStackTrace();
}

return str;
}

public static void main(String[] args) {

ActionSave as = new ActionSave();
as.save();

}

}

//为实现保存功能.

功能实现的比较少,不过还是基本可以的.实现所用功能的我没做过,不好意思.

回答(2):

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*; //Date needed
import java.io.PrintWriter;
public class NotePad extends JFrame
{
JTextArea jta;
class newl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.setText("");
}
}

class openl implements ActionListener
{ public void actionPerformed(ActionEvent e)
{
JFileChooser jf=new JFileChooser();
jf.showOpenDialog(NotePad.this);

}

}
//保存文件的监听
class savel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jf = new JFileChooser();
jf.showSaveDialog(NotePad.this);

}
}
//打印的监听 ?
class printl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// PrintWriter p = new PrintWriter(NotePad.this);
}
}
//退出记事本的监听
class exitl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);//退出
}
}

//拷贝的监听
class copyl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
}
}

//粘贴的监听
class pastel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
}
//剪切的监听
class cutl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
}
}
//查找的监听

//添加日期的监听
class datel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Date d=new Date();
jta.append(d.toString());
}
}

//构造函数
public NotePad()
{
jta=new JTextArea("",24,40);
JScrollPane jsp=new JScrollPane(jta);
JMenuBar jmb=new JMenuBar();
JMenu mFile=new JMenu("File");
JMenu mEdit=new JMenu("Edit");

JMenuItem mNew=new JMenuItem("New",KeyEvent.VK_N);
mNew.addActionListener(new newl());
mFile.add(mNew);

JMenuItem mOpen=new JMenuItem("Open",KeyEvent.VK_O);
mOpen.addActionListener(new openl());
mFile.add(mOpen);

JMenuItem mSave=new JMenuItem("Save");
mSave.addActionListener(new savel());
mFile.add(mSave);

mFile.addSeparator(); //添加分割线

JMenuItem mPrint = new JMenuItem("Print");
mPrint.addActionListener(new printl());
mFile.add(mPrint);

mFile.addSeparator(); //添加分割线

JMenuItem mExit=new JMenuItem("Exit");
mExit.addActionListener(new exitl());
mFile.add(mExit);
mFile.setMnemonic(KeyEvent.VK_F);

//编辑菜单的子菜单的处理
JMenuItem jmi;
jmi=new JMenuItem("Copy");
jmi.addActionListener(new copyl());
mEdit.add(jmi);

jmi=new JMenuItem("Cut");
jmi.addActionListener(new cutl());
mEdit.add(jmi);

jmi=new JMenuItem("Paste");
jmi.addActionListener(new pastel());
mEdit.add(jmi);

mEdit.addSeparator(); //添加分割线

jmi=new JMenuItem("Find");

mEdit.add(jmi);

jmi=new JMenuItem("FindNext");
mEdit.add(jmi);
mEdit.addSeparator();
jmi=new JMenuItem("Select All");
mEdit.add(jmi);
jmi=new JMenuItem("Date/Time");
jmi.addActionListener(new datel());
mEdit.add(jmi);

jmb.add(mFile);
jmb.add(mEdit);

this.setJMenuBar(jmb);

this.getContentPane().add(jsp);
this.setSize(200,200);
this.setVisible(true);
}
//主函数,程序入口点
public static void main(String s[])
{
new NotePad();
}

}

回答(3):

道道道

回答(4):

貌似很多人要这个
难道是作业~?