如何在记事本中运行java代码?用记事本写完代码后运行方法如下:
1、用浏览器打开用记事本编写的代码
新建“文本文档”后,鼠标右键点击该文本文档,在菜单栏的“打开方式”选择“用记事本打开”,也可以设置默认打开方式为“记事本”;用记事本打开文本文档后,直接在该文档内根据自己的需要输入想要编辑的网页代码 。
2、记事本写java代码怎么运行
首先,需要安装jdk并配置环境变量 。然后,在命令行中,用javac命令编译用记事本编写的代码 。下一步 , 在命令行中 , 用java命令执行编译后的结果 。
代码是什么
代码是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系 。代码设计的原则包括唯一确定性、标准化和通用性、可扩充性与稳定性、便于识别与记忆、力求短小与格式统一以及容易修改等 。
计算机源代码最终目的是将人类可读文本翻译成为计算机可执行的二进制指令,这种过程叫编译 , 它由通过编译器完成 。源代码就是用汇编语言和高级语言写出来的地代码 。目标代码是指源代码经过编译程序产生的能被 cpu直接识别二进制代码 。
可执行代码就是将目标代码连接后形成的可执行文件,当然也是二进制的 。
java简单记事本代码以下代码是一个完整的实现,你只要复制过去就可以了~~
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
public class Start
{
public static void main(String args[])
{
new TextPad();
}
}
class TextPad extends JFrame implements ActionListener
{
JTextArea jta=new JTextArea("小廖记事本:)",18,52);
JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行",true);
String ss1=jta.getText();
UndoableEditListener ue=new UndoHander();
UndoManager undo = new UndoManager();
int StartFindPos=0,a=0,b=0;
GridBagConstraints gbc=new GridBagConstraints();
//Dimension dd=new Dimension();
// jta.getDocument().addUndoableEditListener(ue);
public TextPad()
{
//MyMenuListener ml=new MyMenuListener();
//JTextArea jta=new JTextArea("This is my textpad",18,52);
//System.out.println(dd.getHeight());
//System.out.println(dd.getWidth());
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
//System.out.println("OK");
this.setTitle("一个功能比较齐全的JAVA记事本");
this.setLocation(180,100);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JPanel jp=new JPanel();
JScrollPane jsp=new JScrollPane(jta);
jp.add(jsp);
//Rectangle rt=new Rectangle(0,0,this.getWidth(),this.getHeight());
//jsp.setBounds(rt);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
JMenu mf=new JMenu("文件(F)");
JMenuItem mtf1=new JMenuItem("新建");
mtf1.addActionListener(this);
JMenuItem mtf2=new JMenuItem("打开");
//mtf2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
mtf2.addActionListener(this);
JMenuItem mtf3=new JMenuItem("保存");
//mtf3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
mtf3.addActionListener(this);
JMenuItem mtf4=new JMenuItem("另存为");
mtf4.addActionListener(this);
JMenuItem mtf5=new JMenuItem("退出");
mtf5.addActionListener(this);
JMenu me=new JMenu("编辑(E)");
JMenuItem mte1=new JMenuItem("撤消");
mte1.addActionListener(this);
jta.getDocument().addUndoableEditListener(ue);
if(undo.canUndo())
{
mte1.setEnabled(false);
}
JMenuItem mte2=new JMenuItem("剪切");
mte2.addActionListener(this);
JMenuItem mte3=new JMenuItem("复制");
mte3.addActionListener(this);
JMenuItem mte4=new JMenuItem("粘贴");
mte4.addActionListener(this);
//JMenuItem mte5=new JMenuItem("删除");
//mte5.addActionListener(this);
JMenuItem mte6=new JMenuItem("查找");
mte6.addActionListener(this);
//JMenuItem mte7=new JMenuItem("查找下一个");
JMenuItem mte8=new JMenuItem("替换");
mte8.addActionListener(this);
//JMenuItem mte9=new JMenuItem("转到");
JMenuItem mte10=new JMenuItem("全选");
mte10.addActionListener(this);
JMenuItem mte11=new JMenuItem("日期/时间");
mte11.addActionListener(this);
JMenu mo=new JMenu("格式(O)");
//JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行(W)");
mto1.addActionListener(this);
JMenuItem mto2=new JMenuItem("字体");
mto2.addActionListener(this);
JMenu mv=new JMenu("查看(V)");
JMenuItem mtv1=new JMenuItem("状态栏");
mtv1.setEnabled(false);
JMenu mh=new JMenu("帮助(H)");
JMenuItem mth1=new JMenuItem("关于记事本");
mth1.addActionListener(this);
JMenuBar mb=new JMenuBar();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
mb.add(mf);
mb.add(me);
mb.add(mo);
mb.add(mv);
mb.add(mh);
this.setJMenuBar(mb);
mf.add(mtf1);
mf.add(mtf2);
mf.add(mtf3);
mf.add(mtf4);
mf.addSeparator();
mf.add(mtf5);
me.add(mte1);
me.addSeparator();
me.add(mte2);
me.add(mte3);
me.add(mte4);
//me.add(mte5);
me.addSeparator();
me.add(mte6);
//me.add(mte7);
me.add(mte8);
//me.add(mte9);
me.addSeparator();
me.add(mte10);
me.add(mte11);
mo.add(mto1);
mo.add(mto2);
mv.add(mtv1);
mh.add(mth1);
this.getContentPane().add(jsp);
this.setSize(600,400);
this.setResizable(true);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
try
{
Frame f=new Frame();
FileDialog fd=new FileDialog(f,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String fpath=fd.getDirectory();
String fname=fd.getFile();
BufferedReader br=new BufferedReader(new FileReader(fpath fname));
jta.setText("");
String s=br.readLine();
while(s!=null)
{
jta.append(s "\n");
s=br.readLine();
}
br.close();
}
catch(Exception ex)
{
}
}
if(e.getActionCommand().equals("保存"))
{
String fns=null;
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"保存文件",FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("新建"))
{
jta.setText("");
}
if(e.getActionCommand().equals("另存为"))
{
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"文件另存为",FileDialog.SAVE);
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("退出"))
{
String ss2=jta.getText();
if(!ss1.equals(ss2))
{
System.out.println("File is changed.");
}
System.exit(0);
}
if(e.getActionCommand().equals("撤消"))
{
try
{
undo.undo();
//System.out.println(undo.canUndo());
}
catch(Exception eundo)
{
}
}
if(e.getActionCommand().equals("剪切"))
{
jta.cut();
}
if(e.getActionCommand().equals("复制"))
{
jta.copy();
}
if(e.getActionCommand().equals("粘贴"))
{
jta.paste();
}
if(e.getActionCommand().equals("删除"))
{
}
if(e.getActionCommand().equals("全选"))
{
jta.selectAll();
}
if(e.getActionCommand().equals("查找"))
{
try
{
final JDialog jd=new JDialog(this,"查找",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridwidth=1;
gbc.gridheight=1;
jd.getContentPane().setLayout(gbl);
jd.setSize(380,100);
jd.setResizable(false);
//jd.setDefaultLookAndFeelDecorated(true);
final JTextField jtf=new JTextField(15);
JLabel jlFind=new JLabel("查找内容:");
jd.getContentPane().add(jlFind);
JButton jbFind=new JButton("查找");
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf.getText();
if(a=0)
{
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
}
}
}
);
JButton jbCancel=new JButton("取消");
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.getContentPane().add(jtf);
jd.getContentPane().add(jbFind);
jd.getContentPane().add(jbCancel);
//jd.setResizable(false);
jd.setLocation(240,200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
catch(Exception efind)
{
}
}
if(e.getActionCommand().equals("替换"))
{
final JDialog jd=new JDialog(this,"替换",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=1;
gbc.weighty=1;
gbc.gridwidth=1;
gbc.gridheight=1;
JLabel jlFind=new JLabel("查找:");
JLabel jp=new JLabel("替换内容:");
final JTextField jtf=new JTextField(15);
final JTextField jtf1=new JTextField(15);
jd.getContentPane().setLayout(gbl);
jd.setSize(330,150);
jd.setResizable(false);
final JButton jbReplace=new JButton("替换");
final JButton jbReplaceAll=new JButton("替换所有");
final JButton jbCancel=new JButton("取消");
final JButton jbFind=new JButton("查找");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jlFind,gbc);
gbc.gridx=1;
gbc.gridy=0;
jd.getContentPane().add(jtf1,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jbFind,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jp,gbc);
gbc.gridx=1;
gbc.gridy=1;
jd.getContentPane().add(jtf,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jbReplace,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jbReplaceAll,gbc);
gbc.gridx=2;
gbc.gridy=3;
jd.getContentPane().add(jbCancel,gbc);
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf1.getText();
if(a=0)
{
a=strA.indexOf(strB,StartFindPos);
//System.out.println(a b);
b=strB.length();
StartFindPos=a b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
}
}
}
);
jbReplace.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("替换"))
{
String strRepleace=jtf.getText();
jta.replaceSelection(strRepleace);
}
}
}
);
jbReplaceAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
while(a-1)
{
String strA=jta.getText();
String strB=jtf1.getText();
a=strA.indexOf(strB,StartFindPos);
if(a==-1)
{
break;
}
//System.out.println(a b);
b=strB.length();
StartFindPos=a b;
//System.out.println(StartFindPos);
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
String strRepleaceAll=jtf.getText();
jta.replaceSelection(strRepleaceAll);
StartFindPos=a b;
}
JOptionPane.showMessageDialog(null, "全部替换完毕", "替换内容",1);
a=0;
StartFindPos=0;
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setLocation(240,200);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("日期/时间"))
{
final JDialog jd=new JDialog(this,"插入日期");
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField jtf=new JTextField(10);
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
//Calendar cl=Calendar.getInstance();
//DateFormat df=DateFormat.getInstance();
//String sdate1=df.format(cl.getTime());
//jtf.setText(sdate1);
jp1.add(jtf);
jp1.add(jbOK);
jp1.add(jbCancel);
jd.getContentPane().add(jp1,"North");
JPanel jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
final JCheckBox jcb1=new JCheckBox("格式一");
final JCheckBox jcb2=new JCheckBox("格式二");
final JCheckBox jcb3=new JCheckBox("格式三");
jp2.add(jcb1);
jp2.add(jcb2);
jp2.add(jcb3);
jd.getContentPane().add(jp2,"Center");
jd.setSize(220,120);
jd.setResizable(false);
jd.setLocation(240,200);
final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cl=Calendar.getInstance();
DateFormat df=DateFormat.getInstance();
final String sdate=df.format(cl.getTime());
jcb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式一"))
{
if(jcb1.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy年MM月dd日");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb2.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle1)
{
estyle1.printStackTrace();
}
}
else
{
jcb2.setEnabled(true);
jcb3.setEnabled(true);
}
try
{
System.out.println(jta.getLineStartOffset(3));
//System.out.println(jta.getLineOfOffset(346));
}
catch(Exception eee)
{
}
}
}
}
);
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式二"))
{
if(jcb2.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy/MM/dd");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb1.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle2)
{
estyle2.printStackTrace();
}
}
else
{
jcb1.setEnabled(true);
jcb3.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式三"))
{
if(jcb3.isSelected())
{
jtf.setText(sdate);
jcb1.setEnabled(false);
jcb2.setEnabled(false);
}
else
{
jcb1.setEnabled(true);
jcb2.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("确定"))
{
int pos=jta.getCaretPosition();
jta.insert(jtf.getText(),pos);
}
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("自动换行"))
{
if(mto1.getState())
{
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
}
else
{
jta.setLineWrap(false);
jta.setWrapStyleWord(false);
}
//System.out.println("OK");
}
if(e.getActionCommand().equals("字体"))
{
final JDialog jd=new JDialog(this,"字体设置");
jd.setLocation(240,200);
//final JFrame jfontview=new JFrame();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
JTextField jtf1=new JTextField(6);
final JTextArea jtaview=new JTextArea(4,8);
//jfontview.getContentPane().add(jtaview);
final JTextField jtf2=new JTextField(6);
final JTextField jtf3=new JTextField(3);
JComboBox jcb1=new JComboBox();
final JComboBox jcb2=new JComboBox();
jcb2.addItem("BOLD");
jcb2.addItem("ITALIC");
jcb2.addItem("PLAIN");
//jcb2.addItem("BOLDITALIC");
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf2.setText((String)jcb2.getSelectedItem());
}
}
);
final JComboBox jcb3=new JComboBox();
jcb3.addItem("14");
jcb3.addItem("18");
jcb3.addItem("22");
jcb3.addItem("26");
jcb3.addItem("30");
jcb3.addItem("34");
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf3.setText((String)jcb3.getSelectedItem());
//jtaview.setText("SS");
// int fontsizeview=Integer.parseInt((String)jcb3.getSelectedItem());
//Font fontview=new Font("字体预览",Font.BOLD,fontsizeview);
//jtaview.setFont(fontview);
//jtaview.setEditable(false);
}
}
);
gbc.gridheight=1;
gbc.gridwidth=1;
//gbc.gridx=1;
//gbc.gridy=0;
gbc.weightx=0.5;
gbc.weighty=0.5;
jd.getContentPane().setLayout(gbl);
//JLabel jl1=new JLabel("预览:");
JLabel jl2=new JLabel("字型:");
JLabel jl3=new JLabel("大小:");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jl2,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jl3,gbc);
//gbc.gridx=4;
//gbc.gridy=0;
//jd.getContentPane().add(jl3,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jtf2,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jtf3,gbc);
//gbc.gridx=4;
// gbc.gridy=1;
//jd.getContentPane().add(jtf3,gbc);
gbc.gridx=0;
gbc.gridy=2;
jd.getContentPane().add(jcb2,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jcb3,gbc);
//gbc.gridx=4;
//gbc.gridy=2;
//jd.getContentPane().add(jcb3,gbc);
gbc.gridx=4;
gbc.gridy=1;
jd.getContentPane().add(jbOK,gbc);
gbc.gridx=4;
gbc.gridy=2;
jd.getContentPane().add(jbCancel,gbc);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtf2.getText().equals("PLAIN"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=0;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
// System.out.println(f.getFont("楷体_GB2312").getFontName());
}
if(jtf2.getText().equals("BOLD"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=1;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
if(jtf2.getText().equals("ITALIC"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=2;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
jd.dispose();
//System.out.println(fontstyle);
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jd.dispose();
}
}
);
jd.setSize(200,120);
jd.setResizable(false);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("关于记事本"))
{
JOptionPane jop=new JOptionPane(null,JOptionPane.INFORMATION_MESSAGE);
jop.showMessageDialog(null,"小廖记事本","关于记事本",JOptionPane.OK_OPTION);
//System.out.println("OK");
}
}
class UndoHander implements UndoableEditListener
{
public void undoableEditHappened(UndoableEditEvent eundo)
{
undo.addEdit(eundo.getEdit());
}
}
}
Java记事本源代码import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;
public class Demo extends JFrame {
private static final long serialVersionUID = 1L; //Eclipse自动生成序列号
String name = "无标题";
JPanel menuPanel = new JPanel();
JTextArea text = new TextAreaMenu();//文本编辑区
JScrollPane jsp = new JScrollPane(text);//可滚动编辑区
JMenuBar mnbMain = new JMenuBar();
JMenu mnServer = new JMenu("文件(F)");
JMenu mnEdit= new JMenu("编辑(E)");
JMenuItem[] mniServers = new JMenuItem[]{
new JMenuItem("新建(N)"),
new JMenuItem("保存(S)"),
new JMenuItem("打开(O)"),
new JMenuItem("退出(X)"),
};
{
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
mnbMain.add(mnServer);
menuPanel.add(mnbMain);
mnbMain.setBounds(5, 0, 50, 30);
for (int i = 0; imniServers.length; i) {
mnServer.add(mniServers[i]);
}
mniServers[0].addActionListener(new ActionListener() { //定义"新建"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
new Demo(getLocation().x 15,getLocation().y 5);
}
});
mniServers[1].addActionListener(new ActionListener() { //定义"保存"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
chooseToSave();
}
});
mniServers[2].addActionListener(new ActionListener() { //定义"打开"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
chooseToOpen();
}
});
mniServers[3].addActionListener(new ActionListener() { //定义"退出"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
text.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
}
});
}
public Demo(int x,int y) {
this.setTitle( name" - 记事本");
this.setBounds(x, y, 600, 400);
this.setLayout(new BorderLayout());
this.add(menuPanel, BorderLayout.NORTH);
this.add(jsp);
jsp.setBounds(5, 30, getWidth()-10, getHeight()-50);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public Demo() {
this(200,200);
}
protected void chooseToSave() {
File file = chooseFile();
if(null==file)return;
if(file.exists()){
int cho = JOptionPane.showConfirmDialog(this, "文件已存在,是否覆盖?");
System.out.println(cho);
if(cho==JOptionPane.OK_OPTION)save(file);
else return;
}
else save(file);
}
private void save(File file) {
name = file.getName();
write(text.getText(),file.getPath());
this.setTitle( name" - 记事本");
}
protected void chooseToOpen() {
File file = chooseFile();
if(null==file||!file.exists())return;
name = file.getName();
Demo.this.setTitle( name" - 记事本");
read(text,file);
}
/*********************************************MAIN**************************************************/
public static void main(String[] args) {
new Demo();
}
private File chooseFile(){
JFileChooser chooser = new JFileChooser(); //构建文件选择器
chooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "文本文件";
}
@Override
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return f.isDirectory() || name.endsWith(".txt")
||name.endsWith(".c") || name.endsWith(".java")
||name.endsWith(".cpp");//可识别文件
}
});
int result = chooser.showDialog(null, "确定");
if (result==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
} else {
System.out.println("未选择文件");
}
return chooser.getSelectedFile();
}
public static void read(JTextArea text,File file){//定义读取文件操作
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String string = null;
while((string = br.readLine()) != null){
text.append(string "\n");
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(String txt,String fileName){
FileWriter fw;
try {
fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(txt);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class TextAreaMenu extends JTextArea implements MouseListener {
private static final long serialVersionUID = -2308615404205560110L;
private JPopupMenu pop = null; // 弹出菜单
private JMenuItem selectAll = null,copy = null, paste = null, cut = null, cancel=null; // 功能菜单
public TextAreaMenu() {
super();
init();
}
private void init() {
this.addMouseListener(this);
this.setSelectedTextColor(Color.red);
pop = new JPopupMenu();
pop.add(selectAll = new JMenuItem("全选"));
pop.add(copy = new JMenuItem("复制"));
pop.add(paste = new JMenuItem("粘贴"));
pop.add(cut = new JMenuItem("剪切"));
pop.add(cancel = new JMenuItem("撤销"));
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', InputEvent.CTRL_MASK));
copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
cancel.setAccelerator(KeyStroke.getKeyStroke('Z', InputEvent.CTRL_MASK));
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
this.add(pop);
}
/**
* 菜单动作
* @param e
*/
public void action(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals(selectAll.getText())) { // 全选
this.selectAll();
}
else if (str.equals(copy.getText())) { // 复制
this.copy();
} else if (str.equals(paste.getText())) { // 粘贴
this.paste();
} else if (str.equals(cut.getText())) { // 剪切
this.cut();
}
else if (str.equals(cancel.getText())) { //撤销
this.cut();
}
}
public JPopupMenu getPop() {
return pop;
}
public void setPop(JPopupMenu pop) {
this.pop = pop;
}
/**
* 剪切板中是否有文本数据可供粘贴
*
* @return true为有文本数据
*/
public boolean isClipboardString() {
boolean b = false;
Clipboard clipboard = this.getToolkit().getSystemClipboard();
Transferable content = clipboard.getContents(this);
try {
if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
b = true;
}
} catch (Exception e) {
}
return b;
}
/**
* 文本组件中是否具备复制的条件
*
* @return true为具备
*/
public boolean isCanCopy() {
boolean b = false;
int start = this.getSelectionStart();
int end = this.getSelectionEnd();
if (start != end)
b = true;
return b;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
copy.setEnabled(isCanCopy());
paste.setEnabled(isClipboardString());
cut.setEnabled(isCanCopy());
pop.show(this, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
}
}
记事本java代码//继承java记事本实现代码的类换一下java记事本实现代码 , 应该用swing包里面
class Jsben extends Frame{-- class Jsben extends JFrame{
framejava记事本实现代码的方法setMenuBar(bar);和setJMenuBar(bar);这个不太一样java记事本实现代码,功能类似
如何用记事本进行java编程 ?步骤如下:1、新建一个“文本文档”然后打开 。2、写好代码保存,然后把后缀名由txt改为java 。3、按Windows图标键 R打开系统的“运行”,输入“cmd”,打开系统“命令提示符” 。4、使用
cd
命令定位到之前写好的代码的文件路径 。5、使用
“javac
文件名.java”命令编译 。6、如果没有错误 , 使用“java
文件名”命令运行即可 。
【java记事本实现代码 java记事本代码实现】关于java记事本实现代码和java记事本代码实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 微信广告如何推广赚钱,微信广告怎么推广的
- 机战网络游戏,机战端游
- 国外免费虚拟电脑主机,海外虚拟主机租用
- c语言进入函数前有值 c语言进入函数前有值怎么办
- 南京短视频如何营销产品,南京短视频策划团队
- 数组索引为变量js,数组的索引必须为数字吗
- gt函数python gt函数在哪个库
- 如何重复营销模式,重复营销的概念
- 飞行游戏起飞教程视频,真实飞行模拟器怎么起飞教程