java统一管理文字_为了统一管理组件和容器,Java为所有组件类定义的超类
5、[ 主观题 ]【程序分析题】阅读下列程序,请回答以下问题:
(1)该程序中的类 MyPanel 的成员变量 twoClick 的作用是什么,该成员变量的值是如何改变的?
(2)程序运行时,用户在界面的不同位置,用鼠标点击两次,界面中会出现什么? import java.awt.*;import javax.swing.*;import java.awt.event.*; class MyPanel extends JPanel implements MouseListener{ int leftx,lefty,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();}
}