1这个程序为什么只显示GlassPane上的效果,却把后边的东西全部遮住了啊,高手指点啊!
2还有想请教一下谁有关于JAVA格式规范的资料,能提供一下吗?(感觉写JAVA时前后文顺序不一样会有很大影响).
程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GlassPaneTest extends JFrame {
private JLayeredPane jlp;
public JButton[] jb=new JButton[5];
public static void main(String args[]){
GlassPaneTest test=new GlassPaneTest();
test.setSize(300,200);
test.setVisible(true);
}
public GlassPaneTest(){
String[] layer={"default","palette","modal","popup","drag"};
jlp=getLayeredPane();
for(int i=0;i<5;i++){
jb[i]=new JButton(layer[i]);
jb[i].setBounds(i*50,i*20,80,60);
jlp.add(jb[i]);
jlp.setLayer(jb[i],i*100);
}
GlassPane glass=new GlassPane(jlp);
setGlassPane(glass);
glass.setVisible(true);
}
}
class GlassPane extends Component implements MouseListener,MouseMotionListener{
private int mx,my;
private JLayeredPane jlp;
public GlassPane(JLayeredPane p){
mx=my=-99;
jlp=p;
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.fillOval(mx-5,my-5,10,10);
}
public void mouseClicked(MouseEvent e){
redispatchMouseEvent(e);
}
public void mouseEntered(MouseEvent e){
redispatchMouseEvent(e);
}
public void mouseExited(MouseEvent e){
redispatchMouseEvent(e);
}
public void mousePressed(MouseEvent e){
redispatchMouseEvent(e);
}
public void mouseReleased(MouseEvent e){
redispatchMouseEvent(e);
}
public void mouseDragged(MouseEvent e){
redispatchMouseEvent(e);
}
public void mouseMoved(MouseEvent e){
mx=e.getX();
my=e.getY();
repaint();
}
public void redispatchMouseEvent(MouseEvent e){
Point point=e.getPoint();
Component component=jlp.getComponentAt(point);
if(component!=null)
component.dispatchEvent(new MouseEvent(component,e.getID(),e.getWhen(),e.getModifiers(),point.x,point.y,e.getClickCount(),e.isPopupTrigger()));
}
}