導航:首頁 > 源碼編譯 > 用java寫一個簡易文本編譯器

用java寫一個簡易文本編譯器

發布時間:2022-03-05 15:17:41

『壹』 用java編寫一個圖形應用程序,可以是一個簡單的文本編輯器、計算器等等。 求完整代碼

//網路文庫找的,可費了我的財富值了,你可要把分給我呀。
package lee;
/*文件名:Calculator.java

*說明:簡易科學計算器
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends Frame implements ActionListener, WindowListener
{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField; //計算結果顯示區
private String lastCommand; //保存+,-,*,/,=命令0
private double result; //保存計算結果
private boolean start; //判斷是否為數字的開始
private JMenuBar menubar;
private JMenuItem m_exit,m2_ejz,m2_bjz;
private Dialog dialog;
private Label label_dialog;
private JButton button_sqrt,button_plusminus,button_CE,button_cancel,button_1,button_2,
button_3,button_4,button_5,button_6,button_7,button_8,button_9,button_0,
button_plus,button_minus,button_multiply,button_divide,button_point,
button_equal,button_log,button_tan,button_cos,button_sin,button_exp;

public Calculator() //構造方法設置布局、為按鈕注冊事件監聽器
{
super("Calculator");
this.setLocation(240,200);
this.setSize(350,300);
this.setResizable(true);
this.setLayout(new GridLayout(7,1));
this.addmyMenu(); //調用成員方法添加菜單
displayField=new JTextField(30);
this.add(displayField);
displayField.setEditable(true);

start=true;
result=0;
lastCommand = "=";

JPanel panel0=new JPanel();
panel0.setLayout(new GridLayout(1,4,4,4));

JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(1,5,4,4));
this.add(panel1);
button_sqrt=new JButton("sqrt");
button_plusminus=new JButton("+/-");
button_exp=new JButton("exp");
button_CE=new JButton("退格");
button_cancel=new JButton("C");

JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,5,4,4));
this.add(panel2);
button_7=new JButton("7");
button_8=new JButton("8");
button_9=new JButton("9");
button_log=new JButton("log");
button_divide=new JButton("/");

JPanel panel3=new JPanel();
panel3.setLayout(new GridLayout(1,5,4,4));
this.add(panel3);
button_4=new JButton("4");
button_5=new JButton("5");
button_6=new JButton("6");
button_tan=new JButton("tan");
button_multiply=new JButton("*");

JPanel panel4=new JPanel();
panel4.setLayout(new GridLayout(1,5,4,4));
this.add(panel4);
button_1=new JButton("1");
button_2=new JButton("2");
button_3=new JButton("3");
button_cos=new JButton("cos");
button_minus=new JButton("-");

JPanel panel5=new JPanel();
panel5.setLayout(new GridLayout(1,5,4,4));
this.add(panel5);
button_0=new JButton("0");
button_point=new JButton(".");
button_equal=new JButton("=");
button_sin=new JButton("sin");
button_plus=new JButton("+");

panel1.add(button_sqrt);
panel1.add(button_plusminus);
panel1.add(button_exp);
panel1.add(button_CE);
panel1.add(button_cancel);
panel2.add(button_7);
panel2.add(button_8);
panel2.add(button_9);
panel2.add(button_log);
panel2.add(button_divide);
panel3.add(button_4);
panel3.add(button_5);
panel3.add(button_6);
panel3.add(button_tan);
panel3.add(button_multiply);
panel4.add(button_1);
panel4.add(button_2);
panel4.add(button_3);
panel4.add(button_cos);
panel4.add(button_minus);
panel5.add(button_0);
panel5.add(button_point);
panel5.add(button_equal);
panel5.add(button_sin);
panel5.add(button_plus);

button_sqrt.addActionListener(this);
button_plusminus.addActionListener(this);
button_exp.addActionListener(this);
button_CE.addActionListener(this);
button_cancel.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
button_9.addActionListener(this);
button_log.addActionListener(this);
button_divide.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_tan.addActionListener(this);
button_multiply.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_cos.addActionListener(this);
button_minus.addActionListener(this);
button_0.addActionListener(this);
button_point.addActionListener(this);
button_equal.addActionListener(this);
button_sin.addActionListener(this);
button_plus.addActionListener(this);

this.addWindowListener(new WinClose()); //注冊窗口監聽器
this.setVisible(true);
}

private void addmyMenu() //菜單的添加
{
JMenuBar menubar=new JMenuBar();
this.add(menubar);
JMenu m1=new JMenu("選項");
JMenu m2=new JMenu("進制轉換");
JMenuItem m1_exit=new JMenuItem("退出");
m1_exit.addActionListener(this);
JMenuItem m2_ejz=new JMenuItem("二進制");
m2_ejz.addActionListener(this);
JMenuItem m2_bjz=new JMenuItem("八進制");
m2_bjz.addActionListener(this);
JMenu m3 = new JMenu(" 幫助");
JMenuItem m3_Help = new JMenuItem("用法");
m3_Help.addActionListener(this);

dialog = new Dialog(this,"提示",true); //模式窗口
dialog.setSize(240,80);
label_dialog = new Label("",Label.CENTER); //標簽的字元串為空,居中對齊
dialog.add(label_dialog);
dialog.addWindowListener(this); //為對話框注冊窗口事件監聽器

m1.add(m1_exit);
menubar.add(m1);
m2.add(m2_ejz);
m2.add(m2_bjz);
menubar.add(m2);
m3.add(m3_Help);
menubar.add(m3); }

public void actionPerformed(ActionEvent e) //按鈕的單擊事件處理方法
{
if(e.getSource().equals(button_1)||e.getSource().equals(button_2)||
e.getSource().equals(button_3)||e.getSource().equals(button_4)||
e.getSource().equals(button_5)|| e.getSource().equals(button_6)||
e.getSource().equals(button_7)|| e.getSource().equals(button_8)||
e.getSource().equals(button_9) ||e.getSource().equals(button_0)||
e.getSource().equals(button_point)||e.getSource().equals(button_plusminus)||
e.getSource().equals(button_cancel)||e.getSource().equals(button_CE))
{ //非運算符的處理方法
String input=e.getActionCommand();

if (start)
{
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}
if(!input.equals("+/-"))
{
String str=displayField.getText();
if(input.equals("退格")) //退格鍵的實現方法
{
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}
else if(input.equals("C")) //清零鍵的實現方法
{
displayField.setText("0");
start=true;
}
else
displayField.setText(displayField.getText()+input);
}
}
else if (e.getActionCommand()=="二進制") //二進制的轉換
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toBinaryString(n));
}
else if (e.getActionCommand()=="八進制") //八進制的轉換
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toOctalString(n));
}
else if (e.getActionCommand()=="退出") //選項中退出的處理方法
{
System.exit(0);
}
else if (e.getActionCommand()=="用法") //按下'幫助'菜單欄中用法的處理方法
{
label_dialog.setText("sqrt,exp等鍵是先輸運算符再輸數字\n");
dialog.setLocation(400,250);
dialog.setVisible(true);
}
else //各運算符的識別
{
String command=e.getActionCommand();
if(start)
{
lastCommand=command;
}
else
{
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}

public void calculate(double x) //各運算符的具體運算方法
{
double d=0;
if (lastCommand.equals("+"))
result+= x;
else if (lastCommand.equals("-"))
result-=x;
else if (lastCommand.equals("*"))
result*=x;
else if (lastCommand.equals("/"))
result/=x;
else if (lastCommand.equals("="))
result=x;
else if (lastCommand.equals("sqrt"))
{
d=Math.sqrt(x);
result=d;
}
else if (lastCommand.equals("exp"))
{
d=Math.exp(x);
result=d;
}
else if (lastCommand.equals("log"))
{
d=Math.log(x);
result=d;
}
else if (lastCommand.equals("tan"))
{
d=Math.tan(x);
result=d;
}
else if (lastCommand.equals("cos"))
{
d=Math.cos(x);
result=d;
}
else if (lastCommand.equals("sin"))
{
d=Math.sin(x);
result=d;
}
displayField.setText(""+ result);
}

public void windowClosing(WindowEvent e)
{
if(e.getSource()==dialog)
dialog.setVisible(false); //隱藏對話框
else
System.exit(0);
}

public void windowOpened(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }

public static void main(String args[])
{
Calculator calculator=new Calculator();
}
}

class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e) //單擊窗口關閉按鈕時觸發並執行實現窗口監聽器介面中的方法
{
System.exit(0); //結束程序運行
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}

『貳』 能編譯運行java的簡單文本編輯器 求代碼

package Notepad;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
@SuppressWarnings("serial")
public class Notepad extends JFrame
{
private JTextArea editor;
private Container c;
private Font f=new Font("sanserif",Font.PLAIN,12);
//菜單欄成員屬性聲明
private JMenuBar mb; //菜單棒
private JMenu Menu; //文件菜單
private JMenuItem New,Open,Save,Close; //文件菜單的菜單項
//工具欄成員屬性聲明
private JToolBar toolBar; //工具棒
private JButton New1,Open1,Save1,About; //聲明4個按鈕,分別為:「新建」、「打開」、「保存」、「關於」

//彈出式菜單屬性聲明
private JPopupMenu pm;
private JMenuItem item1,item2,item3,item4;

public Notepad()
{
super("文本編輯器");
setSize(800,600);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){ System.err.println("不能被設置外觀的原因:"+e);}

c=getContentPane(); //創建一個內容面板
editor = new JTextArea(); //創建一個文本區
c.add(new JScrollPane(editor)); //設置滾動條,並添加到內容面板
//菜單欄的實現
//文件菜單的實現
mb = new JMenuBar(); //創建菜單棒
Menu = new JMenu("菜單(M)");//創建菜單
New = new JMenuItem("新建(N)...Ctrl+N");
Open = new JMenuItem("打開(O)...Ctrl+O");
Save = new JMenuItem("保存(S)...Ctrl+S");
Close = new JMenuItem("退出");
JMHandler JM=new JMHandler(); //創建監聽器
New.addActionListener(JM);//注冊監聽器
Open.addActionListener(JM);
Save.addActionListener(JM);
Close.addActionListener(JM);
Menu.add(New);
Menu.add(Open);
Menu.add(Save);
Menu.addSeparator(); //添加分隔線
Menu.add(Close);
Menu.setFont(f); //設置菜單中文體的字體

//將菜單全部添加菜單棒里
mb.add(Menu);

//工具欄的實現
//按鈕分別為:「新建」、「打開」、「保存」、「關於」
toolBar =new JToolBar(); //創建工具棒
New1= new JButton(new ImageIcon("img/New.png"));
Open1= new JButton(new ImageIcon("img/Open.png"));
Save1= new JButton(new ImageIcon("img/Save.png"));
About= new JButton(new ImageIcon("img/About.png"));
TBHandler TB=new TBHandler(); //創建按鈕監聽器
New1.addActionListener(TB); Open1.addActionListener(TB);
Save1.addActionListener(TB); About.addActionListener(TB);
//把按鈕全部添加到工具棒中
toolBar.add(New1); toolBar.add(Open1);
toolBar.add(Save1); toolBar.add(About);

//彈出菜單的實現
pm =new JPopupMenu(); //創建彈出菜單
item1 = new JMenuItem("新建");
item2 = new JMenuItem("打開");
item3 = new JMenuItem("保存");
item4 = new JMenuItem("關於");
JPHandler JP=new JPHandler();
item1.addActionListener(JP); //注冊菜單項的滑鼠事件監聽器
item2.addActionListener(JP);
item3.addActionListener(JP);
item4.addActionListener(JP);
editor.addMouseListener(JP); //注冊文本區的滑鼠事件監聽器
pm.add(item1); pm.add(item2);
pm.add(item3); pm.add(item4);

//把菜單欄、工具欄、彈出菜單添加到內容面板
setJMenuBar(mb); //顯示菜單欄
c.add(toolBar,BorderLayout.NORTH);

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//自定義類實現文件菜單項的事件處理
private class JMHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==New){ creatFile(); }
else if(e.getSource()==Open){ loadFile(); }
else if(e.getSource()==Save){ saveFile(); }
else{System.exit(0);}
}
}
private void creatFile() {//新建文件方法
editor.setText(null);
}
public void loadFile() //打開文件方法
{
JFileChooser fc=new JFileChooser();
int r=fc.showOpenDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
File file=fc.getSelectedFile();
try{ editor.read(new FileReader(file),null);}
catch(IOException e){}
}
}

public void saveFile() //保存文件的方法
{
JFileChooser fc=new JFileChooser();
int r=fc.showSaveDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
File file=fc.getSelectedFile();
try{ editor.write(new FileWriter(file));}
catch(IOException e){}
}
}

//自定義實現查找事件處理
//自定義類實現工具欄的按鈕事件處理
private class TBHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==New1){ creatFile(); } //實現打開文件功能
else if(e.getSource()==Open1) {loadFile();}
else if(e.getSource()==Save1) {saveFile();} //實現保存文件功能
else {
editor.();
editor.requestFocus();
}
}
}

//自定義類實現彈出式菜單的事件處理
private class JPHandler implements ActionListener,MouseListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==item1){ creatFile(); } //實現創建文件功能
else if(e.getSource()==item2){ loadFile(); } //實現打開文件功能
else if(e.getSource()==item3) {saveFile();} //實現保存文件功能
else if(e.getSource()==item4) //關於文件
{
}
}
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger()) //判斷是否按下滑鼠右鍵
pm.show(editor,e.getX(),e.getY()); //顯示彈出式菜單
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
}

public static void main(String []args)
{
Notepad N=new Notepad();
}

}

『叄』 能編譯運行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("文字處理系統");
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.*;
import java.io.*;
public class MyTextEditor extends JFrame implements ActionListener,ItemListener,MouseListener
{
private File file;
private JTextArea textarea;
private JRadioButtonMenuItem rbmi_red,rbmi_blue,rbmi_green;
private JMenuItem menuitem_,menuitem_cut,menuitem_paste,menuitem_seek;
private JMenuItem menuitem_song,menuitem_fang,menuitem_kai;//字體變數
private JMenuItem menuitem_normal,menuitem_bold,menuitem_italic;//字形變數
private JMenuItem menuitem_20,menuitem_30,menuitem_40;//字型大小變數
private JMenuItem menuitem_exit,menuitem_infor;
private JPopupMenu popupmenu;
private JMenuItem menuitem_red,menuitem_green,menuitem_blue;
private JDialog dialog,dialog1;
private JButton button_seek;
private JTextField textfield_seek;
private JLabel label_seek,label_infor;
String seek;

public MyTextEditor()
{
super("文本編輯器");
this.setSize(400,300);
this.setLocation(360,300);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);

Container ss=this.getContentPane();
this.textarea = new JTextArea();
JScrollPane dd=new JScrollPane(textarea);
ss.add(dd);
textarea.addMouseListener(this);
this.addMenu();
this.setVisible(true);
this.Dialog();
this.Dialog1();

this.file = null;
}

public MyTextEditor(String filename)
{
this();
if (filename!=null)
{
this.file = new File(filename);
this.setTitle(filename);
this.textarea.setText(this.readFromFile());
}
}

public MyTextEditor(File file)
{
this();
if (file!=null)
{
this.file = file;
this.setTitle(this.file.getName());
this.textarea.setText(this.readFromFile());
}
}

public void Dialog() //建立對話框的方法
{
dialog=new JDialog(this,"查找",true);
dialog.setLayout(new FlowLayout());
dialog.setSize(200,90);

label_seek=new JLabel("查找內容");
dialog.add(label_seek);

textfield_seek=new JTextField(10);
dialog.add(textfield_seek);

button_seek=new JButton("查找");
dialog.add(button_seek);
button_seek.addActionListener(this);

}
public void Dialog1()
{

dialog1=new JDialog(this,"專利",true);
dialog1.setLayout(new FlowLayout());
dialog1.setSize(200,100);

label_infor=new JLabel("劉同虎製作");
dialog1.add(label_infor);
}

public void addMenu()
{
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);

JMenu menu_file = new JMenu("文件"); //文件菜單
menubar.add(menu_file);
JMenuItem menuitem_open = new JMenuItem("打開");
menu_file.add(menuitem_open);
menuitem_open.addActionListener(this);

JMenuItem menuitem_save = new JMenuItem("保存");
menu_file.add(menuitem_save);
menuitem_save.addActionListener(this);

JMenuItem menuitem_saveas = new JMenuItem("另存為");
menu_file.add(menuitem_saveas);
menuitem_saveas.addActionListener(this);

menuitem_exit=new JMenuItem("退出" );
menu_file.add(menuitem_exit);
menuitem_exit.addActionListener(this);

menuitem_infor=new JMenuItem("信息");
menu_file.add(menuitem_infor);
menuitem_infor.addActionListener(this);

JMenu menu_editor=new JMenu("編輯");//編輯菜單
menubar.add(menu_editor);

menuitem_seek=new JMenuItem("查找");
menu_editor.add(menuitem_seek);
menuitem_seek.addActionListener(this);

menuitem_=new JMenuItem("復制");
menuitem_.addActionListener(this);
menu_editor.add(menuitem_);

menuitem_cut=new JMenuItem("剪切");
menu_editor.add(menuitem_cut);
menuitem_cut.addActionListener(this);

menuitem_paste=new JMenuItem("粘貼");
menu_editor.add(menuitem_paste);
menuitem_paste.addActionListener(this);

JMenuItem menu_color=new JMenu("顏色");//顏色菜單
menu_editor.add(menu_color);

ButtonGroup buttongroup=new ButtonGroup();
rbmi_red=new JRadioButtonMenuItem("紅",true);
buttongroup.add(rbmi_red);
menu_color.add(rbmi_red);
rbmi_red.addItemListener(this);

rbmi_blue=new JRadioButtonMenuItem("藍",true);
buttongroup.add(rbmi_blue);
menu_color.add(rbmi_blue);
rbmi_blue.addItemListener(this);

rbmi_green=new JRadioButtonMenuItem("綠",true);
buttongroup.add(rbmi_green);
menu_color.add(rbmi_green);
rbmi_green.addItemListener(this);

JMenu menu_font=new JMenu("設置字體");//設置字體菜單
menubar.add(menu_font);

menuitem_song=new JMenuItem("宋體");
menu_font.add(menuitem_song);
menuitem_song.addActionListener(this);

menuitem_fang=new JMenuItem("仿宋");
menu_font.add(menuitem_fang);
menuitem_fang.addActionListener(this);

menuitem_kai=new JMenuItem("楷體");
menu_font.add(menuitem_kai);
menuitem_kai.addActionListener(this);

JMenu menu_style=new JMenu("設置字形");//設置字形菜單
menubar.add(menu_style);

menuitem_bold=new JMenuItem("粗體");
menu_style.add(menuitem_bold);
menuitem_bold.addActionListener(this);

menuitem_italic=new JMenuItem("斜體");
menu_style.add(menuitem_italic);
menuitem_italic.addActionListener(this);

JMenu menu_size=new JMenu("設置字型大小"); //設置字型大小菜單
menubar.add(menu_size);

menuitem_20=new JMenuItem("20");
menu_size.add(menuitem_20);
menuitem_20.addActionListener(this);

menuitem_30=new JMenuItem("30");
menu_size.add(menuitem_30);
menuitem_30.addActionListener(this);

menuitem_40=new JMenuItem("40");
menu_size.add(menuitem_40);
menuitem_40.addActionListener(this);

popupmenu=new JPopupMenu(); //快捷菜單
JMenuItem menuitem_red=new JMenuItem("紅色");
popupmenu.add(menuitem_red);
menuitem_red.addActionListener(this);

JMenuItem menuitem_green=new JMenuItem("綠色");
popupmenu.add(menuitem_green);
menuitem_green.addActionListener(this);

menuitem_blue=new JMenuItem("藍色");
popupmenu.add(menuitem_blue);
menuitem_blue.addActionListener(this);

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

}

public void writeToFile(String lines) //寫文件
{
try
{
FileWriter fout = new FileWriter(this.file);
fout.write(lines+"\r\n");
fout.close();
}
catch (IOException ioex)
{
return;
}
}

public String readFromFile() //讀文件
{
try
{
FileReader fin = new FileReader(this.file);
BufferedReader bin = new BufferedReader(fin);
String aline="", lines="";
do
{
aline = bin.readLine();
if (aline!=null)
lines += aline + "\r\n";
} while (aline!=null);

bin.close();
fin.close();
return lines;
}
catch (IOException ioex)
{
return null;
}
}

『伍』 用JAVA寫一個文本編輯器,要用到設計模式,急!

10分,還要一個完整帶文檔的,你當網路的這破分多值錢啊。

『陸』 如何用java寫一個文本編輯器

JDK自帶一個簡單的Notepad,你可以研究一下他的源代碼。
在%JAVA_HOME%\demo\jfc\Notepad 目錄下
JDK自帶一個稍復雜的Stylepad,你可以研究一下他的源代碼。
在%JAVA_HOME%\demo\jfc\Stylepad目錄下

『柒』 在Java中編寫文本編輯器

http://www.pudn.com/downloads40/sourcecode/mobile/j2me/detail140240.html

『捌』 用JAVA設計一個簡單文本編輯器

要求挺多

『玖』 如何用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={"粗體","傾斜","常規","粗斜"};

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寫一個簡易文本編譯器相關的資料

熱點內容
命令方塊指令冰封劍 瀏覽:784
android中so文件 瀏覽:276
手工用氣球做的捏捏樂解壓神器 瀏覽:196
app升級後就閃退怎麼辦 瀏覽:35
手錶上的樂塗app怎麼下載 瀏覽:721
程序員身上的六宗罪是什麼 瀏覽:145
游戲編程精粹6 瀏覽:69
修復ie的命令 瀏覽:602
linux伺服器怎麼查看地址 瀏覽:65
底部異地持倉源碼 瀏覽:105
加密應用手機 瀏覽:798
程序員考試考什麼科目 瀏覽:485
程序員必備文檔編輯 瀏覽:960
踩水果解壓大全 瀏覽:634
什麼是dk伺服器在 瀏覽:461
nusoapphp下載 瀏覽:929
黑莓原生解壓rar 瀏覽:956
百度解壓縮在哪 瀏覽:788
硬解壓卡怎麼用 瀏覽:183
新買的聯想伺服器怎麼配置 瀏覽:757