📄
字号:
if(i%4==0) {b[i].setBounds(k1,40,30,30);k1=k1+31;}
else if(i%4==1) {b[i].setBounds(k2,71,30,30);k2=k2+31;}
else if(i%4==2) {b[i].setBounds(k3,102,30,30);k3=k3+31;}
else if(i%4==3) {b[i].setBounds(k4,133,30,30);k4=k4+31;}
}
for(int i=0;i<40;i++)
{ x[i]=b[i].getBounds().x;y[i]=b[i].getBounds().y;//获取按钮左上角的x,y坐标。
}
}
public void keyTyped(KeyEvent e)
{ }
public void keyPressed(KeyEvent e)
{if(e.getKeyCode()==KeyEvent.VK_UP)
{for(int i=0;i<=39;i++)
{ if(s.equals(String.valueOf(i)))
{y[i]=y[i]-2;
if(y[i]<=0) y[i]=0;
b[i].setLocation(x[i],y[i]);
}
}
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{for(int i=0;i<=39;i++)
{if(s.equals(String.valueOf(i)))
{y[i]=y[i]+2;
if(y[i]>=300) y[i]=300;
b[i].setLocation(x[i],y[i]);
}
}
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{for(int i=0;i<=39;i++)
{if(s.equals(String.valueOf(i)))
{x[i]=x[i]-2;
if(x[i]<=0) x[i]=0;
b[i].setLocation(x[i],y[i]);
}
}
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{for(int i=0;i<=39;i++)
{if(s.equals(String.valueOf(i)))
{x[i]=x[i]+2;
if(x[i]>=300) x[i]=300;
b[i].setLocation(x[i],y[i]);
}
}
}
}
public void keyReleased(KeyEvent e)
{}
public void actionPerformed(ActionEvent e)
{ for(int i=0;i<40;i++)
{if(e.getSource()==b[i])
s=b[i].getLabel();
}
}
}
18-例子14
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example18_14 extends Applet
implements ActionListener,KeyListener
{TextField text=new TextField(10);Label label=null;
Button button=new Button("确定");
public void init()
{text.addKeyListener(this);//获得处理键盘事件的监视器。
button.addActionListener(this);
label=new Label("输入一个数后点击按扭:");
add(label); add(text);add(button);
}
public void actionPerformed(ActionEvent e)
{double a=0;
a=Double.valueOf(text.getText()).doubleValue();
text.setText(""+Math.sqrt(a));
}
public void keyPressed(KeyEvent e)
{label.setBackground(Color.green);
char c=e.getKeyChar();
boolean b1=(c<'0'||c>'9'),
b2=(c!='.'),
b3=(c!=KeyEvent.VK_BACK_SPACE);
if(b1&&b2&&b3) //判断是否输入了非数字字符。
{label.setBackground(Color.red);
text.setText(text.getText().substring(0,text.getText().indexOf(c)));
}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
}
18-例子15
import java.awt.*;import java.awt.event.*;
//创建棋盘的类:
class ChessPad extends Panel implements MouseListener,ActionListener
{ int x=-1,y=-1, 棋子颜色=1; //控制棋子颜色的成员变量。
Button button=new Button("重新开局"); //控制重新开局的按扭。
TextField text_1=new TextField("请黑棋下子"),
text_2=new TextField(); //提示下棋的两个文本框。
ChessPad()
{setSize(440,440);
setLayout(null);setBackground(Color.pink);
addMouseListener(this);add(button);button.setBounds(10,5,60,26);
button.addActionListener(this);
add(text_1);text_1.setBounds(90,5,90,24);
add(text_2);text_2.setBounds(290,5,90,24);
text_1.setEditable(false);text_2.setEditable(false);
}
public void paint(Graphics g) //绘制围棋棋盘的外观。
{for(int i=40;i<=380;i=i+20)
{g.drawLine(40,i,400,i);}
g.drawLine(40,400,400,400);
for(int j=40;j<=380;j=j+20)
{g.drawLine(j,40,j,400);}
g.drawLine(400,40,400,400);
g.fillOval(97,97,6,6); g.fillOval(337,97,6,6);
g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);
g.fillOval(217,217,6,6);
}
public void mousePressed(MouseEvent e) //当按下鼠标左键时下棋子。
{ if(e.getModifiers()==InputEvent.BUTTON1_MASK)
{ x=(int)e.getX();y=(int)e.getY(); //获取按下鼠标时的坐标位置。
ChessPoint_black chesspoint_black=new ChessPoint_black(this);
ChessPoint_white chesspoint_white=new ChessPoint_white(this);
int a=(x+10)/20,b=(y+10)/20;
if(x/20<2||y/20<2||x/20>19||y/20>19) //棋盘以外不下棋子。
{}
else{ if(棋子颜色==1) //当棋子颜色是1时下黑棋子。
{this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-7,b*20-7,16,16);
棋子颜色=棋子颜色*(-1);
text_2.setText("请白棋下子");
text_1.setText("");
}
else if(棋子颜色==-1) //当棋子颜色是-1时下白棋子。
{this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-7,b*20-7,16,16);
棋子颜色=棋子颜色*(-1);
text_1.setText("请黑棋下子");
text_2.setText("");
}
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{this.removeAll();棋子颜色=1;
add(button);button.setBounds(10,5,60,26);
add(text_1);text_1.setBounds(90,5,90,24);
text_2.setText("");text_1.setText("请黑棋下子");
add(text_2);text_2.setBounds(290,5,90,24);
}
}
//负责创建黑色棋子的类:
class ChessPoint_black extends Canvas implements MouseListener
{ ChessPad chesspad=null; //棋子所在的棋盘。
ChessPoint_black(ChessPad p)
{setSize(20,20);chesspad=p; addMouseListener(this);
}
public void paint(Graphics g) //绘制棋子的大小。
{ g.setColor(Color.black);g.fillOval(0,0,14,14);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{chesspad.remove(this);//当用鼠标右键点击棋子时,从棋盘中去掉该棋子(悔棋)。
chesspad.棋子颜色=1;
chesspad.text_2.setText("");chesspad.text_1.setText("请黑棋下子");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()>=2)
chesspad.remove(this); //当用左键双击该棋子时,吃掉该棋子。
}
}
//负责创建白色棋子的类:
class ChessPoint_white extends Canvas implements MouseListener
{
ChessPad chesspad=null;
ChessPoint_white(ChessPad p)
{setSize(20,20);addMouseListener(this);
chesspad=p;
}
public void paint(Graphics g)
{ g.setColor(Color.white);g.fillOval(0,0,14,14);
}
public void mousePressed(MouseEvent e)
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{chesspad.remove(this);chesspad.棋子颜色=-1;
chesspad.text_2.setText("请白棋下子"); chesspad.text_1.setText("");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e)
{if(e.getClickCount()>=2)
chesspad.remove(this);
}
}
public class Chess extends Frame //添加棋盘的窗口。
{ChessPad chesspad=new ChessPad();
Chess()
{setSize(600,600);
setVisible(true);
setLayout(null);
Label label=
new Label("单击下棋子,双击吃棋子,右击棋子悔棋",Label.CENTER);
add(label);label.setBounds(70,55,440,26);
label.setBackground(Color.orange);
add(chesspad);chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){System.exit(0);}
});
}
public static void main(String args[])
{ Chess chess=new Chess();
}
}
18-例子16
import java.awt.event.*;import java.applet.*;
import java.awt.*;
public class Move extends.Applet implements KeyListener,ActionListener
{ Button b_go=new Button("go"),
b_replay=new Button("replay");
Rectangle rect1,rect2,rect3;
int b_x=0,b_y=0;
public void init()
{b_go.addKeyListener(this);
b_replay.addActionListener(this);
setLayout(null);
//代表迷宫的矩形:
rect1=new Rectangle(20,40,200,40);
rect2=new Rectangle(200,40,24,240);
rect3=new Rectangle(200,220,100,40);
b_go.setBackground(Color.red); //代表走迷宫者的按扭。
add(b_go);b_go.setBounds(22,45,20,20);
b_x=b_go.getBounds().x;b_y=b_go.getBounds().y;
b_go.requestFocus() ;
add(b_replay);b_replay.setBounds(2,2,45,16);//点击重新开始的按扭。
}
public void paint(Graphics g)
{ //画出迷宫:
g.setColor(Color.green);
g.fillRect(20,40,200,40);
g.setColor(Color.yellow);
g.fillRect(200,40,40,240);
g.setColor(Color.cyan);
g.fillRect(200,220,100,40);
g.drawString("出口",310,220);
g.setColor(Color.black);
g.drawString("点击红色按扭,然后用键盘上的方向键移动按扭",100,20);
}
public void keyPressed(KeyEvent e)
{ //按键盘上的上下、左右键在迷宫中行走:
if((e.getKeyCode()==KeyEvent.VK_UP))
{ //创建一个和按扭:b_go 同样大小的矩形:
Rectangle rect=new Rectangle(b_x,b_y,20,20);
//要求必须在迷宫内行走:
if(rect.intersects(rect1)||rect.intersects(rect2)||
rect.intersects(rect3))
{b_y=b_y-2;b_go.setLocation(b_x,b_y);}
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
if(rect.intersects(rect1)||rect.intersects(rect2)||
rect.intersects(rect3))
{ b_y=b_y+2;b_go.setLocation(b_x,b_y);}
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
if(rect.intersects(rect1)||rect.intersects(rect2)
||rect.intersects(rect3))
{b_x=b_x-2;b_go.setLocation(b_x,b_y);}
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
if(rect.intersects(rect1)||rect.intersects(rect2)||
rect.intersects(rect3))
{ b_x=b_x+2;b_go.setLocation(b_x,b_y);}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{ b_go.setBounds(22,45,20,20);
b_x=b_go.getBounds().x;b_y=b_go.getBounds().y;
b_go.requestFocus() ;
}
}
18-例子17
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class People extends Button implements FocusListener //代表华容道人物的类。
{Rectangle rect=null;
int left_x,left_y;//按扭的左上角坐标.
int width,height; //按扭的宽和高.
String name; int number;
People(int number,String s,int x,int y,int w,int h,Hua_Rong_Road road)
{super(s);
name=s;this.number=number;
left_x=x;left_y=y;
width=w;height=h; setBackground(Color.orange);
road.add(this); addKeyListener(road);
setBounds(x,y,w,h);addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{ setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{ setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet
implements KeyListener,ActionListener
{ People people[]=new People[10];
Rectangle left,right,above ,below;//华容道的边界 .
Button restart=new Button("重新开始");
public void init()
{setLayout(null); add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
people[0]=new People(0,"曹操",104,54,100,100,this);
people[1]=new People(1,"关羽",104,154,100,50,this);
people[2]=new People(2,"张飞",54, 154,50,100,this);
people[3]=new People(3,"刘备",204,154,50,100,this);
people[4]=new People(4,"张辽",54, 54, 50,100,this);
people[5]=new People(5,"曹仁",204, 54, 50,100,this);
people[6]=new People(6,"兵 ",54,254,50,50,this);
people[7]=new People(7,"兵 ",204,254,50,50,this);
people[8]=new People(8,"兵 ",104,204,50,50,this);
people[9]=new People(9,"兵 ",154,204,50,50,this);
people[9].requestFocus();
left=new Rectangle(49,49,5,260);
people[0].setForeground(Color.white) ;
right=new Rectangle(254,49,5,260);
above=new Rectangle(49,49,210,5);
below=new Rectangle(49,304,210,5);
}
public void paint(Graphics g)
{//画出华容道的边界:
g.setColor(Color.cyan);
g.fillRect(49,49,5,260);//left.
g.fillRect(254,49,5,260);//right.
g.fillRect(49,49,210,5); //above.
g.fillRect(49,304,210,5);//below.
//提示曹操逃出位置和按键规则:
g.drawString("点击相应的人物,然后按键盘上的上下左右箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public void keyPressed(KeyEvent e)
{ People man=(People)e.getSource();//获取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ man.left_y=man.left_y+50; //向下前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或下边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{ man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{ man.left_y=man.left_y-50; //向上前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或上边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{ man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{man.left_x=man.left_x-50; //向左前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或左边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{ man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{ man.left_x=man.left_x+50; //向右前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或右边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{ man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{ this.removeAll();
this.init();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -