全國2012年1月自考《Java語言程序設(shè)計(jì)(一)》試題_第6頁
35.閱讀下列程序,請回答以下問題:
(1)該程序中的類MyPanel的成員變量twoClick的作用是什么,該成員變量的值是如何改變的?
(2)程序運(yùn)行時(shí),用戶在界面的不同位置,用鼠標(biāo)點(diǎn)擊兩次,界面中會出現(xiàn)什么?
import java.awt.*;import javax.swing.*;import java.awt.event.*;
class MyPanel extends JPanel implements MouseListener{
int leftx,lefly,rightx,righty;
boolean twoClick;
MyPanel() {
twoClick=false;addMouseListener(this);
}
public void mousePressed(MouseEvent e){
if(!twoClick){
leftx=e.getX();lefty=e.getY();
}else{
rightx=e.getX();righty=e.getY();repaint();
}
twoClick=!twoClick:
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void paintComponent(Graphics g){
g.clearRect(0,0,this.getWidth(),this.getHeight());
g.fillRect(1efix,lefty,Math.abs(rightx-leftx),
Math.abs(righty-lefty));
}
}
class MyWin extends JFrame{
MyPanel p=new MyPanel();
MyWin(){
getContentPane().add(p);
setSize(400,400);setLocation(100,100);setVisible(true);
}
}
public class Test35 extends JFrame{
public static void main(String[]args){new MyWin();}
}
責(zé)編: