《Java2实用教程》课后习题参考答案

public static void main(String args[]) {

new WindowCursor(); } }

15.改进本章例子7.30,当释放鼠标键时,如果当前组件和其他组件相交,就将其他组件设置为不可见状 态。

答: import java.awt.*; import java.awt.event.*;

import javax.swing.SwingUtilities;

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

第8 章建立对话框

1. 编写一个应用程序,用户可以在一个文本框里输入数字字符,按Ente r 键后将数字放入一个文本区。 当输入的数字大于100 0 时,弹出一个有模式的对话框,提示用户数字已经大于1000,是否继续将该数 字放入文本区。

答: import java.awt.event.*; import java.awt.*;

import javax.swing.JOptionPane;

class WindowSaveNumber extends Frame implements ActionListener {

TextField textInputNumber; TextArea textSaveNumber; WindowSaveNumber() {

textInputNumber=new TextField(15); textSaveNumber=new TextArea();

add(textInputNumber,BorderLayout.NORTH);

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4