发布网友
共5个回答
热心网友
这个是最标准的记事本源代码!希望能帮到您!
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NoteBook
{
private JFrame jf;
private JMenuBar jmb;
private JMenu jm1,jm2,jm3,jm4;
private JMenuItem jmt1,jmt2,jmt3,jmt4,jmt5,jmt6,jmt7,jmt8,jmt9;
private JCheckBoxMenuItem jcb1,jcb2;
private JRadioButton jcb3,jcb4,jcb5;
private JLabel jl1;
private JScrollBar jsb;
private ButtonGroup bg;
private JToolBar jtb;
private Icon i1,i2,i3,i4,i5;
private JButton jb1,jb2,jb3,jb4,jb5;
private JTextArea jta;
private static int size=20;
private static int mode=0;
String title="error"; //声明字符串title
String fileName="NOname.txt"; //声明filename
int type=JOptionPane.ERROR_MESSAGE;
public NoteBook()
{
jmb=new JMenuBar();
jm1=new JMenu("文件");
jm2=new JMenu("编辑");
jm3=new JMenu("格式");
jm4=new JMenu("关于");
jmt1=new JMenuItem("新建");jmt1.addActionListener(new JMenuItemHander());
jmt2=new JMenuItem("打开");jmt2.addActionListener(new JMenuItemHander());
jmt3=new JMenuItem("保存");jmt3.addActionListener(new JMenuItemHander());
jmt4=new JMenuItem("退出");jmt4.addActionListener(new JMenuItemHander());
jmt5=new JMenuItem("全选");jmt5.addActionListener(new JMenuItemHander());
jmt6=new JMenuItem("复制");jmt6.addActionListener(new JMenuItemHander());
jmt7=new JMenuItem("粘贴");jmt7.addActionListener(new JMenuItemHander());
jmt8=new JMenuItem("剪切");jmt8.addActionListener(new JMenuItemHander());
jmt9=new JMenuItem("帮助");jmt9.addActionListener(new JMenuItemHander());
jcb1=new JCheckBoxMenuItem("粗体");jcb1.addActionListener(new JCheckBoxMenuItemHander());
jcb2=new JCheckBoxMenuItem("斜体");jcb2.addActionListener(new JCheckBoxMenuItemHander());
bg=new ButtonGroup();
jcb3=new JRadioButton("蓝色");jcb3.addActionListener(new JCheckRadioButtonHander());bg.add(jcb3);
jcb4=new JRadioButton("红色");jcb4.addActionListener(new JCheckRadioButtonHander());bg.add(jcb4);
jcb5=new JRadioButton("绿色");jcb5.addActionListener(new JCheckRadioButtonHander());bg.add(jcb5);
jl1=new JLabel("文字大小");
jsb=new JScrollBar(JScrollBar.VERTICAL,20,20,20,100);jsb.addAdjustmentListener(new AdjustHander());
// jcb3=new JRadioButton("12");jcb3.addActionListener(new JCheckBoxMenuItemHander());bg.add(jcb1);
// jcb4=new JRadioButton("16");jcb4.addActionListener(new JCheckBoxMenuItemHander());bg.add(jcb2);
// jcb5=new JRadioButton("20");jcb5.addActionListener(new JCheckBoxMenuItemHander());bg.add(jcb3);
i1=new ImageIcon("2.jpg");
i2=new ImageIcon("3.jpg");
i3=new ImageIcon("4.jpg");
i4=new ImageIcon("5.jpg");
i5=new ImageIcon("6.jpg");
jtb=new JToolBar("工具栏");
jb1=new JButton(i1);jb1.setToolTipText("全选");jb1.addActionListener(new JButtonHander());
jb2=new JButton(i2);jb2.setToolTipText("复制");jb2.addActionListener(new JButtonHander());
jb3=new JButton(i3);jb3.setToolTipText("粘贴");jb3.addActionListener(new JButtonHander());
jb4=new JButton(i4);jb4.setToolTipText("剪切");jb4.addActionListener(new JButtonHander());
jb5=new JButton(i5);jb5.setToolTipText("提示");jb5.addActionListener(new JButtonHander());
// jb6=new JButton("查找");jb1.addActionListener(new JButtonHander());
// jb7=new JButton("后退");jb1.addActionListener(new JButtonHander());
jta=new JTextArea();
jta.setFont(new Font("宋体",0,20));
jta.setLineWrap(true);
}
public void method()
{
jf=new JFrame("记事本");
Container c=jf.getContentPane();
c.add(new JScrollPane(jta));
jm1.add(jmt1);
jm1.addSeparator();
jm1.add(jmt2);
jm1.add(jmt3);
jm1.addSeparator();
jm1.add(jmt4);
jm2.add(jmt5);
jm2.addSeparator();
jm2.add(jmt6);
jm2.add(jmt7);
jm2.add(jmt8);
jm3.add(jcb1);
jm3.add(jcb2);
jm3.add(jcb3);
jm3.add(jcb4);
jm3.add(jcb5);
jm4.add(jmt9);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmb.add(jm4);
jf.setJMenuBar(jmb);
jtb.add(jb1);
jtb.add(jb2);
jtb.add(jb3);
jtb.add(jb4);
jtb.addSeparator();
jtb.add(jb5);
jtb.add(jl1);
jtb.add(jsb);
jtb.setOrientation(JScrollBar.VERTICAL);
c.add(BorderLayout.WEST,jtb);
jf.setSize(500,500);
jf.setLocation(300,100);
jf.setVisible(true);
}
public static void main(String args[])
{
NoteBook n=new NoteBook();
n.method();
}
public void readfile()//读文件
{
JFileChooser openfile=new JFileChooser();//声明初始化对象
openfile.setDialogTitle("打开文件");
openfile.setApproveButtonText("打开");
openfile.showOpenDialog(jf);
File file=openfile.getSelectedFile();
if(file!=null)
{
FileInputStream inputfile=null;
String message="文件不能找到";
try
{
inputfile=new FileInputStream(file);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(jf,message,title,type);
}
int readbytes;
String message1="读文件发生错误";
try
{
while((readbytes=inputfile.read())!=-1)
{
jta.append(String.valueOf((char)readbytes));
}
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(jf,message1,title,type);
}
String closemessage="关闭文件发生错误";
try
{
inputfile.close();
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(jf,closemessage,title,type);
}
}
}
public void writefile(String filename)//保存文件
{
File filesa;
String messagef="文件未找到";
FileOutputStream outputfile=null;
if(fileName==null)
{
JFileChooser savefile=new JFileChooser();
savefile.setApproveButtonText("保存");
savefile.setDialogTitle("保存文件");
savefile.showSaveDialog(jf);
filesa=savefile.getSelectedFile();
}else{
filesa=new File(fileName);
}
try
{
outputfile=new FileOutputStream(filesa);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(jf,messagef,title,type);
}
String filecontent=jta.getText();
String wrmessage="下文件错误";
try
{
outputfile.write(filecontent.getBytes());
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(jf,wrmessage,title,type);
}
String cmessage="关闭流发生错误";
try
{
outputfile.close();
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(jf,cmessage,title,type);
}
}
class JMenuItemHander implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jmt1)
{
jta.setText("");
}
if(ae.getSource()==jmt2)
{
readfile();
}
if(ae.getSource()==jmt3)
{
fileName=null;
writefile(fileName);
}
if(ae.getSource()==jmt4)
{
System.exit(0);
}
if(ae.getSource()==jmt5)
{
jta.selectAll();
}
if(ae.getSource()==jmt6)
{
jta.copy();
}
if(ae.getSource()==jmt7)
{
jta.paste();
}
if(ae.getSource()==jmt8)
{
jta.cut();
}
if(ae.getSource()==jmt9)
{
JFrame jf=new JFrame("记事本_帮助文档");
Container c=jf.getContentPane();
JLabel jl=new JLabel("记事本使用说明,详见http://www.baidu.com");
c.add(jl);
jf.setSize(260,100);
jf.setResizable(false);
jf.setLocation(400,200);
jf.setVisible(true);
}
}
}
class JCheckBoxMenuItemHander implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int mode=0;
if(jcb1.isSelected())
{
mode=mode+Font.BOLD;
}
else
{
mode=mode+Font.PLAIN;
}
if(jcb2.isSelected())
{
mode=mode+Font.ITALIC;
}
else
{
mode=mode+Font.PLAIN;
}
getmode(mode);
jta.setFont(new Font("宋体",mode,size));
}
}
class JCheckRadioButtonHander implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(jcb3.isSelected())
{
jta.setForeground(Color.blue);
}
if(jcb4.isSelected())
{
jta.setForeground(Color.red);
}
if(jcb5.isSelected())
{
jta.setForeground(Color.green);
}
}
}
class JButtonHander implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jta.selectAll();
}
if(ae.getSource()==jb2)
{
jta.copy();
}
if(ae.getSource()==jb3)
{
jta.paste();
}
if(ae.getSource()==jb4)
{
jta.cut();
}
if(ae.getSource()==jb5)
{
JFrame jf=new JFrame("记事本_提示说明");
Container c=jf.getContentPane();
JLabel jl=new JLabel("记事本提示说明,详见http://www.baidu.com");
c.add(jl);
jf.setSize(260,100);
jf.setResizable(false);
jf.setLocation(400,200);
jf.setVisible(true);
}
}
}
class AdjustHander implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
int size=jsb.getValue();
getsize(size);
jta.setFont(new Font("宋体",mode,size));
}
}
public static int getsize(int size1)
{
size=size1;
return size;
}
public static int getmode(int mode1)
{
mode=mode1;
return mode;
}
}
热心网友
命令行操作
javac HelloWorld.java(要有jdk编译成类文件)
java HelloWorld(JVM运行)
默认要在同一目录下,如:d:/menu/HelloWorld.java
你需要cd(命令行)到d:/menu,再分别输入那两个语句
问题是没那个类文件
你可以下载个IDE工具用用,要是初学者,那记事本也不是件坏事!
热心网友
在你的jdk1.60文件夹里的demo\jfc\Notepad文件夹有标准的源码
热心网友
JDK中有例子,你可以看一下,在jdk1.xxx\demo\jfc\Notepad 下。
热心网友
Jedit,自己搜一下。