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

📄 文本编辑器.java

📁 Java 图形界面入门实例
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//版权所有helj 
import java.awt.*; 
import java.io.*; 
import java.util.*; 
//W_color;C_str;C_file;C_back;W_find;W_fond 

interface I_Method 
{ 
public void setQuit(); 
public void setfont(Font f); 
public void setcolor(Color c); 
public void Find(String find,int dire); 
} 



class D_color extends Dialog 
{ 
private Button OK,Cancel; 
private I_Method mp; 
private Rectangle test; 

private Color current= Color.black; 
private Color []colors; 
private Color []colors1; 
private Color []colors2; 
private int strlen= 250; 

public D_color(Frame fr,I_Method mp,int x,int y) 
{ 
super(fr,"调色板",true); 
this.mp= mp; 
colors= new Color[strlen]; 
colors1= new Color[strlen]; 
colors2= new Color[strlen]; 
for (int i = 0; i < strlen; i++) 
{ 
float h = ((float)i)/((float)strlen); 
colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f)); 
} 
for (int i = 0; i < strlen; i++) 
{ 
float h = ((float)i)/((float)strlen); 
colors1[i] = new Color(Color.HSBtoRGB(1.0f,h,1.0f)); 
} 
for (int i = 0; i < strlen; i++) 
{ 
float h = ((float)i)/((float)strlen); 
colors2[i] = new Color(Color.HSBtoRGB(1.0f,1.0f,h)); 
} 

setLayout(null); 
OK= new Button("确定"); 
Cancel= new Button("取消"); 
OK.reshape(320,100,80,30); 
add(OK); 
Cancel.reshape(320,150,80,30); 
add(Cancel); 
test= new Rectangle(0,0,300,250); 
reshape(x/2-210,y/2-140,420,280); 
show(); 
} 

public void update(Graphics g) 
{ 
int y= 0; 
for (int i = 0; i < strlen; i++) 
{ 
g.setColor(colors[i]); 
g.fillRect(0,y,100,1); 
y+=1; 
} 
y= 0; 
for (int i = 0; i < strlen; i++) 
{ 
g.setColor(colors1[i]); 
g.fillRect(100,y,100,1); 
y+=1; 
} 
y= 0; 
for (int i = 0; i < strlen; i++) 
{ 
g.setColor(colors2[i]); 
g.fillRect(200,y,100,1); 
y+=1; 
} 
g.setColor(current); 
g.fillRect(330,30,60,60); 
} 

public void paint(Graphics g) 
{ 
update(g); 
} 

public boolean mouseMove(Event evt,int x,int y) 
{ 
if(test.inside(x,y)) 
{ 
Graphics g1= getGraphics(); 
//g1.drawImage(back,oldx,oldy); 
//g1.clipRect(x-3,y-3,6,6); 
g1.setColor(Color.white); 
g1.drawArc(x-3,y-3,6,6,60,360); 
g1.drawLine(x-3,y,x+3,y); 
g1.drawLine(x,y-3,x,y+3); 
repaint(); 
//oldx= x-3; 
//oldy= y-3; 
return true; 
} 
return false; 
} 


public boolean mouseDown(Event evt,int x,int y) 
{ 
if((x>=0)&&(x<=100)) 
{ 
current= colors[y]; 
System.out.println(y); 
getGraphics().setColor(current); 
getGraphics().fillRect(330,30,60,60); 
return true; 
} 
if((x>100)&&(x<=200)) 
{ 
current= colors1[y]; 
getGraphics().setColor(current); 
getGraphics().fillRect(330,30,60,60); 
return true; 
} 
if((x>200)&&(x<=300)) 
{ 
current= colors2[y]; 
getGraphics().setColor(current); 
getGraphics().fillRect(330,30,60,60); 
return true; 
} 
return false; 
} 

public boolean action(Event evt,Object arg) 
{ 
if(evt.target instanceof Button) 
{ 
if(evt.target== OK) 
{ 
mp.setcolor(current); 
dispose(); 
return true; 
} 
if(evt.target== Cancel) 
{ 
dispose(); 
return true; 
} 
} 
return super.action(evt,arg); 
} 
} 

class W_quick extends Window 
{ 
MainF fr; 
private Dimension sz; 
private int w,h; 
private int fw,fh; 
private FontMetrics fm; 
private int index=0; 

public W_quick(MainF fr,I_Method qm,int x,int y) 
{ 
super(fr); 
this.fr= fr; 
setBackground(Color.lightGray); 

} 

public void show(int x,int y) 
{ 
repaint(); 
reshape(x,y,100,160); 
show(); 
} 

private void draw_uplines(Graphics g,int x,int y,int w1,int h1) 
{ 
g.setColor(Color.white); 
g.drawLine(x,y,x+w1-1,x); 
g.drawLine(x,y,x,y+h1-1); 

g.setColor(Color.black); 
g.drawLine(x+w1-1,y-1,x+w1-1,y+h1-1); 
g.drawLine(x+w1-2,y+1,x+w1-2,y+h1-2); 
g.drawLine(x+1,y+h-2,x+w1-2,y+h1-2); 
// g.drawLine(x,y+h1-1,x+w1-1,y+h1-1); 
// g.setColor(Color.black); 
// g.drawLine(x,y+h1-1,x+w1-1,y+h1-1); 

} 

public void paint(Graphics g) 
{ 
sz= size(); 
w= sz.width; 
h= sz.height; 
// g.setFont(Ffont); 
fm= g.getFontMetrics(); 
fh=fm.getHeight(); 
fw= fm.charWidth('C'); 
g.setColor(Color.gray); 
g.drawRect(10,(index-1)*fh+6,w-20,fh); 
// g.setColor(Color.lightGray); 
// g.drawRect(0,0,w,h); 
draw_uplines(g,1,1,w-1,h-1); 
g.setColor(Color.black); 
g.drawString("Cut",13,fh); 
g.drawString("Copy",13,2*fh); 
g.drawString("Paste",13,3*fh); 
g.drawString("Delete",13,4*fh); 
} 

public boolean mouseMove(Event evt,int x,int y) 
{ 
for(int i=1;i<=4;i++) 
if((y>(i-1)*fh)&&(y<(i*fh))) 
{ 
index= i; 
repaint(); 
} 
return super.mouseMove(evt,x,y); 
} 

public boolean mouseDown(Event evt,int x,int y) 
{ 
if(y>0&&y { 
fr.current_t.dels(); 
fr.eitem.enable(false); 
fr.eitem1.enable(false); 
fr.eitem3.enable(false); 
fr.eitem2.enable(true); 
hide(); 
return true; 
} 
if(y>fh&&(y<(2*fh))) 
{ 
fr.eitem.enable(false); 
fr.eitem1.enable(false); 
fr.eitem3.enable(false); 
fr.eitem2.enable(true); 
hide(); 
return true; 
} 
if((y>(2*fh))&&(y<(3*fh))) 
{ 
fr.current_t.adds(); 
fr.eitem2.enable(false); 
fr.eitem.enable(false); 
fr.eitem1.enable(false); 
fr.eitem3.enable(false); 
hide(); 
return true; 
} 
if((y>(3*fh))&&(y<(4*fh))) 
{ 
fr.current_t.dels(); 
fr.eitem3.enable(false); 
fr.eitem.enable(false); 
fr.eitem1.enable(false); 
fr.eitem2.enable(false); 
hide(); 
return true; 
} 
return super.mouseDown(evt,x,y); 
} 
} 

class ptest extends Panel 
{ 
String test= "ABCDabcd"; 
D_font ff; 

public ptest(D_font ff) 
{ 
this.ff= ff; 
repaint(); 
} 

private void draw_downlines(Graphics g,int x,int y,int w,int h) 
{ 
g.setColor(Color.white); 
g.drawLine(x+1,y+h-1,x+w-1,y+h-1); 
g.drawLine(x+w-1,y+h-1,x+w-1,y+1); 

g.setColor(Color.black); 
g.drawLine(x,y,x,y+h); 
g.drawLine(x+1,y+1,x+1,y+h-1); 
g.drawLine(x,y,x+w,y); 
g.drawLine(x+1,y+1,x+w-1,y+1); 
} 

public void rep() 
{ 
repaint(); 
} 

public void paint(Graphics g) 
{ 
Dimension sz= size(); 
Point pt= location(); 
g.setColor(Color.darkGray); 
g.drawRect(0,0,sz.width-2,sz.height-2); 

g.setColor(Color.white); 
g.drawLine(1,1,1,sz.height-3); 
g.drawLine(1,1,sz.width-3,1); 

g.drawLine(0,sz.height-1,sz.width-1,sz.height-1); 
g.drawLine(sz.width-1,0,sz.width-1,sz.height-1); 

draw_downlines(g,15,10,sz.width-30,sz.height-20); 
g.clipRect(16,11,sz.width-31,sz.height-21); 
g.setColor(Color.black); 
g.setFont(ff.bf); 
g.drawString(test,20,sz.height-21); 
} 
} 



class D_font extends Dialog 
{ 
public Font bf; 
private Button OK,Cancel; 
private Label l1,l2,l3; 
private String items1[]={"Helvetica","Courier","TimesRoman","Dialog"}; 
private String items2[]={"PLAIN","BOLD","PLAIN+BOLD","ITALIC","BOLD+ITALIC"}; 
private int[] style= new int[5]; 
private String items3[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"}; 
private TextField f1,f2,f3; 
private List ll1,ll2,ll3; 
private ptest test; 
private I_Method im; 

public void init() 
{ 
style[0]= Font.PLAIN; 
style[1]= Font.BOLD; 
style[2]= Font.PLAIN+Font.BOLD; 
style[3]= Font.ITALIC; 
style[4]= Font.BOLD+Font.ITALIC; 
} 

public D_font(Frame fr,I_Method qm,int x,int y) 
{ 
super(fr,"字体",true); 
init(); 
setFont(new Font("TimesRoman",Font.PLAIN,14)); 
im= qm; 

GridBagLayout gridBag= new GridBagLayout(); 
GridBagConstraints c= new GridBagConstraints(); 
setLayout(gridBag); 
c.insets.left= 4; 
c.insets.right= 4; 
c.insets.bottom= 1; 
c.insets.top= 1; 

l1= new Label("字体:",Label.LEFT); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=0; 
c.gridy=0; 
c.gridwidth=2; 
c.gridheight =1; 
gridBag.setConstraints(l1,c); 
add(l1); 

l2= new Label("字体样式:",Label.LEFT); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=2; 
c.gridy=0; 
c.gridwidth=1; 
c.gridheight =1; 
gridBag.setConstraints(l2,c); 
add(l2); 

l3= new Label("大小:",Label.LEFT); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=3; 
c.gridy=0; 
c.gridwidth=1; 
c.gridheight =1; 
gridBag.setConstraints(l3,c); 
add(l3); 

OK= new Button("确定"); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=4; 
c.gridy=1; 
c.gridwidth=1; 
c.gridheight =1; 
gridBag.setConstraints(OK,c); 
add(OK); 

f1= new TextField(); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=0; 
c.gridy=1; 
c.gridwidth=2; 
c.gridheight =1; 
gridBag.setConstraints(f1,c); 
add(f1); 

f2= new TextField(); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=2; 
c.gridy=1; 
c.gridwidth=1; 
c.gridheight =1; 
gridBag.setConstraints(f2,c); 
add(f2); 

f3= new TextField(); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=3; 
c.gridy=1; 
c.gridwidth=1; 
c.gridheight =1; 
gridBag.setConstraints(f3,c); 
add(f3); 

Cancel= new Button("取消"); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=4; 
c.gridy=2; 
c.gridwidth=1; 
c.gridheight =1; 
gridBag.setConstraints(Cancel,c); 
add(Cancel); 

ll1= new List(); 
for(int i=0;i ll1.addItem(items1[i]); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 1.0; 
c.weighty= 1.0; 
c.gridx=0; 
c.gridy=2; 
c.gridwidth=2; 
c.gridheight =7; 
gridBag.setConstraints(ll1,c); 
add(ll1); 

ll2= new List(); 
for(int i1=0;i1 ll2.addItem(items2[i1]); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=2; 
c.gridy=2; 
c.gridwidth=1; 
c.gridheight =7; 
gridBag.setConstraints(ll2,c); 
add(ll2); 

ll3= new List(); 
for(int i2=0;i2 ll3.addItem(items3[i2]); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 0.0; 
c.weighty= 0.0; 
c.gridx=3; 
c.gridy=2; 
c.gridwidth=1; 
c.gridheight =7; 
gridBag.setConstraints(ll3,c); 
add(ll3); 

test= new ptest(this); 
c.gridwidth= GridBagConstraints.REMAINDER; 
c.fill= GridBagConstraints.BOTH; 
c.weightx= 1.0; 
c.weighty= 1.0; 
c.gridx=0; 
c.gridy=9; 
c.gridwidth=5; 
c.gridheight =4; 
gridBag.setConstraints(test,c); 
add(test); 

reshape(x/2-210,y/2-140,420,280); 
show(); 
} 

public boolean action(Event evt,Object arg) 
{ 
if(evt.target instanceof Button) 
{ 
if(evt.target== OK) 
{ 
if(ll1.getSelectedIndex()==-1) 
ll1.select(0); 
if(ll3.getSelectedIndex()==-1) 
ll3.select(0); 
if(ll2.getSelectedIndex()==-1) 
ll2.select(0); 
int size= Integer.parseInt(items3[ll3.getSelectedIndex()]); 
bf=new Font(items1[ll1.getSelectedIndex()],style[ll2.getSelectedIndex()],size); 
im.setfont(bf); 
dispose(); 
return true; 
} 
if(evt.target== Cancel) 
{ 
dispose(); 
return true; 
} 
} 

if(evt.target instanceof List) 
{ 
if(ll1.getSelectedIndex()==-1) 
ll1.select(0); 
if(ll3.getSelectedIndex()==-1) 
ll3.select(0); 
if(ll2.getSelectedIndex()==-1) 
ll2.select(0); 
f1.setText(ll1.getSelectedItem()); 
f2.setText(ll2.getSelectedItem()); 
f3.setText(ll3.getSelectedItem()); 

int size= Integer.parseInt(items3[ll3.getSelectedIndex()]); 
bf=new Font(items1[ll1.getSelectedIndex()],style[ll2.getSelectedIndex()],size); 
test.rep(); 
return true; 
} 
return super.action(evt,arg); 
} 
} 


class W_quit extends Dialog 

⌨️ 快捷键说明

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