?? 第十八章例子.txt
字號:
18-例子1
import java.applet.*; import java.awt.*; import java.awt.event.*;
public class Example18_1 extends Applet implements MouseListener
{ TextField text;
public void init()
{ text=new TextField(40); add(text);
addMouseListener(this) ;//向小程序增加監視。
}
public void mousePressed(MouseEvent e)
{text.setText("鼠標鍵按下了,位置是"+e.getX()+","+e.getY() );
}
public void mouseReleased(MouseEvent e)
{ text.setText(" 鼠標松開了,位置是"+e.getX()+","+e.getY() );
}
public void mouseEntered(MouseEvent e)
{text.setText(" 鼠標進來了,位置是"+e.getX()+","+e.getY() );
}
public void mouseExited(MouseEvent e)
{text.setText(" 鼠標走開了");
}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()==2)
{ text.setText("鼠標鍵雙擊,位置:"+e.getX()+","+e.getY());
}
else {}
}
}
18-例子2
import java.applet.*;import java.awt.*;import java.awt.event.*;
public class Example18_2 extends Applet implements MouseListener
{ TextField text;int x;
public void init()
{ x=5; text=new TextField(40); add(text);
addMouseListener(this) ;//小程序監視小程序容器中的鼠標事件。
}
public void paint(Graphics g)
{ x=x+3; g.drawOval(10,10,x,x);
}
public void mousePressed(MouseEvent e)
{text.setText("鼠標鍵按下了,位置是"+e.getX()+","+e.getY() );
repaint(); //調用這個方法時,程序將清除paint()方法以前所
//畫的內容,并再調用paint()方法。
}
public void mouseReleased(MouseEvent e)
{ text.setText(" 鼠標松開了,位置是"+e.getX()+","+e.getY() );
}
public void mouseEntered(MouseEvent e)
{text.setText(" 鼠標進來了,位置是"+e.getX()+","+e.getY() );
}
public void mouseExited(MouseEvent e)
{text.setText(" 鼠標走開了");
}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()==2)
{ text.setText("鼠標鍵雙擊,位置:"+e.getX()+","+e.getY());
}
else {}
}
}
18-例子3
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example18_3 extends Applet implements MouseListener
{ double x,y;
public void init()
{x=-1;y=-1; addMouseListener(this);
}
public void paint(Graphics g)
{if(x!=-1&&y!=-1 &&x>=80)
{g.setColor(Color.blue);
g.drawOval((int)x,(int)y,10,10);
}
else if (x!=-1&&y!=-1&& x<80&&x>40 )
{g.setColor(Color.red);
g.drawString("小心地雷",(int)x+5,(int)y+5);
g.drawString("往右走",(int)x+5,(int)y+15);
}
else if (x!=-1&&y!=-1&& x<=40 )
{g.setColor(Color.red);
g.drawString("地雷爆炸",(int)x+15,(int)y+5);
g.fillOval((int)x,(int)y,20,20);
}
}
public void mousePressed(MouseEvent e)
{ x=e.getX();y=e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
}
18-例子4
import java.applet.*;import java.awt.*;import java.awt.event.*;
public class Example18_4 extends Applet implements MouseListener
{ TextField text;int x,y,tom;
public void init()
{ x=5; tom=0; text=new TextField(40);
add(text);addMouseListener(this) ;//向小程序增加監視器。
}
public void paint(Graphics g)
{if(tom==1)
{g.setColor(Color.red);
g.fillOval(10+(int)x,10+(int)y,15,15);
g.setColor(Color.green); g.fillOval(100,100,30,30);
g.setColor(Color.red); g.fillOval(120,120,35,35);
}
else if(tom==-1)
{g.setColor(Color.pink); g.fillOval(100,100,50,50);
g.setColor(Color.white); g.fillOval(120,120,45,45);
}
else if(tom==2)
{ g.setColor(Color.red);
g.drawString("來了,漫畫帶來了嗎?",80,120);
}
else if(tom==-2)
{ g.setColor(Color.green);
g.drawString("歡迎再來,別忘了關門",80,120);
}
}
public void mousePressed(MouseEvent e)
{ tom=1; x=e.getX(); y=e.getY();
text.setText("鼠標鍵按下了,位置是"+e.getX()+","+e.getY() );
repaint();
}
public void mouseReleased(MouseEvent e)
{ tom=-1;text.setText(" 鼠標松開了,位置是"+e.getX()+","+e.getY() );
repaint();
}
public void mouseEntered(MouseEvent e)
{tom=2; text.setText(" 鼠標進來了,位置是"+e.getX()+","+e.getY() );
repaint();
}
public void mouseExited(MouseEvent e)
{tom=-2; text.setText(" 鼠標走開了");
repaint();
}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()==2)
{ text.setText("鼠標鍵雙擊,位置:"+e.getX()+","+e.getY());
}
}
}
18-例子5
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example18_5 extends Applet implements MouseListener
{ TextField text;
public void init()
{ text=new TextField(40);
add(text);addMouseListener(this) ;//向小程序增加監視器。
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
text.setText("鼠標右鍵按下了,位置是"+e.getX()+","+e.getY() );
else if(e.getModifiers()==InputEvent.BUTTON1_MASK)
text.setText("鼠標left按下了,位置是"+e.getX()+","+e.getY() );
}
public void mouseReleased(MouseEvent e)
{ }
public void mouseEntered(MouseEvent e)
{ }
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{ }
}
18-例子6
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example18_6 extends Applet implements MouseMotionListener
{ TextField text;
public void init()
{ text=new TextField();
setLayout(new BorderLayout());
setBackground(Color.green);
addMouseMotionListener(this); add(text,"South");
}
public void mouseDragged(MouseEvent e)
{text.setBackground(Color.red);
text.setText("鼠標的坐標:"+"("+e.getX()+","+e.getY()+")");
}
public void mouseMoved(MouseEvent e)
{text.setBackground(Color.blue);
text.setText("鼠標的坐標:"+"("+e.getX()+","+e.getY()+")");
}
}
18-例子7
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Example18_7 extends Applet implements MouseListener
{ TextArea text;Button button;
public void init()
{ button=new Button("我上面也能發生鼠標事件");
button.addMouseListener(this);
text=new TextArea();
text.addMouseListener(this);
setLayout(new BorderLayout());
add(button,"North");add(text,"Center");
}
public void mousePressed(MouseEvent e)
{ if(e.getSource()==button)
{text.setText("按鈕發生鼠標事件"+"("+e.getX()+","+e.getY()+")");
}
else if(e.getSource()==text)
{text.setText("\n"+"文本區發生鼠標事件"+"("+e.getX()+","+e.getY()+")");
}
}
public void mouseReleased(MouseEvent e)
{ text.setText("鼠標釋放位置:"+"("+e.getX()+","+e.getY()+")");
}
public void mouseEntered(MouseEvent e)
{ text.setText("鼠標進入位置:"+"("+e.getX()+","+e.getY()+")");
}
public void mouseExited(MouseEvent e)
{ text.setText("鼠標退出位置:"+"("+e.getX()+","+e.getY()+")");
}
public void mouseClicked(MouseEvent e)
{ text.setText("鼠標點擊位置:"+"("+e.getX()+","+e.getY()+")");
}
}
18-例子8.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example18_8 extends Applet implements MouseListener
{ double x,y;
public void init()
{x=-1;y=-1; addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{x=e.getX();y=e.getY();
if(0<=x&&x<=50&&0<=y&&y<=50)
{setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));}
}
public void mouseReleased(MouseEvent e)
{x=e.getX();y=e.getY();
if(!(0<=x&&x<=50&&0<=y&&y<=50))
{setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));}
}
public void mouseEntered(MouseEvent e)
{setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
18-例子9
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example18_9 extends Applet implements MouseMotionListener
{ int x=-1,y=-1;
public void init()
{ setBackground(Color.green);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{if(x!=-1&&y!=-1)
{g.setColor(Color.red);
g.drawLine(x,y,x,y);
}
}
public void mouseDragged(MouseEvent e)
{x=(int)e.getX();y=(int)e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void update(Graphics g)
{ paint(g);
}
}
18-例子10
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example18_10 extends Applet
implements ActionListener,
MouseMotionListener
{ int x=-1,y=-1,橡皮擦通知=0,清除通知=0;
Color c=new Color(255,0,0);int con=3;
Button b_red,b_blue,b_green,
清除,b_quit;
public void init()
{addMouseMotionListener(this);
b_red=new Button("畫紅色圖形"); b_blue=new Button("蘭色圖形");
b_green=new Button("畫綠色圖形"); b_quit=new Button("橡皮");
清除=new Button("清除");
add(b_red); add(b_green); add(b_blue); add(b_quit);add(清除);
b_red.addActionListener(this); b_green.addActionListener(this);
b_blue.addActionListener(this); b_quit.addActionListener(this);
清除.addActionListener(this);
}
public void paint(Graphics g)
{if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0)
{g.setColor(c); g.fillOval(x,y,con,con);
}
else if(橡皮擦通知==1&&清除通知==0)
{g.clearRect(x,y,10,10);
}
else if(清除通知==1&&橡皮擦通知==0)
{g.clearRect(0,0,getSize().width,getSize().height);
}
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();y=(int)e.getY(); repaint();
}
public void mouseMoved(MouseEvent e)
{ }
public void update(Graphics g)
{ paint(g);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==b_red)
{ 橡皮擦通知=0;清除通知=0; c=new Color(255,0,0);
}
else if(e.getSource()==b_green)
{橡皮擦通知=0;清除通知=0; c=new Color(0,255,0);
}
else if(e.getSource()==b_blue)
{ 橡皮擦通知=0;清除通知=0; c=new Color(0,0,255);
}
if(e.getSource()==b_quit)
{橡皮擦通知=1;清除通知=0 ;
}
if(e.getSource()==清除)
{清除通知=1; 橡皮擦通知=0;repaint();
}
}
}
18-例子11
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example18_11 extends Applet implements KeyListener
{Button b=new Button("我在這里");
public void init()
{addKeyListener(this);//小程序(容器)獲得處理鍵盤事件的監視器。
add(b);
}
public void keyPressed(KeyEvent e)
{if(e.getKeyCode()==KeyEvent.VK_A)//判斷是否按下了a字符鍵。
{b.setBounds(50,60,100,100);
b.setBackground(Color.red);}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
}
18-例子12
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example18_12 extends Applet implements KeyListener
{Button b=new Button("我能被移動");
int b_x=0,b_y=0;
public void init()
{b.addKeyListener(this);//按鈕獲得鍵盤事件監視器.
setLayout(null);
add(b);
b.setBounds(20,20,70,30);
b_x=b.getBounds().x;b_y=b.getBounds().y;//獲取按鈕左上角的x,y坐標
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{if(e.getKeyCode()==KeyEvent.VK_UP)
{b_y=b_y-2;
if(b_y<=0) b_y=0;
b.setLocation(b_x,b_y);
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{b_y=b_y+2;
if(b_y>=300) b_y=300;
b.setLocation(b_x,b_y);
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{b_x=b_x-2;
if(b_x<=0) b_x=0;
b.setLocation(b_x,b_y);
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{b_x=b_x+2;
if(b_x>=300) b_x=300;
b.setLocation(b_x,b_y);
}
}
public void keyReleased(KeyEvent e)
{}
}
18-例子13
import java.applet.*;import java.awt.*;import java.awt.event.*;
public class Example18_13 extends Applet implements KeyListener,ActionListener
{Label label=new Label("點擊一個積木,然后移動它");
Button b[]=new Button[40];
int x[]=new int[40];
int y[]=new int[40];
String s=null;
public void init()
{ setLayout(null);
add(label);
label.setBounds(0,0,150,30);
int k1=70,k2=70,k3=70,k4=70;
for(int i=0;i<40;i++)
{b[i]=new Button(String.valueOf(i));
if(i%3==0) b[i].setBackground(Color.red);
if(i%3==1) b[i].setBackground(Color.blue);
if(i%3==2) b[i].setBackground(Color.yellow);
b[i].addKeyListener(this);//按鈕獲得鍵盤事件監視器.
b[i].addActionListener(this); add(b[i]);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -