真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼文本編輯器,基于java的文本編輯器設(shè)計(jì)與實(shí)現(xiàn)

求JAVA編寫的文本編輯器。

import java.awt.*;

成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、網(wǎng)站制作與策劃設(shè)計(jì),定日網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:定日等地區(qū)。定日做網(wǎng)站價(jià)格咨詢:18982081108

import java.awt.event.*;

import java.io.*;

public class Notepad /*implements ActionListener , MouseListener , MouseMotionListener , WindowListener , ItemListener , KeyListener, TextListener */

{

//成員變量

private Frame mainFrame;//主框架

private MenuBar mb ; //菜單條

private Menu mFile , mEdit , mFormat , mHelp ; //菜單:文件,編輯,格式,幫助

private MenuItem miNew , miOpen , miSave , miSaveAs , miExit ;//文件菜單項(xiàng):新建,打開,保存,另存為,退出

private MenuItem miCut , miCopy , miPaste , miDelete ;//編輯菜單項(xiàng):剪切,復(fù)制,粘貼,刪除

private MenuItem miFont , miLowtoCapital, miCapitaltoLow ,miEncrypt , miDisencrypt;//格式菜單項(xiàng):字體

private MenuItem miAboutNotepad;//幫助菜單項(xiàng):關(guān)于記事本

private TextArea ta;//文本區(qū)

private String tempString;//臨時(shí)字符串,用于存儲需要復(fù)制粘貼的字符串

private boolean textValueChanged = false;

private int id_font ;//字體

String fileName = "";//上次保存后的文件名和地址

//構(gòu)造函數(shù)

public Notepad(){

//框架

mainFrame = new Frame ("Notepad v0.99 by Launching");

mb = new MenuBar ();

ta = new TextArea (30 ,60);

ta.setFont( new Font ( "Times New Rome" , Font.PLAIN , 15));

ta.setBackground(new Color(0 , 250 , 200));

//菜單條

mFile = new Menu ( "File");

mEdit = new Menu ( "Edit");

mFormat = new Menu ("Format");

mHelp = new Menu ("Help");

//"文件"

miNew = new MenuItem ("New");

miOpen = new MenuItem ("Open");

miSave = new MenuItem ("Save");

miSaveAs = new MenuItem ("Save as");

miExit = new MenuItem ("Exit");

//"編輯"

miCut = new MenuItem ("Cut");

miCopy = new MenuItem ("Copy");

miPaste = new MenuItem ("Paste");

miDelete = new MenuItem ("Delete");

//"格式"

miFont = new MenuItem ("Font");

miLowtoCapital = new MenuItem("Low to Capital");

miCapitaltoLow = new MenuItem("Capital to Low");

miEncrypt = new MenuItem("Encrypt");

miDisencrypt = new MenuItem("Disencrypt");

//"幫助"

miAboutNotepad = new MenuItem ("About Notepad");

//添加文件菜單項(xiàng)

mFile.add(miNew);

mFile.add(miOpen);

mFile.add(miSave);

mFile.add(miSaveAs);

mFile.add(miExit);

//添加編輯菜單項(xiàng)

mEdit.add(miCut);

mEdit.add(miCopy);

mEdit.add(miPaste);

mEdit.add(miDelete);

//添加格式菜單項(xiàng)

mFormat.add(miFont);

mFormat.add(miLowtoCapital);

mFormat.add(miCapitaltoLow);

mFormat.add(miEncrypt);

mFormat.add(miDisencrypt);

//添加幫助菜單項(xiàng)

mHelp.add(miAboutNotepad);

//菜單條添加菜單

mb.add(mFile);

mb.add(mEdit);

mb.add(mFormat);

mb.add(mHelp);

//框架添加菜單條

mainFrame.setMenuBar( mb );

//初始字符串賦為空

tempString = "";

//添加文本區(qū)

mainFrame.add(ta, BorderLayout.CENTER);

mainFrame.setSize(800 , 500);

mainFrame.setLocation( 100 ,100);// 起始位置

mainFrame.setResizable(true);//不可更改大小

mainFrame.setVisible(true);

//mainFrame.pack();

//////////////////////////////////////////////////////////////////////////////////

////////////////////////////////增加監(jiān)視器////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

//主框架

mainFrame.addWindowListener(new WindowAdapter (){ //關(guān)閉窗口

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

//文本區(qū)

ta.addKeyListener( new KeyAdapter(){

public void KeyTyped(KeyEvent e){

textValueChanged = true ; //鍵盤按鍵按下即導(dǎo)致文本修改

}

});

////////////////"文件"菜單://////////////////////

//新建

miNew.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

ta.replaceRange("", 0 , ta.getText().length()) ;//清空文本區(qū)的內(nèi)容

fileName = "";//文件名清空

}

});

//打開

miOpen.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

FileDialog d=new FileDialog(mainFrame , "open file" , FileDialog.LOAD );//打開文件對話框

d.addWindowListener( new WindowAdapter(){ //關(guān)閉文件對話框窗口

public void windowClosing(WindowEvent ee){

System.exit(0);

}

});

d.setVisible(true);

File f = new File( d.getDirectory()+d.getFile() ); //建立新文件

fileName = d.getDirectory()+d.getFile();//得到文件名

char ch[] = new char [(int)f.length()];///用此文件的長度建立一個字符數(shù)組

try//異常處理

{

//讀出數(shù)據(jù),并存入字符數(shù)組ch中

BufferedReader bw = new BufferedReader( new FileReader(f) );

bw.read(ch);

bw.close();

}

catch( FileNotFoundException fe ){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie){

System.out.println("IO error");

System.exit(0);

}

String s =new String (ch);

ta.setText(s);//設(shè)置文本區(qū)為所打開文件的內(nèi)容

}

});

//保存

miSave.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

if( fileName.equals("") ){ //如果文件沒有被保存過,即文件名為空

FileDialog d=new FileDialog(mainFrame , "save file" , FileDialog.SAVE );//保存文件對話框

d.addWindowListener( new WindowAdapter(){ //關(guān)閉文件對話框窗口

public void windowClosing(WindowEvent ee){

System.exit(0);

}

});

d.setVisible(true);

String s = ta.getText();//得到所輸入的文本內(nèi)容

try//異常處理

{

File f = new File( d.getDirectory()+d.getFile());//新建文件

fileName = d.getDirectory()+d.getFile();//得到文件名

BufferedWriter bw = new BufferedWriter( new FileWriter (f));//輸入到文件中

bw.write(s , 0 , s.length());

bw.close();

}

catch(FileNotFoundException fe_){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie_)

{

System.out.println(" IO error");

System.exit(0);

}

}

else //如果文件已經(jīng)保存過

{

String s = ta.getText();//得到所輸入的文本內(nèi)容

try//異常處理

{

File f = new File( fileName );//新建文件

BufferedWriter bw = new BufferedWriter( new FileWriter (f));//輸入到文件中

bw.write(s , 0 , s.length());

bw.close();

}

catch(FileNotFoundException fe_){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie_)

{

System.out.println(" IO error");

System.exit(0);

}

}

}

});

//另存為

miSaveAs.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

FileDialog d=new FileDialog(mainFrame , "save file" , FileDialog.SAVE );//保存文件對話框

d.addWindowListener( new WindowAdapter(){ //關(guān)閉文件對話框窗口

public void windowClosing(WindowEvent ee){

System.exit(0);

}

});

d.setVisible(true);

String s = ta.getText();//得到所輸入的文本內(nèi)容

try//異常處理

{

File f = new File( d.getDirectory()+d.getFile());//新建文件

BufferedWriter bw = new BufferedWriter( new FileWriter (f));//輸入到文件中

bw.write(s , 0 , s.length());

bw.close();

}

catch(FileNotFoundException fe_){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie_)

{

System.out.println(" IO error");

System.exit(0);

}

}

});

//退出

miExit.addActionListener( new ActionListener(){ ///退出程序

public void actionPerformed(ActionEvent e){

System.exit(0);

}

});

////////////////"編輯"菜單:////////////////////

//剪切

miCut.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

tempString = ta.getSelectedText(); ///得到要復(fù)制的內(nèi)容,暫存在tempString中

StringBuffer tmp = new StringBuffer ( ta.getText());//臨時(shí)存儲文本

int start = ta.getSelectionStart(); //得到要刪除的字符串的起始位置

int len = ta.getSelectedText().length(); //得到要刪除的字符串的長度

tmp.delete( start , start+len); ///刪除所選中的字符串

ta.setText(tmp.toString());//用新文本設(shè)置原文本

}

});

//復(fù)制

miCopy.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

tempString = ta.getSelectedText(); ///得到要復(fù)制的內(nèi)容,暫存在tempString中

}

});

//粘貼

miPaste.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

StringBuffer tmp = new StringBuffer ( ta.getText());//臨時(shí)存儲文本

int start = ta.getSelectionStart(); //得到要粘貼的位置

tmp.insert(start , tempString);//查入要粘貼的內(nèi)容

ta.setText(tmp.toString());//用新文本設(shè)置原文本

}

});

//刪除

miDelete.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

StringBuffer tmp = new StringBuffer ( ta.getText());//臨時(shí)存儲文本

int start = ta.getSelectionStart(); //得到要刪除的字符串的起始位置

int len = ta.getSelectedText().length(); //得到要刪除的字符串的長度

tmp.delete( start , start+len); ///刪除所選中的字符串

ta.setText(tmp.toString());//用新文本設(shè)置原文本

}

});

////////////////"格式"菜單:////////////////////

//字體

miFont.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

final Dialog d = new Dialog ( mainFrame , "Font");//新建對話框

d.setLocation( 250 ,250);// 起始位置

d.setLayout( new BorderLayout());//表格布局

//////////////////////////上部分面板

Label l_font = new Label ("font");//font標(biāo)簽

Panel p_1 = new Panel();

p_1.add(l_font);

p_1.setVisible(true);

//////////////////////////中部分面板

List font_list = new List (6 , false);//字體列表

//添加字體項(xiàng)目

font_list.add("Plain");///普通字體

font_list.add("Bold"); ///粗體

font_list.add("Italic");//斜體

font_list.addItemListener( new MyItemListener_font() ); //字體增加監(jiān)視器

Panel p_2 = new Panel();

p_2.add(font_list);

p_2.setVisible(true);

//////////////////////////下部分面板

Button ok = new Button ("OK");

ok.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

d.dispose();

}

});

ok.setSize( new Dimension (20 , 5) );

Panel p_3 = new Panel();//下部分面板

p_3.add(ok);

p_3.setVisible(true);

//添加三個面板

d.add(p_1 , BorderLayout.NORTH);

d.add(p_2 , BorderLayout.CENTER);

d.add(p_3 , BorderLayout.SOUTH);

d.pack();

d.addWindowListener( new WindowAdapter(){ //關(guān)閉對話框窗口

public void windowClosing(WindowEvent ee){

d.dispose();

}

});

d.setVisible(true);

}

});

//小寫字母轉(zhuǎn)大寫

miLowtoCapital.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所輸入的文本內(nèi)容

StringBuffer temp = new StringBuffer("");

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

if((int)s.charAt(i)=97 (int)s.charAt(i)=122 ){

temp.append((char)((int)s.charAt(i)-32));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

//大寫字母轉(zhuǎn)小寫

miCapitaltoLow.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所輸入的文本內(nèi)容

StringBuffer temp = new StringBuffer("");

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

if((int)s.charAt(i)=65 (int)s.charAt(i)=90 ){

temp.append((char)((int)s.charAt(i)+32));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

//加密

miEncrypt.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所輸入的文本內(nèi)容

StringBuffer temp = new StringBuffer("");

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

if(s.charAt(i)=40 s.charAt(i)=125){

if(i%2==0){

temp.append((char)(s.charAt(i) + 1 ));

}

else

temp.append((char)(s.charAt(i) - 1 ));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

//解密

miDisencrypt.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所輸入的文本內(nèi)容

StringBuffer temp = new StringBuffer("");

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

if(s.charAt(i)=40 s.charAt(i)=125){

if(i%2==0){

temp.append((char)(s.charAt(i) - 1 ));

}

else

temp.append((char)(s.charAt(i) + 1 ));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

////////////////"幫助"菜單:////////////////////

//關(guān)于記事本

miAboutNotepad.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

final Dialog d = new Dialog ( mainFrame , "AboutNotepad");//新建對話框

TextArea t = new TextArea("\nwelcome to use Notepad " + "\n\n" + "Copyright@Launching " + "\n\n" + "free software" + "\n\n" + "v0.99");//添加標(biāo)簽

t.setSize( new Dimension ( 5 , 5));

t.setEditable(false);

d.setResizable(false);//不可調(diào)整大小

d.add(t);

d.pack();

d.addWindowListener( new WindowAdapter(){ //關(guān)閉對話框窗口

public void windowClosing(WindowEvent ee){

d.dispose();

}

});

d.setLocation( 100 ,250);// 起始位置

d.setVisible(true);

}

});

}

class MyItemListener_font implements ItemListener { //字體監(jiān)聽器

public void itemStateChanged(ItemEvent e) {

id_font = ((java.awt.List)e.getSource()).getSelectedIndex();

switch( id_font){

case 0:{

ta.setFont(new Font("Times New Roman", Font.PLAIN ,ta.getFont().getSize()) );//普通文字

break;

}

case 1:{

ta.setFont(new Font("Times New Roman" , Font.BOLD ,ta.getFont().getSize()) );//粗體文字

break;

}

case 2:{

ta.setFont(new Font("Times New Roman" , Font.ITALIC ,ta.getFont().getSize()) );//斜體文字

break;

}

}

}

}

/////////////////////////////////////////主函數(shù)///////////////////////////////////////////////////

public static void main(String arg[]){

Notepad test = new Notepad(); ///創(chuàng)建記事本

}

//////////////////////////////////////////////////////////////////////////////////////////////////

}

JAVA編寫好的文本編輯器代碼怎么運(yùn)行啊

import?java.awt.BorderLayout;

import?java.awt.event.ActionEvent;

import?java.awt.event.ActionListener;

import?java.io.FileReader;

import?java.io.FileWriter;

import?java.io.IOException;

import?javax.swing.JFrame;

import?javax.swing.JMenu;

import?javax.swing.JMenuBar;

import?javax.swing.JMenuItem;

import?javax.swing.JScrollPane;

import?javax.swing.JTextArea;

import?javax.swing.SwingUtilities;

public?class?MenuTester?extends?JFrame?implements?ActionListener

{

private?static?final?long?serialVersionUID?=?1L;

private?static?final?String?FILE?=?"d:/1.txt";

JTextArea?textArea;

JScrollPane?scrollPane;

public?MenuTester?()

{

setTitle?("MenuTester");

setLayout?(new?BorderLayout?());

JMenuBar?menuBar?=?new?JMenuBar?();

JMenu[]?menu?=?new?JMenu[]?{?new?JMenu?("文件"),?new?JMenu?("編輯")?};

JMenuItem[]?menuItem1?=?new?JMenuItem[]?{?new?JMenuItem?("新建"),?new?JMenuItem?("打開"),?new?JMenuItem?("保存"),?new?JMenuItem?("關(guān)閉")?};

JMenuItem[]?menuItem2?=?new?JMenuItem[]?{?new?JMenuItem?("復(fù)制"),?new?JMenuItem?("粘貼"),?new?JMenuItem?("剪切")?};

for?(?int?i?=?0;?i??menu.length;?i++?)

{

menuBar.add?(menu[i]);

}

for?(?int?i?=?0;?i??menuItem1.length;?i++?)

{

menu[0].add?(menuItem1[i]);

}

for?(?int?i?=?0;?i??menuItem2.length;?i++?)

{

menu[1].add?(menuItem2[i]);

}

menuItem1[0].addActionListener?(this);

menuItem1[1].addActionListener?(this);

menuItem1[2].addActionListener?(this);

menuItem1[3].addActionListener?(this);

setJMenuBar?(menuBar);

textArea?=?new?JTextArea?();

scrollPane?=?new?JScrollPane?(textArea);

scrollPane.setVisible?(false);

add?(scrollPane,?BorderLayout.CENTER);

setResizable?(false);

setSize?(800,?600);

setLocationRelativeTo?(null);

setDefaultCloseOperation?(JFrame.EXIT_ON_CLOSE);

setVisible?(true);

}

public?static?void?main?(?String[]?args?)

{

SwingUtilities.invokeLater?(new?Runnable?()

{

@Override

public?void?run?()

{

new?MenuTester?();

}

});

}

@Override

public?void?actionPerformed?(?ActionEvent?e?)

{

String?command?=?e.getActionCommand?();

if?("新建".equals?(command))

{

scrollPane.setVisible?(true);

validate?();

}

else?if?("關(guān)閉".equals?(command))

{

scrollPane.setVisible?(false);

textArea.setText?("");

validate?();

}

else?if?("打開".equals?(command))

{

scrollPane.setVisible?(true);

validate?();

try

{

FileReader?fr?=?new?FileReader?(FILE);

char[]?cs?=?new?char[1];

while?(-1?!=?(?fr.read?(cs)?))

{

textArea.append?(new?String?(cs));

}

fr.close?();

}

catch?(Exception?e1)

{

e1.printStackTrace?();

}

}

else?if?("保存".equals?(command))

{

try

{

FileWriter?fw?=?new?FileWriter?(FILE);

fw.write?(textArea.getText?());

fw.flush?();

fw.close?();

}

catch?(IOException?e1)

{

e1.printStackTrace();

}

}

}

}

java 文本編輯器

這么說吧,其實(shí)java版的記事本就是一個JTextArea再加上一些菜單項(xiàng),再加上查找對話框、替換對話框等。

除了在“打開”“保存”“新建”等操作時(shí)要判斷一下是否彈出“編輯文件已改變,是否保存”的對話框,別的沒多大難度吧。。。。

參考網(wǎng)址里是一個基本實(shí)現(xiàn)所有windows自帶記事本的程序,里面分ReplaceDialog.java、Notepad.java、FontDialog.java、FindDialog.java四個文件,顧名思義,共要建四個類。

你要自己實(shí)現(xiàn)的話,可以一步一步地來,慢慢加入復(fù)雜的功能。

要的話留下郵箱。

能編譯運(yùn)行java的簡單文本編輯器

import java.awt.BorderLayout;

public class WordProcessSystem extends JFrame{

private JFileChooser chooser;

private JTextArea textArea;

private File file;

private String string = "";

private Font font;

public WordProcessSystem(){

super();

setTitle("文字處理系統(tǒng)");

setBounds(100, 100,600, 500);

getContentPane().setLayout(new BorderLayout());

getContentPane().setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();

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

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

menuItem.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String str1 = JOptionPane.showInputDialog("請輸入新建文件名:");

try {

file = new File("E:\\", str1 + ".txt");

if(file.exists()){

JOptionPane.showMessageDialog(null, "文件名已存在!");

}

else{

file.createNewFile();

}

} catch (Exception e1) {

// TODO: handle exception

}

}

});

chooser = new JFileChooser();

chooser.setCurrentDirectory(new File("."));

JMenuItem menuItem1 = new JMenuItem("打開");

menuItem1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int result = chooser.showOpenDialog(null);

if(result == JFileChooser.APPROVE_OPTION){

String str = chooser.getSelectedFile().toString();

try {

file = new File(str);

readFromFile(file);

textArea.setText(string);

} catch (Exception e1) {

// TODO: handle exception

}

}

}

});

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

menuItem2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

System.exit(0);

}

});

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

menuItem3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String string = textArea.getText();

try {

writeToFile(file, string);

} catch (Exception e1) {

// TODO: handle exception

}

}

});

menuItem3.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){

if(e.getKeyChar() == KeyEvent.VK_C){

// if(e.getKeyChar() == KeyEvent.VK_S){

String string = textArea.getText();

try {

writeToFile(file, string);

} catch (Exception e1) {

// TODO: handle exception

}

// }

}

}

});

JMenuItem menuItem4 = new JMenuItem("另存為");

menuItem4.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String s1 = JOptionPane.showInputDialog("請輸入保存文件路徑和文件名:");

file = new File(s1);

System.out.println(file.getPath());

try {

writeToFile(file, string);

System.out.println("aaaa");

} catch (Exception e1) {

// TODO: handle exception

}

}

});

menu.add(menuItem);

menu.add(menuItem1);

menu.add(menuItem2);

menu.add(menuItem3);

menu.add(menuItem4);

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

final JMenu menu3 = new JMenu("字體");

String []fontString = {"宋體","楷體","隸書"};

final JMenuItem []menu3_1 = new JMenuItem[fontString.length];

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

String changeString = "";

menu3_1[i] = new JMenuItem(fontString[i]);

menu3_1[i].setActionCommand(changeString + i);

menu3_1[i].addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method

switch (e.getActionCommand().charAt(0)) {

case '0':

font = new Font("宋體",Font.PLAIN,20);

textArea.setFont(font);

break;

case '1':

font = new Font("楷體",Font.PLAIN,18);

textArea.setFont(font);

break;

case '2':

font = new Font("隸書",Font.BOLD,18);

textArea.setFont(font);

break;

default:

break;

}

}});

menu3.add(menu3_1[i]);

}

JMenu menu4 = new JMenu("字號");

String []fontBig = {"10","15","20","25","30","35","40"};

final JMenuItem []menu4_1 = new JMenuItem[fontBig.length];

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

String changeString = "";

menu4_1[i] = new JMenuItem(fontBig[i]);

menu4_1[i].setActionCommand(changeString + i);

menu4_1[i].addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method

switch (e.getActionCommand().charAt(0)) {

case '0':

font = new Font(menu3.getText(),Font.PLAIN,10);

textArea.setFont(font);

break;

case '1':

font = new Font(menu3.getText(),Font.PLAIN,15);

textArea.setFont(font);

break;

case '2':

font = new Font(menu3.getText(),Font.PLAIN,20);

textArea.setFont(font);

break;

case '3':

font = new Font(menu3.getText(),Font.PLAIN,25);

textArea.setFont(font);

break;

case '4':

font = new Font(menu3.getText(),Font.PLAIN,30);

textArea.setFont(font);

break;

case '5':

font = new Font(menu3.getText(),Font.PLAIN,35);

textArea.setFont(font);

break;

case '6':

font = new Font(menu3.getText(),Font.PLAIN,40);

textArea.setFont(font);

break;

default:

break;

}

}});

menu4.add(menu4_1[i]);

}

menu2.add(menu3);

menu2.add(menu4);

menuBar.add(menu);

menuBar.add(menu2);

setJMenuBar(menuBar);

textArea = new JTextArea();

textArea.setVisible(true);

// textArea.setFont(new Font("宋體",Font.BOLD,20));

add(textArea);

JScrollPane scrollPane = new JScrollPane(textArea);

scrollPane.setSize(600, 410);

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

add(scrollPane);

}

public void readFromFile(File f) throws IOException{

FileReader fReader = new FileReader(file);

BufferedReader bReader = new BufferedReader(fReader);

char []data = new char[10000];

int length = 0;

while((length = fReader.read(data)) 0){

string += new String(data,0,length);

}

// string = new String("");

// while(true){

// String s1 = bReader.readLine();

// if(s1 == null){

// break;

// }

// string += (s1 + "\n");

bReader.close();

fReader.close();

}

public void writeToFile(File file,String string) throws IOException{

if(!file.exists()){

file.createNewFile();

}

FileWriter fWriter = new FileWriter(file);

fWriter.write(string);

fWriter.close();

}

public static void main(String[] args) {

WordProcessSystem wordProcessSystem = new WordProcessSystem();

wordProcessSystem.setVisible(true);

}

}

java編譯文本編輯器

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class EditorJFrame extends JFrame implements ActionListener, ItemListener, MouseListener

{

private JTextField text_size; //字號文本行

private JCheckBox checkbox_bold, checkbox_italic; //粗體、斜體復(fù)選框

private JButton button_cut, button_copy, button_paste; //剪切、復(fù)制、粘貼按鈕

private JTextArea textarea; //文本區(qū)

private JPopupMenu popupmenu; //快捷菜單

private JDialog dialog; //出錯提示對話框

private JLabel label_dialog; //對話框中的標(biāo)簽

public EditorJFrame()

{

super("文本編輯器"); //默認(rèn)BorderLayout布局

this.setSize(500,300);

this.setLocation(300,240);

this.setDefaultCloseOperation(EXIT_ON_CLOSE); //單擊窗口關(guān)閉按鈕時(shí),結(jié)束程序運(yùn)行

textarea = new JTextArea("TextArea");

textarea.addMouseListener(this); //為文本區(qū)注冊鼠標(biāo)事件監(jiān)聽器

this.add(textarea); //文本區(qū)添加到框架的中部

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //面板為流布局,左對齊

this.add(panel,"North"); //面板添加到框架的北部

text_size = new JTextField("12",10);

panel.add(text_size);

text_size.addActionListener(this); //注冊文本行的單擊事件監(jiān)聽器

checkbox_bold = new JCheckBox("粗體"); //復(fù)選框

panel.add(checkbox_bold);

checkbox_bold.addItemListener(this); //注冊復(fù)選框的選擇事件監(jiān)聽器

checkbox_italic = new JCheckBox("斜體");

panel.add(checkbox_italic);

checkbox_italic.addItemListener(this);

this.addmyMenu(); //調(diào)用自定義方法,添加菜單

this.setVisible(true);

}

private void addmyMenu() //添加主菜單、快捷菜單、對話框

{

JMenuBar menubar = new JMenuBar(); //菜單欄

this.setJMenuBar(menubar); //框架上添加菜單欄

JMenu menu_file = new JMenu("文件"); //菜單

menubar.add(menu_file); //菜單欄中加入菜單

menu_file.add(new JMenuItem("打開")); //生成菜單項(xiàng)并加入到菜單

menu_file.add(new JMenuItem("保存"));

menu_file.addSeparator(); //加分隔線

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

menu_file.add(menuitem_exit);

menuitem_exit.addActionListener(this); //為菜單項(xiàng)注冊單擊事件監(jiān)聽器

JMenu menu_edit = new JMenu("編輯");

menubar.add(menu_edit);

JMenu menu_style = new JMenu("字形");

menu_style.add(new JCheckBoxMenuItem("粗體")); //復(fù)選菜單項(xiàng)

menu_style.add(new JCheckBoxMenuItem("斜體"));

menu_edit.add(menu_style); //菜單加入到菜單中成為二級菜單

JMenu menu_color = new JMenu("顏色");

menu_edit.add(menu_color);

ButtonGroup buttongroup = new ButtonGroup(); //按鈕組

JRadioButtonMenuItem rbmi_red = new JRadioButtonMenuItem("紅",true); //單選菜單項(xiàng)

buttongroup.add(rbmi_red); //單選菜單項(xiàng)添加到按鈕組

menu_color.add(rbmi_red); //單選菜單項(xiàng)添加到菜單

JRadioButtonMenuItem rbmi_green = new JRadioButtonMenuItem("綠",true);

buttongroup.add(rbmi_green);

menu_color.add(rbmi_green);

JRadioButtonMenuItem rbmi_blue = new JRadioButtonMenuItem("藍(lán)",true);

buttongroup.add(rbmi_blue);

menu_color.add(rbmi_blue);

menubar.add(new JMenu("幫助"));

popupmenu = new JPopupMenu(); //彈出式菜單對象

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

menuitem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));//設(shè)置快捷鍵Ctrl+X

popupmenu.add(menuitem_cut); //加入剪切菜單項(xiàng)

menuitem_cut.addActionListener(this);

JMenuItem menuitem_copy = new JMenuItem("復(fù)制");

menuitem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));//設(shè)置快捷鍵Ctrl+C

popupmenu.add(menuitem_copy);

menuitem_copy.addActionListener(this);

JMenuItem menuitem_paste = new JMenuItem("粘貼");

menuitem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));//設(shè)置快捷鍵Ctrl+V

popupmenu.add(menuitem_paste);

menuitem_paste.addActionListener(this);

textarea.add(popupmenu); //文本區(qū)添加快捷菜單

dialog = new JDialog(this,"提示");

dialog.setSize(240,80);

label_dialog = new JLabel("",JLabel.CENTER);

dialog.add(label_dialog);

dialog.setDefaultCloseOperation(HIDE_ON_CLOSE); //單擊對話框的關(guān)閉按鈕時(shí),隱藏對話框而不結(jié)束程序運(yùn)行

}

public void actionPerformed(ActionEvent e) //單擊事件處理程序

{

if(e.getActionCommand()=="退出") //不能用switch(int)語句

System.exit(0); //單擊菜單項(xiàng)時(shí)結(jié)束程序

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

textarea.cut(); //將選中文本剪切送系統(tǒng)剪貼板

if(e.getActionCommand()=="復(fù)制")

textarea.copy();

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

textarea.paste();

if(e.getSource()==text_size) //單擊文本行時(shí),改變字號

{

int size=0;

try

{

size = Integer.parseInt(text_size.getText());

if (size=0 || size72)

throw new Exception("SizeException"); //拋出異常對象

java.awt.Font font = textarea.getFont();

textarea.setFont(new Font(font.getName(),font.getStyle(),size));

}

catch(NumberFormatException nfe)

{

label_dialog.setText("\""+text_size.getText()+"\" 不能轉(zhuǎn)換成整數(shù),請重新輸入!");

dialog.setLocation(this.getX()+100,this.getY()+100);

dialog.setVisible(true);

}

catch(Exception ex)

{

if (ex.getMessage()=="SizeException") //捕獲自己拋出的異常對象

{

label_dialog.setText(size+" 字號不合適,請重新輸入!");

dialog.setLocation(this.getX()+100,this.getY()+100);

dialog.setVisible(true);

}

}

finally{}

}

}

public void itemStateChanged(ItemEvent e) //復(fù)選框選擇事件處理程序

{ //實(shí)現(xiàn)ItemListener接口中的方法

Font font = textarea.getFont();

int style = font.getStyle();

if (e.getSource()==checkbox_bold)

style = style ^ 1; //整數(shù)的位運(yùn)算,異或^

if (e.getSource()==checkbox_italic)

style = style ^ 2;

textarea.setFont(new Font(font.getName(),style,font.getSize()));

}

public void mouseClicked(MouseEvent mec) //單擊鼠標(biāo)時(shí)觸發(fā)

{ //實(shí)現(xiàn)MouseListener接口中的方法

if (mec.getModifiers()==mec.BUTTON3_MASK) //單擊的是鼠標(biāo)右鍵

popupmenu.show(textarea,mec.getX(),mec.getY());//在鼠標(biāo)單擊處顯示快捷菜單

}

public void mousePressed(MouseEvent mep) { }

public void mouseReleased(MouseEvent mer) { }

public void mouseEntered(MouseEvent mee) { }

public void mouseExited(MouseEvent mex) { }

public void mouseDragged(MouseEvent med) { }

public static void main(String arg[])

{

new EditorJFrame();

}

}

如何用java編寫一個簡單的文本編輯器?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

public class f1 extends Frame implements ActionListener

{

private MenuBar menubar=new MenuBar();

private Menu filemenu=new Menu("文件");

private Menu editmenu=new Menu("編輯");

private Menu formmenu=new Menu("格式");

private MenuItem[] itemf=new MenuItem[4];

private MenuItem[] iteme=new MenuItem[6];

private MenuItem[] items=new MenuItem[2];

private TextArea tf=new TextArea();

public int a=0,b=0,c=0,style=Font.PLAIN,size=15;

public String s1="red:"+a+" "+"green:"+b+" "+"blue"+c,

s2="宋體";

public String[] sz1={"10","16","24","30","32","36"},

sz2={"宋體","黑體","幼圓","隸書","行楷","Arial","Georgia"},

sz3={"粗體","傾斜","常規(guī)","粗斜"};

JDialog dialog=new JDialog(this,"字體",true);

Container cp=dialog.getContentPane();

JLabel[] lb=new JLabel[8];

JLabel lb1=new JLabel(s1,JLabel.LEFT);

JButton b1=new JButton("確定"),

b2=new JButton("取消");

JComboBox jc1=new JComboBox(),

jc2=new JComboBox(),

jc3=new JComboBox();

JScrollBar jb1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

JScrollBar jb2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

JScrollBar jb3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);


本文題目:java代碼文本編輯器,基于java的文本編輯器設(shè)計(jì)與實(shí)現(xiàn)
分享網(wǎng)址:http://weahome.cn/article/dseepho.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部