?? java.6.txt
字號:
《Java就業(yè)培訓(xùn)教程》P290源碼
程序清單:TestFrame.java
import java.awt.*;
import java.awt.event.*;
public class TestFrame
{
public static void main(String [] args)
{
Frame f=new Frame("IT資訊交流網(wǎng)");
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new MyWindowListener());
}
}
class MyWindowListener implements WindowListener
{
public void windowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}
《Java就業(yè)培訓(xùn)教程》P295源碼
import java.awt.*;
import java.awt.event.*;
public class TestFrame implements ActionListener
{
Frame f=new Frame("IT資訊交流網(wǎng)");
public static void main(String [] args)
{
TestFrame tf=new TestFrame();
tf.init();
}
public void init()
{
Button btn=new Button("退出");
btn.addActionListener(new TestFrame());
f.add(btn);
f.setSize(300,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
f.setVisible(false);
f.dispose();
System.exit(0);
}
}
《Java就業(yè)培訓(xùn)教程》P296源碼
import java.awt.*;
import java.awt.event.*;
class TestFrame
{
Frame f=new Frame("IT資訊交流網(wǎng)");
public static void main(String [] args)
{
new TestFrame().init();
}
public void init()
{
Button btn=new Button("退出");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
f.add(btn);
f.setSize(300,300);
f.setVisible(true);
}
}
《Java就業(yè)培訓(xùn)教程》P300源碼
程序清單:TestMyButton.java
import java.awt.*;
import java.awt.event.*;
class MyButton extends Button
{
private MyButton friend;
public void setFriend(MyButton friend)
{
this.friend = friend;
}
public MyButton(String name)
{
super(name);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
protected void processMouseMotionEvent(MouseEvent e)
{
setVisible(false);
friend.setVisible(true);
}
}
public class TestMyButton
{
public static void main(String [] args)
{
MyButton btn1 =new MyButton("你來抓我呀!");
MyButton btn2 =new MyButton("你來抓我呀!");
btn1.setFriend(btn2);
btn2.setFriend(btn1);
btn1.setVisible(false);
Frame f =new Frame("it315");
f.add(btn1, "North");//將btn1增加到f的北部
f.add(btn2, "South");//將btn2增加到f的南部
f.setSize(300,300);
f.setVisible(true);
btn1.setVisible(false);
}
}
《Java就業(yè)培訓(xùn)教程》P301源碼
程序清單:DrawLine.java
import java.awt.*;
import java.awt.event.*;
public class DrawLine
{
Frame f= new Frame("IT資訊交流網(wǎng)");
public static void main(String [] args)
{
new DrawLine().init();
}
public void init()
{
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
f.getGraphics().setColor(Color.red);
//設(shè)置繪圖顏色為紅色
f.getGraphics().drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
}
《Java就業(yè)培訓(xùn)教程》P306源碼
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class MyLine
{
private int x1;
private int y1;
private int x2;
private int y2;
public MyLine(int x1,int y1,int x2,int y2)
{
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}
public void drawMe(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
}
}
public class RerawAllLine extends Frame
{
Vector vLines=new Vector();
public static void main(String [] args)
{
RedrawAllLine f=new RedrawAllLine();
f.init();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
Enumeration e=vLines.elements();
while(e.hasMoreElements())
{
MyLine ln=(MyLine)e.nextElement();
ln.drawMe(g);
}
}
public void init()
{
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
((Window)e.getSource()).dispose();
System.exit(0);
}
});
addMouseListener(new MouseAdapter(){
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
Graphics g=e.getComponent().getGraphics();
g.setColor(Color.red);
g.drawLine(orgX,orgY,e.getX(),e.getY());
vLines.add(new MyLine(orgX,orgY,e.getX(),e.getY()));
}
});
this.setSize(300,300);
setVisible(true);
}
}
《Java就業(yè)培訓(xùn)教程》P311源碼
import java.awt.*;
import java.awt.event.*;
public class DrawImage extends Frame
{
Image img=null;
public static void main(String [] args)
{
DrawImage f= new DrawImage();
f.init();
}
public void init()
{
img=this.getToolkit().getImage("c:\\test.gif");
setSize(300,300);
setVisible(true);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
getGraphics().drawImage(img,0,0,this);
}
}
《Java就業(yè)培訓(xùn)教程》P312源碼
程序清單:DrawLine.java
import java.awt.*;
import java.awt.event.*;
public class DrawLine extends Frame
{
Image oimg=null;
Graphics og=null;
public static void main(String [] args)
{
new DrawLine().init();
}
public void init()
{
setSize(300,300);
setVisible(true);
Dimension d=getSize();
oimg=createImage(d.width,d.height);
og=oimg.getGraphics();
addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(Color.red);//設(shè)置繪圖顏色為紅色
g.setFont(new Font("隸書",Font.ITALIC|Font.BOLD,30));
//設(shè)置文本的字體
g.drawString(new String(orgX +"," +orgY),orgX,orgY);
//打印鼠標按下時的坐標文本
g.drawString(new String(e.getX() +"," +e.getY()),
e.getX(),e.getY());//打印鼠標釋放時的坐標文本
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.red);//設(shè)置繪圖顏色為紅色
og.setFont(new Font("隸書",Font.ITALIC|Font.BOLD,30));
//設(shè)置文本的字體
og.drawString(new String(orgX +"," +orgY),orgX,orgY);
//打印鼠標按下時的坐標文本
og.drawString(new String(e.getX() +"," +e.getY()),
e.getX(),e.getY());//打印鼠標釋放時的坐標文本
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
public void paint(Graphics g)
{
if(oimg !=null)
g.drawImage(oimg,0,0,this);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -