成都网站建设设计

将想法与焦点和您一起共享

java写字板代码 java手写代码

java记事本源代码

给你个做好了的Java的源程序的记事本,自己看看就行了的,不怎么难的···

专注于为中小企业提供做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业新平免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

public class MyNotepad implements ActionListener{

private JFrame frame=new JFrame("新记事本");

private JTextArea jta=new JTextArea();

private String result="";

private boolean flag=true;

private File f;

private JButton jb=new JButton("开始");

private JTextField jtf=new JTextField(15);

private JTextField jt=new JTextField(15);

private JButton jbt=new JButton("替换为");

private JButton jba=new JButton("全部替换");

private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");

private String value;

private int start=0;

private JFrame jf=new JFrame("查找");

private JFrame jfc=new JFrame("替换");

@Override

public void actionPerformed(ActionEvent e) {

String comm=e.getActionCommand();

if("新建".equals(comm)){

if(!(frame.getTitle().equals("新记事本"))){

if(!flag){

write();

newNew();

}else{

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

write();

}

}

}else if(!(jta.getText().isEmpty())){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

write();

newNew();

}

}else{

newNew();

}

}else if("打开".equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

jfc.setDialogType(JFileChooser.OPEN_DIALOG);

int returnVal = jfc.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

frame.setTitle(f.getName());

result=read();

flag=false;

value=result;

jta.setText(result);

}

}else if("保存".equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

if(flag){

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

write();

}

}else{

write();

}

}else if("另存".equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"另存");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

write();

}

}else if("退出".equals(comm)){

System.exit(0);

}else if("撤销".equals(comm)){

jta.setText(value);

}else if("剪切".equals(comm)){

value=jta.getText();

jta.cut();

}else if("复制".equals(comm)){

jta.copy();

}else if("粘贴".equals(comm)){

value=jta.getText();

jta.paste();

}else if("删除".equals(comm)){

value=jta.getText();

jta.replaceSelection(null);

}else if("全选".equals(comm)){

jta.selectAll();

}else if("查找".equals(comm)){

value=jta.getText();

jf.add(jtf,BorderLayout.CENTER);

jf.add(jb,BorderLayout.SOUTH);

jf.setLocation(300,300);

jf.pack();

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}else if("替换".equals(comm)){

value=jta.getText();

GridLayout gl=new GridLayout(3,3);

JLabel jl1=new JLabel("查找内容:");

JLabel jl2=new JLabel("替换为:");

jfc.setLayout(gl);

jfc.add(jl1);

jfc.add(jtf);

jfc.add(jb);

jfc.add(jl2);

jfc.add(jt);

jfc.add(jbt);

JLabel jl3=new JLabel();

JLabel jl4=new JLabel();

jfc.add(jl3);

jfc.add(jl4);

jfc.add(jba);

jfc.setLocation(300,300);

jfc.pack();

jfc.setVisible(true);

jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}else if("版本".equals(comm)){

JDialog jd=new JDialog(frame,"关于对话框");

jd.setSize(200,200);

JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");

jd.add(l,BorderLayout.CENTER);

jd.setLocation(100,200);

jd.setSize(300,300);

jd.setVisible(true);

// jd.pack();

jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

}else if("开始".equals(comm)||"下一个".equals(comm)){

String temp=jtf.getText();

int s=value.indexOf(temp,start);

if(value.indexOf(temp,start)!=-1){

jta.setSelectionStart(s);

jta.setSelectionEnd(s+temp.length());

jta.setSelectedTextColor(Color.GREEN);

start=s+1;

jb.setText("下一个");

// value=value.substring(s+temp.length());//不能截取字串

}else {

JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jf.dispose();

}

}else if("替换为".equals(comm)){

String temp=jtf.getText();

int s=value.indexOf(temp,start);

if(value.indexOf(temp,start)!=-1){

jta.setSelectionStart(s);

jta.setSelectionEnd(s+temp.length());

jta.setSelectedTextColor(Color.GREEN);

start=s+1;

jta.replaceSelection(jt.getText());

}else {

JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jf.dispose();

}

}else if("全部替换".equals(comm)){

String temp=jta.getText();

temp=temp.replaceAll(jtf.getText(), jt.getText());

jta.setText(temp);

}

}

public String read(){

String temp="";

try {

FileInputStream fis = new FileInputStream(f.getAbsolutePath());

byte[] b=new byte[1024];

while(true){

int num=fis.read(b);

if(num==-1)break;

temp=temp+new String(b,0,num);

}

fis.close();

} catch (Exception e1) {

e1.printStackTrace();

}

return temp;

}

public void write(){

try {

FileOutputStream fos=new FileOutputStream(f);

fos.write(jta.getText().getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public void newNew(){

frame.dispose();

new MyNotepad();

flag=true;

}

public MyNotepad(){

JMenuBar jmb=new JMenuBar();

String[] menuLab={"文件","编辑","帮助"};

String[][] menuItemLab={{"新建","打开","保存","另存","退出"},

{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},

{"版本"}};

for(int i=0;imenuLab.length;i++){

JMenu menu=new JMenu(menuLab[i]);

jmb.add(menu);

for(int j=0;jmenuItemLab[i].length;j++){

JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);

menu.add(jmi);

jmi.addActionListener(this);

}

}

frame.setJMenuBar(jmb);

jta.setLineWrap(true);//自动换行

JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板

frame.add(jsp);

jb.addActionListener(this);

jbt.addActionListener(this);

jba.addActionListener(this);

frame.setLocation(200,50);

frame.setSize(620,660);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new MyNotepad();

}

}

帮忙在JAVA程序写字板添加保存,另存为等其他功能

import java.awt.*;

import java.awt.event.*;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FilenameFilter;

import java.io.IOException;

import java.io.OutputStream;

import javax.swing.JFrame;

import wyq.study.io.FileLister;

public class MenuDemo extends Frame implements ActionListener

{

TextArea tf = new TextArea();

MenuBar bar = new MenuBar();

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

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

MenuItem open = new MenuItem("打开");

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

MenuItem saveFileAs = new MenuItem("另存为");

MenuItem close = new MenuItem("关闭");

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

public MenuDemo()

{

super("MenuDemo");

setMenuBar(bar);

menu.add(newf);

menu.add(open);

menu.add(close);

menu.add(saveFile);

menu.add(saveFileAs);

menu.addSeparator();

menu.add(quit);

bar.add(menu);

newf.addActionListener(this);

open.addActionListener(this);

close.addActionListener(this);

quit.addActionListener(this);

saveFile.addActionListener(this);

saveFileAs.addActionListener(this);

//addWindowListener(new WinLis());

//关闭窗口

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

dispose();

}

});

add(tf);

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == newf)

{

tf.setText("新建");

}

if (e.getSource() == open)

{

tf.setText("打开");

}

if (e.getSource() == close)

{

tf.setText("关闭");

}

if (e.getSource() ==saveFile)

{

tf.setText("保存");

saveFile();

}

if (e.getSource() == saveFileAs)

{

tf.setText("另存为");

saveFileAs();

}

if (e.getSource() == quit)

{

System.exit(0);

}

}

public void saveFile()

{

String directory = System.getProperty("user.dir");

FileDialog f = new FileDialog(this, "Save File", FileDialog.SAVE);

f.setDirectory(directory);

f.show();

String fileName =f.getFile(); // 得到保存的文件名

directory=f.getDirectory();//得到文件路径

//System.out.println(directory+" "+file);

String txt=tf.getText();

File file=new File(directory+fileName);

OutputStream out=null;

try

{

out=new FileOutputStream(file);

}

catch (FileNotFoundException e)

{

}

try

{

out.write(txt.getBytes());

}

catch (IOException e)

{

e.printStackTrace();

}

try

{

if(out!=null)

out.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

public void saveFileAs()

{

saveFile();

}

class WinLis extends WindowAdapter

{

public void windowCosing(WindowEvent e)

{

System.exit(0);

}

}

public static void main(String[] args)

{

Frame f = new MenuDemo();

f.setSize(400, 200);

f.setVisible(true);

}

}

Java语言编写一个类似Word的写字板之类

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

class TextEditorFrame extends JFrame{

File file=null;

Color color=Color.red;

TextEditorFrame(){

initTextPane();

initAboutDialog();

initToolBar();

initMenu();

}

void initTextPane(){

getContentPane().add(new JScrollPane(text));

}

JTextPane text=new JTextPane(); //这是用来做文本框的

JFileChooser filechooser=new JFileChooser(); //文件选择框

JColorChooser colorchooser=new JColorChooser();//

JDialog about=new JDialog(this); //关于对话框

JMenuBar menubar=new JMenuBar();//菜单

JMenu[] menus=new JMenu[]{

new JMenu("文件"),

new JMenu("编辑"),

new JMenu("帮助")

};

JMenuItem menuitems[][]=new JMenuItem[][]{{

new JMenuItem("新建"),

new JMenuItem("打开"),

new JMenuItem("保存"),

new JMenuItem("退出")

},

{

new JMenuItem("复制"),

new JMenuItem("剪切"),

new JMenuItem("粘贴"),

new JMenuItem("颜色")

},

{

new JMenuItem("关于")

}

};

void initMenu(){

for(int i=0;imenus.length;i++){

menubar.add(menus[i]);

for(int j=0;jmenuitems[i].length;j++){

menus[i].add(menuitems[i][j]);

menuitems[i][j].addActionListener( action );

}

}

this.setJMenuBar(menubar);

}

ActionListener action=new ActionListener(){ //when here have not wrong:

public void actionPerformed(ActionEvent e){

JMenuItem mi=(JMenuItem)e.getSource();

String id=mi.getText();

if(id.equals("新建")){

text.setText("");

file=null;

}else if(id.equals("打开")){

if(file !=null)filechooser.setSelectedFile(file);

int returnVal=filechooser.showOpenDialog(TextEditorFrame.this);

if(returnVal==JFileChooser.APPROVE_OPTION){

file=filechooser.getSelectedFile();

openFile();

}

}else if(id.equals("保存")){

if(file!=null) filechooser.setSelectedFile(file);

int returnVal=filechooser.showSaveDialog(TextEditorFrame.this);

if(returnVal==JFileChooser.APPROVE_OPTION){

file=filechooser.getSelectedFile();

saveFile();

}

}else if(id.equals("退出")){

TextEditorFrame f=new TextEditorFrame();

int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_CANCEL_OPTION);

if(s==JOptionPane.YES_OPTION)

System.exit(0);

}else if(id.equals("剪切")){

text.cut();

}else if(id.equals("复制")){

text.copy();

}else if(id.equals("粘贴")){

text.paste();

}else if(id.equals("color")){

color=JColorChooser.showDialog(TextEditorFrame.this,"",color);

text.setForeground(color);

}else if(id.equals("关于")){

about.setSize(200,150);

about.show();

}

}

};

void saveFile(){

try{

FileWriter fw=new FileWriter(file);

fw.write(text.getText());

fw.close();

}

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

}

void openFile(){

try{

FileReader fr=new FileReader(file);

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

char []buffer=new char[len];

fr.read(buffer,0,len);

fr.close();

text.setText(new String(buffer));

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

}

void initAboutDialog(){

about.getContentPane().add(new JLabel("作者-覃树新:记事本Q1.0版本"));

about.setModal(true);

about.setSize(200,100);

}

JToolBar toolbar=new JToolBar();//我来加上工具条

JButton [] buttons=new JButton[]{

new JButton("", new ImageIcon("qin.jpg")),

new JButton("", new ImageIcon("shu.jpg")),

new JButton("", new ImageIcon("xin.jpg"))

};

void initToolBar(){

for(int i=0;ibuttons.length;i++)

toolbar.add(buttons[i]);

buttons[0].setToolTipText("复制");

buttons[0].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

text.copy();

}

});

buttons[1].setToolTipText("剪切");

buttons[1].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

text.cut();

}

});

buttons[2].setToolTipText("粘贴");

buttons[2].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

text.paste();

}

});

this.getContentPane().add(toolbar,BorderLayout.NORTH);

}

}

public class F{

public static void main(String args[]){

TextEditorFrame f=new TextEditorFrame();

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

TextEditorFrame f=new TextEditorFrame();

int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_OPTION);

if(s==JOptionPane.YES_OPTION)

System.exit(0);}

});

f.setTitle("简单的记事本");

f.setSize(800,600);

f.show();

}

}


标题名称:java写字板代码 java手写代码
链接地址:http://chengdu.cdxwcx.cn/article/doiccdh.html