Java2实用教程(第三版)课后答案-清华大学出版社 耿祥义主编 下载本文

内容发布更新时间 : 2024/11/5 23:25:37星期一 下面是文章的全部内容请认真阅读。

class Win extends Frame implements MouseListener,MouseMotionListener {

Button button; TextField text; int x,y;

boolean move=false; Win() {

button=new Button(\用鼠标拖动我\text=new TextField(\用鼠标拖动我\text.setBackground(Color.green);

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){}

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]); }

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++) {

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(); } }