云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > Java2实用教程(第三版)课后答案 - 耿祥义主编

Java2实用教程(第三版)课后答案 - 耿祥义主编

  • 62 次阅读
  • 3 次下载
  • 2025/5/7 11:07:07

button.setBackground(Color.blue); button.addMouseListener(this);

button.addMouseMotionListener(this); text.addMouseListener(this);

text.addMouseMotionListener(this); addMouseMotionListener(this); setLayout(new FlowLayout()); add(button); add(text);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

setBounds(10,10,350,300); setVisible(true); validate(); }

public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e) {

move=false;

Rectangle rect=button.getBounds(); int x=(int)rect.getX(); int y=(int)rect.getY();

if(rect.intersects(text.getBounds())) {

if(e.getSource()==button) {

text.setVisible(false); }

else if(e.getSource()==text) {

button.setVisible(false); } } }

public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseMoved(MouseEvent e){}

41

public void mouseDragged(MouseEvent e) {

Component com=null;

if(e.getSource() instanceof Component) {

com=(Component)e.getSource(); if(com!=this) move=true;

e=SwingUtilities.convertMouseEvent(com,e,this); if(move) {

x=e.getX(); y=e.getY();

int w=com.getSize().width, h=com.getSize().height;

com.setLocation(x-w/2,y-h/2); } } } }

public class Test {

public static void main(String args[]) {

Win win=new Win(); } }

16.进一步改进本章例子7.33,要求一个按钮在移动时,不允许和其他按钮相交。 答: import java.awt.*; import java.awt.event.*;

class Win extends Frame implements KeyListener {

Button b[]=new Button[8]; int x,y; Win() {

setLayout(new FlowLayout()); for(int i=0;i<8;i++) {

b[i]=new Button(\

b[i].addKeyListener(this); add(b[i]); }

42

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

setBounds(10,10,300,300); setVisible(true); validate(); }

public void keyPressed(KeyEvent e) {

boolean move=false;

Button button=(Button)e.getSource();

Rectangle buttonrect=button.getBounds(); String str=button.getLabel(); int xx=Integer.parseInt(str); x=button.getBounds().x; y=button.getBounds().y;

if(e.getKeyCode()==KeyEvent.VK_UP) {

y=y-2;

if(y<=0) y=0; }

else if(e.getKeyCode()==KeyEvent.VK_DOWN) {

y=y+2;

if(y>=300) y=300; }

else if(e.getKeyCode()==KeyEvent.VK_LEFT) {

x=x-2;

if(x<=0) x=0; }

else if(e.getKeyCode()==KeyEvent.VK_RIGHT) {

x=x+2;

if(x>=300) x=300; }

buttonrect.setLocation(x,y); for(int k=0;k<8;k++)

43

{

Rectangle rect=b[k].getBounds();

if((buttonrect.intersects(rect))&&(xx!=k)) {

move=true; } }

if(move==false) {

button.setLocation(x,y); } }

public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} }

public class Test {

public static void main(String args[]) {

Win win=new Win(); } }

44

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

button.setBackground(Color.blue); button.addMouseListener(this); button.addMouseMotionListener(this); text.addMouseListener(this); text.addMouseMotionListener(this); addMouseMotionListener(this); setLayout(new FlowLayout()); add(button); add(text); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } );

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com