「java简易记事本」java简易记事本流程

博主:adminadmin 2022-11-26 04:55:06 80

本篇文章给大家谈谈java简易记事本,以及java简易记事本流程对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用java的GUI编写一个记事本

很久之前写过的,凑合着看吧,

package 个人练习;

import java.awt.FileDialog;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.TextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.Writer;

import java.sql.Savepoint;

import javax.swing.JFrame;

/**

 * 创建一个类似记事本的程序

 * 该程序拥有自己的书写窗口

 * 可以自定义保存地址

 * @author Administrator

 *

 */

/*

 * 创建记事本类

 * 实现ActionListener接口

 * 重写它的actionPerformed方法

 * 

 */

public class JiShiBen implements ActionListener {

        //拥有自己的窗口

JFrame frame=new JFrame("记事本程序");

//菜单栏

MenuBar menubar=new MenuBar();

//创建下拉菜单

Menu wenjian=new Menu("文件");

//创建保存按钮

MenuItem dakai=new MenuItem("打开文件");

MenuItem baochun=new MenuItem("保存");

MenuItem xinjian=new MenuItem("新建");

//创建可以显示文本的多行区域

TextArea wenben=new TextArea();

//显示一个对话窗口,用户选择一个文件,如果模式是SAVE,则寻找一个地方去写入一个文件

FileDialog fd;

File file=null;

/*

 * 创建jishiben的构造函数

 */

public  JiShiBen(){

frame.setMenuBar(menubar);//将菜单栏关联到窗口

menubar.add(wenjian);//将下拉菜单添加到菜单栏

wenjian.add(xinjian);

wenjian.add(dakai);//将保存按钮添加到下拉菜单

wenjian.add(baochun);

dakai.addActionListener(this);//由baochuan按钮接受处理操作事件

baochun.addActionListener(this);

xinjian.addActionListener(this);

frame.add(wenben);//将文本区域添加到窗口

//设置窗口的属性

frame.setSize(600,480);

frame.setLocation(200, 200);

frame.setVisible(true);

/*

 * 

 * 用于接收窗口事件的侦听器接口。

 * 旨在处理窗口事件的类要么实现此接口(及其包含的所有方法),

 * 要么扩展抽象类 WindowAdapter(仅重写所需的方法)。

 * 然后使用窗口的 addWindowListener 方法将从该类所创建

 * 的侦听器对象向该 Window 注册。

 * 当通过打开、关闭、激活或停用、

 * 图标化或取消图标化而改变了窗口状态时,

 * 将调用该侦听器对象中的相关方法,

 * 并将 WindowEvent 传递给该方法。 

 * windowClosing(WindowEvent e) 

        用户试图从窗口的系统菜单中关闭窗口时调用。

 *  

 */

frame.addWindowListener(new WindowAdapter() {

 public void windowClosing(WindowEvent e) {

 System.exit(0);//终止当前运行的java虚拟机

 }

});

}

/*

 * 实现ActionListener接口中的方法

 * (non-Javadoc)

 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)

 */

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==baochun){

//public Object getSource()最初发生 Event 的对象。 返回,最初发生 Event 的对象。

fd=new FileDialog(frame,"保存文本文件",FileDialog.SAVE);

/*

 * FileDialog(Dialog parent, String title, int mode) 

          创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。

          

    static int LOAD 

          此常量值指示文件对话框窗口的作用是查找要读取的文件。 

    static int SAVE 

          此常量值指示文件对话框窗口的作用是查找要写入的文件。 

 */

fd.setVisible(true);

//文件的存放位置是通过fd获取的位置,文件名是通过fd获取的文件名

file=new File(fd.getDirectory(), fd.getFile()+".txt");

save(file);

}

if(e.getSource()==dakai){

fd=new FileDialog(frame,"打开文件",FileDialog.LOAD);

fd.setVisible(true);

/*

 * String getFile() 

          获取此文件对话框的选定文件。 

   String getDirectory() 

          获取此文件对话框的目录。        

 */

file=new File(fd.getDirectory()+fd.getFile());

System.out.println(fd.getFile());

System.out.println(fd.getDirectory());

open(file);

}

if(e.getSource()==xinjian){

wenben.setText("");

}

}

/*

 * 创建save方法

 */

public void save(File file){

try{

PrintWriter pw=new PrintWriter(file);

pw.write(wenben.getText());//wenben.getText()返回文本区域的文本

pw.close();

}catch(Exception e){

}

}

//创建open方法

public void open(File file){

try{

FileInputStream fis=new FileInputStream(file);

InputStreamReader isr=new InputStreamReader(fis);

BufferedReader br=new BufferedReader(isr);

    String line=null;

while((line=br.readLine())!=null){

wenben.append(line+"\n");

}

br.close();

}catch(Exception e){

}

}

public static void main(String[] args) {

new JiShiBen();

}

}

用JAVA编个简单的记事本程序

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.util.Calendar;

import java.util.Date;

import javax.swing.*;

class Editor extends Frame implements ActionListener,ItemListener

{

// Date date=new Date();

int n1;String str="";int k=0;String st1,st2;

JTextArea ta=new JTextArea(50,50);

Dialog dialog,dialog1;

Choice ce1,ce2,ce3;

Button btn1,btn2,btn3;Panel p1=new Panel();

Panel p2=new Panel();Panel p3=new Panel();Panel p4=new Panel();

public Editor (String s)

{

super ("记事本编辑板");

ImageIcon YouImg = new ImageIcon("iconimage.jpg");

setIconImage(YouImg.getImage());

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

dispose();

System.exit(0);

}

});

setBounds(50,35,700,500);

MenuBar mb=new MenuBar();

setMenuBar(mb);

Menu file=new Menu("文件(F)");Menu edit=new Menu("编辑(E)");Menu help=new Menu("帮助(H)");

Menu pattern=new Menu("格式(P)");Menu time=new Menu("时间(T)");

MenuItem exit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_Q));

MenuItem tdate=new MenuItem("日期/时间",new MenuShortcut(KeyEvent.VK_D));

MenuItem renew=new MenuItem("新建", new MenuShortcut(KeyEvent.VK_N));

MenuItem save=new MenuItem("保存", new MenuShortcut(KeyEvent.VK_S));

MenuItem open=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_O));

MenuItem copy=new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C));

MenuItem paste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V));

MenuItem cut=new MenuItem("剪切",new MenuShortcut(KeyEvent.VK_X));

MenuItem helptopic=new MenuItem("主题",new MenuShortcut(KeyEvent.VK_T));

helptopic.addActionListener(this);

dialog=new Dialog(this, "字体设置");dialog.setSize(300,300);

dialog1=new Dialog(this, "帮助主题");dialog1.setBounds(250,100,300,400);

dialog.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

dialog.setVisible(false);}

});

dialog1.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

dialog1.setVisible(false);}

});

//dialog.setBackground(Color.WHITE);

dialog.setLayout(null);dialog.setBounds(170,150,450,300);

Label L1=new Label("字体(F)");Label L2=new Label("字形(Y)");

Label L3=new Label("大小(S)");TextArea ta1=new TextArea(5,5);

ta1=new TextArea("至于记事本的主题嘛,因为本人的水平有限, 所有不能为大家作过多的指导,望大家见谅!"); //在这里我想加一个文本框用来放帮助信息。

ce1=new Choice();ce2=new Choice();

ce3=new Choice();

btn1=new Button("确定");btn2=new Button("取消");btn1.addActionListener(this);btn2.addActionListener(this);

btn3=new Button("颜色");

btn3.addActionListener(this);

ce1.add("华文行楷");ce1.add("华文中宋");ce1.add("华文新魏");ce1.add("华文细黑");ce1.add("宋体");ce1.add("方正姚体");

ce1.add("幼圆");ce1.add("隶书");ce1.add("楷体-GB2312");ce1.add("华文行楷");ce1.add("华文彩云");ce1.add("仿宋-GB2312");

ce2.add("粗体");ce2.add("斜体");ce2.add("常规");

for(int n1=0;n1=100;n1++)

{ce3.add(""+n1);}

dialog.add(p1);p1.setBounds(5,30,440,35);p1.setLayout( null);//p1.setBackground(Color.red);

dialog.add(p2);p2.setBounds(5,65,440,27);dialog.add(p3);p3.setBounds(5,90,440,40);//p4.setBounds(5,120,440,138);

p1.setLayout( null);

p1.add(L1);L1.setBounds(5,15,50,25);p1.add(L2);L2.setBounds(150,15,50,25);

p1.add(L3);L3.setBounds(250,15,50,25);p2.setLayout( null);//p2.setBackground(Color.yellow);

p2.add(ce1);ce1.setBounds(5,0,130,25);p2.add(ce2);ce2.setBounds(160,0,90,25);p2.add(ce3);ce3.setBounds(260,0,75,25);

p2.add(btn1);btn1.setBounds(360,0,75,25);

p3.setLayout( null);p3.add(btn2);btn2.setBounds(360,10,75,25);p3.add(btn3);btn3.setBounds(160,10,75,25);

dialog.add(p4);p4.setBackground(Color.yellow);p4.setBounds(5,120,440,138);

dialog1.add(ta1);

//设置字体的下拉情况

MenuItem Font=new MenuItem("字体");//给字体加监听器

Font.addActionListener(this);

MenuItem Replace=new MenuItem("替换"); MenuItem Seek=new MenuItem("查找");

//首要添加组件的情况

mb.add(file);mb.add(edit);mb.add(pattern);mb.add(time);mb.add(help);

file.add(renew);file.addSeparator();file.add(open);time.add(tdate);

file.addSeparator(); file.add(exit);file.addSeparator();file.add(save);

edit.add(copy); edit.addSeparator();edit.add(paste);edit.addSeparator();edit.add(cut);

pattern.add(Font); pattern.add(Replace);

pattern.add(Seek);help.add(helptopic);

//给必要的组件加上监听器

renew.addActionListener( this);tdate.addActionListener(this);

open.addActionListener(this); save.addActionListener(this);exit.addActionListener(this);

cut.addActionListener(this);copy.addActionListener(this);paste.addActionListener(this);

ce1.addItemListener(this);ce2.addItemListener(this);ce3.addItemListener(this);

//添加必要的组件

add(ta,"Center");ta.setBackground(new Color(249,255,234));

setSize(700,500);

setVisible(true);

this.validate() ;

}

public void itemStateChanged(ItemEvent e) {

if (ce2.getSelectedItem().equals("粗体"))

{ k=1;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}

if (ce2.getSelectedItem().equals("斜体"))

{ k=2;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}

if (ce2.getSelectedItem().equals("常规"))

{ k=3;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}

}

public void actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("新建"))

{System.out.println("new ");

ta.setText("");}

if(e.getActionCommand().equals("退出"))

{ System.exit(0);}

try

{

if(e.getActionCommand().equals("打开"))

openText();

if(e.getActionCommand().equals("保存"))

saveText();

}catch(IOException e1){}

if(e.getActionCommand().equals("复制"))

{ str=ta.getSelectedText();}

if(e.getActionCommand().equals("粘贴"))

{ ta.insert(str,ta.getCaretPosition());}

if(e.getActionCommand().equals("剪切"))

{str=ta.getSelectedText();ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());

}

if(e.getActionCommand().equals("字体"))

{dialog.setVisible(true);}

if(e.getActionCommand().equals("主题"))

{dialog1.setVisible(true);}

if(e.getActionCommand().equals("颜色"))

{

Color clr=JColorChooser.showDialog(this,"颜色对话框",null);

ta.setForeground(clr);

}

if(e.getSource()==btn2)

dialog.setVisible(false);

if(e.getSource()==btn1)

{

if(k==1)

{ ta.setFont(new Font(st1,Font.BOLD,Integer.parseInt(st2)));}

if(k==2)

{ ta.setFont(new Font(st1,Font.ITALIC,Integer.parseInt(st2)));}

if(k==3)

{ ta.setFont(new Font(st1,Font.PLAIN,Integer.parseInt(st2)));}

dialog.setVisible(false);

}

//if (e.getActionCommand().equals("日期/时间"))

//ta.setText(ta.getText()+""+date);

if(e.getActionCommand().equals("日期/时间"))

{

Calendar c1 =Calendar.getInstance();

int y = c1.get(Calendar.YEAR);

int m = c1.get(Calendar.MONTH);

int d = c1.get(Calendar.DATE);

int h = c1.get(Calendar.HOUR);

int m1 = c1.get(Calendar.MINUTE);

int m2 = m+1;

ta.setText(ta.getText()+""+(y+"年"+m2+"月"+d+"日"+h+":"+m1));

}

}

public void openText() throws IOException

{

FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);

fd.setVisible(true);FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());

ta.setText("");

int n=0;

while((n=fis.read())!=-1)

ta.append(""+(char)n);

fis.close();

}

public void saveText() throws IOException

{

FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.SAVE);

fd.setVisible(true);

FileOutputStream out=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt");

String str=ta.getText();

String str1=ta.getText();

for(int n=0;nstr.length();n++)

out.write((byte)str.charAt(n));

out.close();

}

public static void main(String[] args)

{

Editor f=new Editor(" 记事本程序");

//添加时间代码

Date date=new Date();

System.out.print(date);

/*Calendar now=Calendar.getInstance();

int year=now.get(Calendar.YEAR);

int month=now.get(Calendar.MONTH)+1;

int day=now.get(Calendar.DATE);

System.out.print(year+"年"+month+"月"+day+"日");

int week=now.get(Calendar.DAY_OF_WEEK);

String str="日一二三四五六";//星期1-7

int i=2*(week-1);

System.out.println("星期"+str.substring(i,i+2));//对应中文的下标*/

}

class dialog extends Dialog

{

public dialog()

{

super(dialog, "字体设置");

class Mycanvas extends Canvas

{

Toolkit tk;

Image img;

Mycanvas()

{

setSize(440,138);

tk=getToolkit();

img=tk.getImage("photo.jpg");

}

public void paint(Graphics g)

{

g.drawImage(img,5,120,440,138,this);

}

}

}

}

}

java简单记事本源代码 带解释

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

class test implements ActionListener

{

JFrame frame;

JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31;

JTextArea ta;

JPanel p1,p2,p3,p4;

JMenuBar mb;

JMenu m1,m2,m3;

JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7;

JRadioButton rb1,rb2;

ButtonGroup bg;

Double d1=0.0,d2=0.0,d3=0.0,d4=1.0,d5=1.0;

String s1="",s2="",s3="",s4="";

int a=0;

char c1;

int i=0;

public static void main(String[] args)

{

test that=new test();

that.go();

}

public void go()

{

frame=new JFrame("计算器");

Container cp= frame.getContentPane();

cp.setLayout(new FlowLayout());

b1=new JButton("7");b2=new JButton("8");b3=new JButton("9");b4=new JButton("/");b5=new JButton("1/x");b6=new JButton("sin");b7=new JButton("log");

b8=new JButton("4");b9=new JButton("5");b10=new JButton("6");b11=new JButton("*");b12=new JButton("x^y");b13=new JButton("cos");b14=new JButton("ln");

b15=new JButton("1");b16=new JButton("2");b17=new JButton("3");b18=new JButton("-");b19=new JButton(new ImageIcon("lanying.gif"));b20=new JButton("tan");b21=new JButton("x^3");

b22=new JButton("0");b23=new JButton("+/-");b24=new JButton(".");b25=new JButton("+");b26=new JButton("√x");b27=new JButton("cot");b28=new JButton("x^2");

b29=new JButton("Backspace");b30=new JButton("C");b31=new JButton("=");

mb=new JMenuBar();

m1=new JMenu("文件(F)");m2=new JMenu("编辑(E)");m3=new JMenu("帮助(H)");

mt1=new JMenuItem("清零");mt2=new JMenuItem("退出");mt3=new JMenuItem("复制");mt4=new JMenuItem("粘贴");mt5=new JMenuItem("版本");mt6=new JMenuItem("标准型");mt7=new JMenuItem("科学型");

ta=new JTextArea(1,30);

p1=new JPanel();p2=new JPanel();p3=new JPanel();p4=new JPanel();

rb1=new JRadioButton("科学型");rb2=new JRadioButton("标准型");

bg=new ButtonGroup();

b1.setForeground(Color.blue);b1.setBackground(Color.white);b2.setForeground(Color.blue);b2.setBackground(Color.white);

b3.setForeground(Color.blue);b3.setBackground(Color.white);b8.setForeground(Color.blue);b8.setBackground(Color.white);

b9.setForeground(Color.blue);b9.setBackground(Color.white);b10.setForeground(Color.blue);b10.setBackground(Color.white);

b15.setForeground(Color.blue);b15.setBackground(Color.white);b16.setForeground(Color.blue);b16.setBackground(Color.white);

b17.setForeground(Color.blue);b17.setBackground(Color.white);b22.setForeground(Color.blue);b22.setBackground(Color.white);

b23.setForeground(Color.blue);b23.setBackground(Color.white);b24.setForeground(Color.blue);b24.setBackground(Color.white);

b4.setForeground(Color.red);b4.setBackground(Color.white);b11.setForeground(Color.red);b11.setBackground(Color.white);

b18.setForeground(Color.red);b18.setBackground(Color.white);b25.setForeground(Color.red);b25.setBackground(Color.white);

b5.setForeground(Color.blue);b5.setBackground(Color.white);b6.setForeground(Color.blue);b6.setBackground(Color.white);

b7.setForeground(Color.blue);b7.setBackground(Color.white);b12.setForeground(Color.blue);b12.setBackground(Color.white);

b13.setForeground(Color.blue);b13.setBackground(Color.white);b14.setForeground(Color.blue);b14.setBackground(Color.white);

b19.setForeground(Color.blue);b19.setBackground(Color.white);b20.setForeground(Color.blue);b20.setBackground(Color.white);

b21.setForeground(Color.blue);b21.setBackground(Color.white);b26.setForeground(Color.blue);b26.setBackground(Color.white);

b27.setForeground(Color.blue);b27.setBackground(Color.white);b28.setForeground(Color.blue);b28.setBackground(Color.white);

b29.setForeground(Color.red);b29.setBackground(Color.white);b30.setForeground(Color.red);b30.setBackground(Color.white);

b31.setForeground(Color.red);b31.setBackground(Color.white);

bg.add(rb1);bg.add(rb2);

p1.setBackground(Color.yellow);

cp.setBackground(Color.CYAN);

m1.setMnemonic(KeyEvent.VK_F);m2.setMnemonic(KeyEvent.VK_E);m3.setMnemonic(KeyEvent.VK_H);

m1.add(mt6);m1.add(mt7);m1.addSeparator();m1.add(mt1);m1.addSeparator();m1.add(mt2);m2.add(mt3);m2.addSeparator();m2.add(mt4);m3.add(mt5);

mb.add(m1);mb.add(m2);mb.add(m3);

frame.setJMenuBar(mb);

p2.setLayout(new GridLayout(4,7));

p3.setLayout(new GridLayout(1,3));

ta.setEditable(false);

p1.add(ta);

p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);

p2.add(b8);p2.add(b9);p2.add(b10);p2.add(b11);p2.add(b12);p2.add(b13);p2.add(b14);

p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);

p2.add(b22);p2.add(b23);p2.add(b24);p2.add(b25);p2.add(b26);p2.add(b27);p2.add(b28);

p3.add(b29);p3.add(b30);p3.add(b31);

Border etched=BorderFactory.createEtchedBorder();

Border border=BorderFactory.createTitledBorder(etched,"计算类型");

p4.add(rb1);p4.add(rb2);

p4.setBorder(border);

b2.setActionCommand("8");

b2.addActionListener(this);

cp.add(p1);cp.add(p4);cp.add(p2);cp.add(p3);

frame.setSize(400,330);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b1.setActionCommand("7");

b1.addActionListener(this);

b2.setActionCommand("8");

b2.addActionListener(this);

b3.setActionCommand("9");

b3.addActionListener(this);

b4.setActionCommand("/");

b4.addActionListener(this);

b5.setActionCommand("1/x");

b5.addActionListener(this);

b6.setActionCommand("sin");

b6.addActionListener(this);

b7.setActionCommand("log");

b7.addActionListener(this);

b8.setActionCommand("4");

b8.addActionListener(this);

b9.setActionCommand("5");

b9.addActionListener(this);

b10.setActionCommand("6");

b10.addActionListener(this);

b11.setActionCommand("*");

b11.addActionListener(this);

b12.setActionCommand("x^y");

b12.addActionListener(this);

b13.setActionCommand("cos");

b13.addActionListener(this);

b14.setActionCommand("ln");

b14.addActionListener(this);

b15.setActionCommand("1");

b15.addActionListener(this);

b16.setActionCommand("2");

b16.addActionListener(this);

b17.setActionCommand("3");

b17.addActionListener(this);

b18.setActionCommand("-");

b18.addActionListener(this);

b19.setActionCommand("x!");

b19.addActionListener(this);

b20.setActionCommand("tan");

b20.addActionListener(this);

b21.setActionCommand("x^3");

b21.addActionListener(this);

b22.setActionCommand("0");

b22.addActionListener(this);

b23.setActionCommand("+/-");

b23.addActionListener(this);

b24.setActionCommand(".");

b24.addActionListener(this);

b25.setActionCommand("+");

b25.addActionListener(this);

b26.setActionCommand("√x");

b26.addActionListener(this);

b27.setActionCommand("cot");

b27.addActionListener(this);

b28.setActionCommand("x^2");

b28.addActionListener(this);

b29.setActionCommand("Backspace");

b29.addActionListener(this);

b30.setActionCommand("C");

b30.addActionListener(this);

b31.setActionCommand("=");

b31.addActionListener(this);

rb1.setActionCommand("kxx");

rb1.addActionListener(this);

rb2.setActionCommand("bzx");

rb2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) //throws Exception

{

if (e.getActionCommand()=="bzx")

{

b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);

b12.setEnabled(false);b13.setEnabled(false);b14.setEnabled(false);

b19.setEnabled(false);b20.setEnabled(false);b21.setEnabled(false);

b26.setEnabled(false);b27.setEnabled(false);b28.setEnabled(false);

}

if (e.getActionCommand()=="kxx")

{

b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);

b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);

b19.setEnabled(true);b20.setEnabled(true);b21.setEnabled(true);

b26.setEnabled(true);b27.setEnabled(true);b28.setEnabled(true);

}

if (e.getActionCommand()=="1")

{

ta.append("1");

}

if (e.getActionCommand()=="2")

{

ta.append("2");

}

if (e.getActionCommand()=="3")

{

ta.append("3");

}

if (e.getActionCommand()=="4")

{

ta.append("4");

}

if (e.getActionCommand()=="5")

{

ta.append("5");

}

if (e.getActionCommand()=="6")

{

ta.append("6");

}

if (e.getActionCommand()=="7")

{

ta.append("7");

}

if (e.getActionCommand()=="8")

{

ta.append("8");

}

if (e.getActionCommand()=="9")

{

ta.append("9");

}

if (e.getActionCommand()=="0")

{

ta.append("0");

}

if (e.getActionCommand()=="+")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=1;

}

if (e.getActionCommand()=="-")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=2;

}

if (e.getActionCommand()=="*")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=3;

}

if (e.getActionCommand()=="/")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=4;

}

if (e.getActionCommand()=="=")

{

s2=ta.getText();

d2=Double.parseDouble(s2);

if(i==1)

{

d3=d1+d2;

ta.setText( d3.toString());

}

if(i==2)

{

d3=d1-d2;

ta.setText( d3.toString());

}

if(i==3)

{

d3=d1*d2;

ta.setText( d3.toString());

}

if(i==4)

{

if(d2==0.0)

ta.setText("ERROR");

else

{

d3=d1/d2;

ta.setText( d3.toString());

}

}

if (i==5)

{

s2=ta.getText();

d2 = Double.parseDouble(s2);

for (int l=1;l=d2 ; l++)

{

d5=d5*d1;

}

ta.setText( d5.toString());

}

}

if (e.getActionCommand()=="C")

{

ta.setText("");

d4=1.0;

d5=1.0;

}

/*if (e.getActionCommand()=="Backspace")

{

s3=ta.getText();

a=s3.length();

//ta.cut(ta.select(a-1,a));

s4=ta.getText(1,3);

ta.setText(s4);

}

*/

if (e.getActionCommand()=="1/x")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=1/d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()==".")

{

ta.append(".");

}

if (e.getActionCommand()=="+/-")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=0-d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x^2")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=d1*d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x^3")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=d1*d1*d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x^y")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=5;

// d2=d1*d1*d1;

// ta.setText( d2.toString());

}

if (e.getActionCommand()=="√x")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=Math.sqrt(d1);

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x!")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

if (d10)

{

ta.setText( "error");

}

else if (d1==0)

{

ta.setText( "0.0");

}

else {

for (int k=1;k=d1 ;k++ )

d4=d4*k;

ta.setText( d4.toString());

}

}

if (e.getActionCommand()=="sin")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=Math.sin(3.1415926*d1/180);

ta.setText( d2.toString());

}

}

}

如何用JAVA编写简单的记事本程序?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

import java.awt.datatransfer.*;public class NewEdit

{

public static void main(String args[])

{

MyFrame EditFrame=new MyFrame();

EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

EditFrame.setVisible(true);

}

}class MyFrame extends JFrame

{

public MyFrame()

{ Dimension screenSize=toolKit.getScreenSize();

int screenHeight=screenSize.height;

int screenWidth=screenSize.width;

setSize(screenWidth/2,screenHeight/2);

setLocation(screenWidth/4,screenHeight/4);

Image img=toolKit.getImage("icon.gif");

setIconImage(img);

setTitle("MyEdit");

JMenuBar menuBar=new JMenuBar();

setJMenuBar(menuBar);

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

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

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

menuBar.add(fileMenu);

menuBar.add(editMenu);

menuBar.add(helpMenu);

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

newItem.addActionListener(new NewListener());

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

openItem.addActionListener(new OpenListener());

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

saveItem.addActionListener(new SaveListener());

JMenuItem escItem=new JMenuItem("退出");

escItem.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent myE)

{

System.exit(0);

}

});

fileMenu.add(newItem);

fileMenu.add(openItem);

fileMenu.add(saveItem);

fileMenu.add(escItem);

JMenuItem allItem=new JMenuItem("全选");

//JMenuItem copyItem=new JMenuItem("复制");

//CopyAction copyAction=new CopyAction();

//copyItem.addActionListener(copyAction);

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

//JMenuItem pasteItem=new JMenuItem("粘贴");

//Action pasteAction=new PasteAction();

//pasteItem.addActionListener(pasteAction);

editMenu.add(allItem);

allItem.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent myE)

{

textArea.selectAll();

}

});

CopyAction copyAction=new CopyAction("剪切");

editMenu.add(copyAction);

CutAction cutAction=new CutAction("复制");

editMenu.add(cutAction);

Action pasteAction=new PasteAction("粘贴");

editMenu.add(pasteAction);

popup=new JPopupMenu();

popup.add(copyAction);

popup.add(pasteAction);

popup.add(cutAction);

textArea = new JTextArea();

textArea.add(popup);

textArea.addMouseListener(new

MouseListener(){

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)

{

popup.show(textArea, e.getX(), e.getY());

}

} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单

JScrollPane scroller = new JScrollPane(textArea);

add(scroller);

//OR--this.getContentPane().add(scroller)

//scroller.setComponentPopupMenu(popup);

//textArea.setInheritsPopupMenu(true);

//scroller.addMouseListener(new MouseAdapter(){});

}

public void writeFile(String fileName)

{

try

{

File file = new File(fileName);

FileWriter write = new FileWriter(file);

write.write(textArea.getText());

write.close();

}

catch(Exception e){

System.out.println("Error closing file!");

}

}

public void openFile(String fileName)

{

try

{

File file = new File(fileName);

FileReader readIn = new FileReader(file);

int size = (int)file.length();

int charsRead = 0;

char[] content = new char[size];

while(readIn.ready())

charsRead += readIn.read(content,charsRead,size-charsRead);

readIn.close();

textArea.setText(new String(content,0,charsRead));

}

catch(IOException e)

{

System.out.println("Error opening file!");

}

}

private class NewListener implements ActionListener

{

public void actionPerformed(ActionEvent myE)

{

textArea.setText("");

}

}

private class OpenListener implements ActionListener

{

public void actionPerformed(ActionEvent myE)

{

openFileDialog.setVisible(true);

fileName = openFileDialog.getDirectory()+openFileDialog.getFile();

if(fileName != null)

{

openFile(fileName);

}

}

}

private class SaveListener implements ActionListener

{

public void actionPerformed(ActionEvent myE)

{

saveFileDialog.setVisible(true);

fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();

if(fileName !=null)

{

writeFile(fileName);

}

}

}

private class CutAction extends AbstractAction

{

public CutAction(String name)

{

super(name);

}

public void actionPerformed(ActionEvent event)

{

String text = textArea.getSelectedText();

StringSelection selection = new StringSelection(text);

clipboard.setContents(selection,null);

textArea.replaceRange("",textArea.getSelectionStart(),

textArea.getSelectionEnd());

}

}

private class CopyAction extends AbstractAction

{

public CopyAction(String name)

{

super(name);

}

public void actionPerformed(ActionEvent event)

{

String text = textArea.getSelectedText();

StringSelection selection= new StringSelection(text);

clipboard.setContents(selection,null);

}

}

private class PasteAction extends AbstractAction

{

public PasteAction(String name)

{

super(name);

}

public void actionPerformed(ActionEvent event)

{

Transferable contents = clipboard.getContents(this);

if(contents==null)

return;

String text;

text="";

try

{

text = (String)contents.getTransferData(DataFlavor.stringFlavor);

}

catch(Exception ex){}

textArea.replaceRange(text,

textArea.getSelectionStart(),textArea.getSelectionEnd());

}

}

private JTextArea textArea;

private JPopupMenu popup;

private String fileName="";

private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);

Toolkit toolKit = Toolkit.getDefaultToolkit();

private Clipboard clipboard = toolKit.getSystemClipboard();}

java程序设计,编写一个记事本程序

全实现,程序太长,发不上去,先实现基本的 图形用户界面

import java.awt.*;

import java.awt.event.*;

public class TestMenu {

public static void main (String[] args) {

new MenuFrame("新建"+" "+"文本文档"+".txt"+" "+"-"+" "+"记事本").launchFrame();

}

}

class MenuFrame extends Frame {

MenuBar mb = null;

MenuFrame (String s) {

super (s);

}

public void launchFrame() {

Menu file = new Menu ("文件");

Menu edit = new Menu ("编辑");

Menu format = new Menu ("格式");

Menu help = new Menu ("帮助");

MenuItem newItem = new MenuItem ("新建");

newItem.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e) {

final Frame ff = new Frame ("记事本");

ff.setMenuBar(mb);

ff.setBounds (300,300,400,200);

ff.setVisible (true);

ff. addWindowListener (new WindowAdapter () {

public void windowClosing(WindowEvent e) {

ff.setVisible (false);

}

} );

}

});

MenuItem saveItem = new MenuItem ("保存");

MenuItem exitItem = new MenuItem ("退出");

MenuItem helpTitle = new MenuItem ("帮助主题");

MenuItem line = new MenuItem ("-");

MenuItem about = new MenuItem ("关于记事本");

MenuItem copy = new MenuItem ("粘贴");

MenuItem serach = new MenuItem ("查找");

edit.add (copy);

edit.add (serach);

help.add (helpTitle);

help.add (line);

help.add (about);

exitItem.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e) {

System.exit (0);

}

} );

file.add (newItem);

file.add (saveItem);

file.add (exitItem);

mb = new MenuBar();

mb.add (file);

mb.add (edit);

mb.add (format);

mb.add (help);

addWindowListener (new WindowAdapter () {

public void windowClosing(WindowEvent e) {

System.exit (0);

}

} );

setLayout (new FlowLayout());

setMenuBar (mb);

setBounds (300,300,400,200);

setVisible (true);

}

}

关于java简易记事本和java简易记事本流程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。