⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 文本编辑器.java

📁 Java 图形界面入门实例
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
{ 
super(title); 

Font font= new Font("TimesRoman",Font.BOLD,20); 
setFont(font); 
setBackground(Color.lightGray); 
horz= new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,0); 
vert= new Scrollbar(Scrollbar.VERTICAL,0,0,0,0); 

// show(); 
addMenu(); 
init(); 
Lshow= new W_quick(this,this,0,0); 

openDialog= new FileDialog(this,"Open File...",FileDialog.LOAD); 
saveDialog= new FileDialog(this,"Save File...",FileDialog.SAVE); 
} 

private void init() 
{ 
setLayout(new BorderLayout()); 
File_panel= new Panel(); 
File_panel.setBackground(Color.lightGray); 
Panel pp= new Panel(); 
pp.setLayout(new BorderLayout(2,2)); 
current_t= new P_area(this,Ffont,Fcolor,"aa","aa"); 
pp.add("Center",current_t); 
pp.add("South",horz); 
pp.add("East",vert); 

add("South",File_panel); 
add("Center",pp); 

Dimension screen_size= Toolkit.getDefaultToolkit().getScreenSize(); 
screenw=screen_size.width; 
screenh= screen_size.height-16; 
reshape(16,16,screen_size.width-32,screen_size.height-32); 
show(); 
} 

private void addMenu() 
{ 
menuBar= new MenuBar(); 
setMenuBar(menuBar); 

Menu menu= new Menu("File"); 
Menu menu1= new Menu("Edit"); 
Menu menu2= new Menu("Option"); 
menuBar.add(menu); 
menuBar.add(menu1); 
menuBar.add(menu2); 

MenuItem fitem= new MenuItem("New"); 
menu.add(fitem); 
MenuItem fitem1= new MenuItem("Open"); 
menu.add(fitem1); 
MenuItem fitem2= new MenuItem("Close"); 
menu.add(fitem2); 
menu.addSeparator(); 
MenuItem fitem3= new MenuItem("Save"); 
menu.add(fitem3); 
MenuItem fitem4= new MenuItem("Save As"); 
menu.add(fitem4); 
menu.addSeparator(); 
MenuItem fitem5= new MenuItem("Quit"); 
menu.add(fitem5); 



menu1.add(new MenuItem("Undo")); 
menu1.add(new MenuItem("Redo")); 
menu1.addSeparator(); 
eitem= new MenuItem("Cut"); 
menu1.add(eitem); 
eitem.enable(false); 
eitem1= new MenuItem("Copy"); 
eitem1.enable(false); 
menu1.add(eitem1); 
eitem2= new MenuItem("Paste"); 
menu1.add(eitem2); 
eitem2.enable(false); 
menu1.addSeparator(); 
eitem3= new MenuItem("Delete"); 
menu1.add(eitem3); 
eitem3.enable(false); 
MenuItem eitem6= new MenuItem("Bookmark"); 
menu1.add(eitem6); 
menu1.addSeparator(); 
MenuItem eitem4= new MenuItem("Find"); 
menu1.add(eitem4); 
MenuItem eitem5= new MenuItem("Repace"); 
menu1.add(eitem5); 

MenuItem oitem= new MenuItem("Color"); 
menu2.add(oitem); 
MenuItem oitem1= new MenuItem("Font"); 
menu2.add(oitem1); 

} 

public void setQuit() 
{ 
this.dispose(); 
System.exit(0); 
} 

public void setfont(Font f) 
{ 
Ffont= f; 
current_t.setFont(f); 
repaint(); 
} 

public void setcolor(Color c) 
{ 
Fcolor= c; 
current_t.setColor(c); 
repaint(); 
} 

public void paint(Graphics g) 
{ 
if(Ffont!= null) 
g.setFont(Ffont); 

} 

public void read(String fn,String fd) 
{ 
current_t.setname(fn,fd); 
String line= null; 
String input= null; 
File s= new File(fd,fn); 
FileInputStream in= null; 
DataInputStream datain= null; 

try 
{ 
in= new FileInputStream(s); 
datain = new DataInputStream(in); 
}catch(Exception e) 
{ 
System.out.println("Unable to open file"); 
return; 
} 

try{ 
while((line=datain.readLine())!= null) 
{ 
line+= "\n"; 
if(input== null) 
input= line; 
else 
input+= line; 
} 
}catch(IOException e){ 
System.out.println("Error in reading file"); 
} 
current_t.open(input); 

try{ 
in.close(); 
}catch(IOException e){ 
System.out.println("e"); 
} 
} 

public void write(String fn,String fd) 
{ 
FileOutputStream out= null; 
File s= new File(fd,fn); 

try{ 
out= new FileOutputStream(s); 
}catch(Exception e) 
{ 
System.out.println("Unable to open file"); 
return; 
} 
PrintStream psOut=new PrintStream(out); 
psOut.print(current_t.getText());/// 
try{ 
out.close(); 
}catch(IOException e){ 
System.out.println("e"); 
} 
} 

public boolean keyDown(Event evt,int key) 
{ 
System.out.println(key); 
switch(key) 
{ 
case 1004://up 
current_t.keycusor(0,-1); 
return true; 
case 1005://down 
current_t.keycusor(0,1); 
return true; 
case 1006: 
current_t.keycusor(-1,0); 
return true; 
case 1007: 
current_t.keycusor(1,0); 
return true; 
case 8: 
current_t.del(-1); 
return true; 
case 127: 
current_t.del(1); 
return true; 
case 1001://end 
current_t.end(); 
return true; 

case 1000://home 
current_t.home(); 
return true; 
case 1002: 
current_t.pageup(); 
return true; 
case 1003: 
current_t.pagedown(); 
return true; 
} 
// if(!current_t.border) 
// current_t.addchar((char)evt.key); 
// else 
current_t.add((char)evt.key); 
return true; 
} 

public boolean mouseDown(Event evt,int x,int y) 
{ 
//****单击右键*******************不完整 
eitem.enable(false); 
eitem1.enable(false); 
eitem3.enable(false); 
current_t.bfind= false; 

if((evt.modifiers&Event.META_MASK)>0) 
{ 
Lshow.show(evt.x,evt.y); 
return false; 
}else 
{ 
Lshow.hide(); 
begin= true; 
current_t.movecusor(evt.x,evt.y); 
if(current_t.selold) 
{ 
current_t.selold= false; 
current_t.selrep(); 
} 
} 
if(end) 
{ 
// current_t.rep(); 
// current_t.paint =true; 
end= false; 
} 
return false; 
} 

public boolean mouseDrag(Event evt,int x,int y) 
{ 
eitem.enable(true); 
eitem1.enable(true); 
eitem3.enable(true); 
current_t.drag(evt.x,evt.y); 
//} 
return true; 
} 

public boolean handleEvent(Event evt) 
{ 
if(evt.id== Event.WINDOW_DESTROY) 
{ 
W_quit aa= new W_quit(this,this); 
aa.show(); 
return true; 
} 
switch(evt.id) 
{ 
case 601: 
case 602: 
case 603: 
case 604: 
case 605: 
//current_t.offsety= (vert.getValue()); 
// current_t.offsetx= (horz.getValue()); 
// current_t.paint= true; 
// current_t.rep(); 
current_t.scrollpaint(horz.getValue(),vert.getValue()); 

vert.setValue(current_t.sc.offsety); 
horz.setValue(current_t.sc.offsetx); 
return true; 
} 


return super.handleEvent(evt); 
} 

public boolean action(Event evt,Object arg) 
{ 
String filename; 
if(evt.target instanceof MenuItem) 
{ 
if(evt.arg.equals("New")) 
{ 
return true; 
} 
if(evt.arg.equals("Open")) 
{ 
openDialog.show(); 
String fn= openDialog.getFile(); 
String filedir= openDialog.getDirectory(); 
read(fn,filedir); 
return true; 
} 
if(evt.arg.equals("Save")) 
{ 
write(current_t.filename,current_t.filedir); 
return true; 
} 
if(evt.arg.equals("Save As")) 
{ 
saveDialog.show(); 
filename= saveDialog.getFile(); 
String filedir= openDialog.getDirectory(); 
write(filename,filedir); 
return true; 
} 
if(evt.arg.equals("Quit")) 
{ 
System.exit(0); 
} 
if(evt.arg.equals("Cut")) 
{ 
current_t.dels(); 
eitem.enable(false); 
eitem1.enable(false); 
eitem3.enable(false); 
eitem2.enable(true); 
return true; 
} 
if(evt.arg.equals("Paste")) 
{ 
current_t.adds(); 
eitem2.enable(false); 
eitem.enable(false); 
eitem1.enable(false); 
eitem3.enable(false); 
return true; 
} 
if(evt.arg.equals("Copy")) 
{ 
eitem.enable(false); 
eitem1.enable(false); 
eitem3.enable(false); 
eitem2.enable(true); 
return true; 
} 
if(evt.arg.equals("Delete")) 
{ 
current_t.dels(); 
eitem3.enable(false); 
eitem.enable(false); 
eitem1.enable(false); 
eitem2.enable(false); 
return true; 
} 
if(evt.arg.equals("Close")) 
{ 
current_t.totle= 0; 
current_t.rep(); 
return true; 
} 
if(evt.arg.equals("Find")) 
{ 
new outPanel(this); 
return true; 
} 
if(evt.arg.equals("Font")) 
{ 
new D_font(this,this,screenw,screenh); 
return true; 
} 
if(evt.arg.equals("Color")) 
{ 
new D_color(this,this,screenw,screenh); 
return true; 
} 
} 
/*else if(evt.target instanceof Button) 
{ 
} */ 
return false; 
} 

public void Find(String find,int dire) 
{ 
current_t.find(find,dire); 
System.out.println(find); 
} 

public static void main(String[] args) 
{ 
MainF ff= new MainF("文本编译器"); 
} 
} 

class outPanel extends Dialog 
{ 
MainF fr; 
Label lbname; //name label 
TextField tfname; //entry field 
outlinePanel psex; //two outlined panels] 
CheckboxGroup cbsex; 
Checkbox up,down,boxc; //2 radios 
Button OK, Cancel; //two pushbuttons 
GridBagConstraints gbc; 
GridBagLayout gbl; 
int dire= 1; 

public outPanel(MainF fr) 
{ 
super(fr,"查找",true); 
this.fr= fr; 
setFont(new Font("Helvetica", Font.PLAIN, 12)); 
setBackground(Color.lightGray); 
gbl =new GridBagLayout(); 
gbc = new GridBagConstraints(); 
setLayout(gbl); 
gbc.ipadx = 5; //make buttons wider 
gbc.ipady = 3; //make buttons higher 
gbc.insets.left =6; 
gbc.insets.right =6; 
gbc.insets.bottom =4; 
gbc.insets.top =4; 
gbc.weightx =1; 
gbc.weighty =1; 

lbname = new Label("查找目标:"); //label 
add_component(lbname, 0,0,1,1); 
tfname = new TextField(20); //text entry field 
add_component(tfname, 1,0,3,1); 
//panel with two buttons for sex 
boxc= new Checkbox("区分大小写"); 
add_component(boxc, 0,2,1,1); 
psex = new outlinePanel(); //first panel 
add_component(psex, 1,1,3,2); //insert in layout 
//put two radio buttons in panel 
up = psex.addCheckbox("向上", false); 
down = psex.addCheckbox("向下", true); 
psex.resize(148,53); 
//add two command buttons to bottom 
OK = new Button("找下一个"); //buttons at bottom 
Cancel = new Button("取消"); 
add_component(OK,4,0,1,1); 
add_component(Cancel,4,2,1,1); 
reshape(100,100, 380,146); 
show(); 
} 
//-------------------------------------- 
private void add_component(Component c, int x, int y, int w, int h) 
{ 
gbc.gridx = x; 
gbc.gridy = y; 
gbc.gridwidth = w; 
gbc.gridheight =h; 
add(c); 
gbl.setConstraints(c, gbc); 
} 
//-------------------------------------- 
public boolean action(Event evt, Object arg) 
{ 
if(evt.target== OK) 
{ 
String ss= tfname.getText(); 
if(boxc.getState()) 
{ 
ss.toLowerCase(); 
fr.Find(ss,dire); 
}else 
fr.Find(ss,dire); 
} 
if (evt.target == Cancel) 
{ 
dispose(); 
return true; 
} 
if(evt.target== up) 
{ 
dire=-1; 
} 
if(evt.target== down) 
{ 
dire= 1; 
} 
if(evt.target== boxc) 
{ 
} 
return super.action(evt, arg); 
} 
} 
//*///======================================= 
class outlinePanel extends Panel 
{ 
CheckboxGroup cbg; 
int yposn; 
Vector cblist; 
//-------------------------------------- 
public outlinePanel() 
{ 
super(); 
cbg = new CheckboxGroup(); //for radio buttons 
setLayout(null); 
yposn = 3; 
cblist = new Vector(); 
} 
//-------------------------------------- 
public void reshape(int x, int y, int w, int h) 
{ 
super.reshape(x,y,w,h); 
for (int i=0; i< cblist.size(); i++) 
{ 
Component c = (Component)cblist.elementAt(i); 
c.reshape(c.location().x, c.location().y, w-6, c.size().height); 
} 
} 
//-------------------------------------- 
public Component add(Component c) 
{ 
super.add(c); 
return c; 
} 
//-------------------------------------- 
public Checkbox addCheckbox(String s, boolean checked) 
{ 
Checkbox cb = new Checkbox(s, cbg, checked); 
add(cb); 
cblist.addElement(cb); 
cb.reshape(3, yposn, 50, 12); 
yposn += 15; 
return cb; 
} 
//-------------------------------------- 
public void paint(Graphics g) 
{ 
Dimension sz = size(); 
Point pt = location(); 
g.setColor(Color.darkGray); // graw rectangle 
g.drawRect(0, 0, sz.width-2, sz.height-2); 
//outline gray rectangle with 2 white inner lines 
g.setColor(Color.white); 
g.drawLine(1, 1, sz.width-3, 1); 
g.drawLine(1, 1, 1, sz.height-3); 
//and two white outer lines 
g.drawLine(0,sz.height-1,sz.width-1,sz.height-1); 
g.drawLine(sz.width-1, 0, sz.width-1, sz.height-1); 
} 
//-------------------------------------- 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -