您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 华容道基于Java swing开发
  所属分类: Java
  开发工具:
  文件大小: 7kb
  下载次数: 0
  上传时间: 2018-06-27
  提 供 者: weixin_********
 详细说明: import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; // ,, public class Hua_Rong_Road extends JFrame implements ActionListener, MouseListener, KeyListener { /** * */ private static final long serialVersionUID = 1L; Person person[] = new Person[ 10]; JButton left, right, above, below; JButton restart = new JButton("重新开始"); JButton nextpass = new JButton("下一关"); public Hua_Rong_Road() { init(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 320, 500); setVisible(true); validate(); } public void init() { setLayout(null); add(restart); restart.setBounds(100, 360, 120, 35); restart.addActionListener(this); add(nextpass); nextpass.setBounds(100, 320, 120, 35); nextpass.addActionListener(this); String name[] = { "曹操", "关羽", "张", "刘", "周", "黄", "兵", "兵", "兵", "兵" }; for (int k = 0; k < name.length; k++) { person[k] = new Person(k, name[k]); person[k].addMouseListener(this); person[k].addKeyListener(this); add(person[k]); } person[0].setBounds(104, 54, 100, 100); person[1].setBounds(104, 154, 100, 50); person[2].setBounds(54, 154, 50, 100); person[3].setBounds(204, 154, 50, 100); person[4].setBounds(54, 54, 50, 100); person[5].setBounds(204, 54, 50, 100); person[6].setBounds(54, 254, 50, 50); person[7].setBounds(204, 254, 50, 50); person[8].setBounds(104, 204, 50, 50); person[9].setBounds(154, 204, 50, 50); person[9].requestFocus(); left = new JButton();// 四个边界 right = new JButton(); above = new JButton(); below = new JButton(); add(left); add(right); add(above); add(below); left.setBounds(49, 49, 5, 260); right.setBounds(254, 49, 5, 260); above.setBounds(49, 49, 210, 5); below.setBounds(49, 304, 210, 5); validate(); } @Override public void keyPressed(KeyEvent e) { Person man = (Person) e.getSource(); if (e.getKeyCode() == KeyEvent.VK_DOWN) go(man, below); if (e.getKeyCode() == KeyEvent.VK_UP) go(man, above); if (e.getKeyCode() == KeyEvent.VK_LEFT) go(man, left); if (e.getKeyCode() == KeyEvent.VK_RIGHT) go(man, right); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void go(Person man, JButton direction) { boolean move = true; Rectangle manRect = man.getBounds(); int x = man.getBounds().x; int y = man.getBounds().y; if (direction == below) y = y + 50; else if (direction == above) y = y - 50; else if (direction == left) x = x - 50; else if (direction == right) x = x + 50; manRect.setLocation(x, y); Rectangle dirctionRect = direction.getBounds(); for (int k = 0; k < 10; k++) { Rectangle personRect = person[k].getBounds(); if ((manRect.intersects(personRect)) && (man.number != k)) move = false; } if (manRect.intersects(dirctionRect)) move = false; if (move == true) man.setLocation(x, y); } public void actionPerformed(ActionEvent e) { dispose(); new Hua_Rong_Road(); } } public class MainClass { public static void main(String[] args) { new Hua_Rong_Road(); } } import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.Color; import javax.swing.JButton; public class Person extends JButton implements FocusListener { /** * */ private static final long serialVersionUID = 1L; int number; Color c=new Color(255, 245, 170); public Person(int number,String s) { super(s); setBackground(c); this.number=number; c=getBackground(); addFocusListener(this); } @Override public void focusGained(FocusEvent e) { setBackground(Color.red); } @Override public void focusLost(FocusEvent e) { setBackground(c); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: Java源代码
 输入关键字,在本站1000多万海量源码库中尽情搜索: