当前位置: 代码迷 >> J2SE >> 为啥重绘JButton后加入到一个JPanel中显示就不正常勒呢
  详细解决方案

为啥重绘JButton后加入到一个JPanel中显示就不正常勒呢

热度:10228   发布时间:2013-02-25 00:00:00.0
为什么重绘JButton后加入到一个JPanel中显示就不正常勒呢
重绘JButton的代码paintComponent如下:
Java code
public void paintComponent(Graphics g) {        Graphics2D g2 = (Graphics2D)g ;         Paint oldPaint = g2.getPaint() ;         if(isEntered){        if (getModel().isArmed()) {            g2.setColor(bacClick);                    } else            g2.setColor(bacNormal);                GradientPaint p = new GradientPaint(0,0,                new Color(0xFFFFFF), 0, 2, bacNormal);        g2.setPaint(p) ;         g2.fillRoundRect(0, 0, getWidth()- 1, getHeight() /2+10, 5, 5);        GradientPaint pInigel = new GradientPaint(0,getHeight()/2-10,bacNormal,0,getHeight(),new Color(0xFFFFFF)) ;         g2.setPaint(pInigel) ;         g2.fillRoundRect(0, getHeight()/2-5, getWidth()- 1, getHeight(), 5, 5);        }        g2.setFont(new Font("方正舒体", 0, 30));        if (getModel().isArmed()) {            g2.setColor(bacNormal);        } else            if(isEntered)                g2.setColor(Color.YELLOW) ;             else            g2.setColor(foreGround);        g2.drawString(text,1, getHeight()/ 2 + 9);        g2.setPaint(oldPaint) ;         super.paintComponent(g);    }

初学java,里面很多都是照着网上写的。单独加入到JFrame是显示正常,可是一加入到JPanel中背景只能显示左上部分,文字显示正常(传不了图啊。。。。)
不知道原因为何,希望知道的友友解释下
附上整个重绘代码:
Java code
package xsb.weiwei;import javax.swing.* ; import java.awt.* ; import java.awt.event.* ; import java.awt.geom.RoundRectangle2D;public class XJButton extends JButton implements MouseListener {    private String text;    private Color bacNormal;    private Color foreGround;    private Color bacClick;    private Shape shape = null;    private int height ;     private boolean isEntered ;    public XJButton(String text) {        this.text = text;        bacNormal = new Color(0xFFB6C1);        foreGround = new Color(0x000000);        bacClick = Color.GRAY;        setContentAreaFilled(false);        addMouseListener(this);        isEntered = false ;         height = 45 ;                     }    public void paintComponent(Graphics g) {        Graphics2D g2 = (Graphics2D)g ;         Paint oldPaint = g2.getPaint() ;         if(isEntered){        if (getModel().isArmed()) {            g2.setColor(bacClick);                    } else            g2.setColor(bacNormal);                GradientPaint p = new GradientPaint(0,0,                new Color(0xFFFFFF), 0, 2, bacNormal);        g2.setPaint(p) ;         g2.fillRoundRect(0, 0, getWidth()- 1, getHeight() /2+10, 5, 5);        GradientPaint pInigel = new GradientPaint(0,getHeight()/2-10,bacNormal,0,getHeight(),new Color(0xFFFFFF)) ;         g2.setPaint(pInigel) ;         g2.fillRoundRect(0, getHeight()/2-5, getWidth()- 1, getHeight(), 5, 5);        }        g2.setFont(new Font("方正舒体", 0, 30));        if (getModel().isArmed()) {            g2.setColor(bacNormal);        } else            if(isEntered)                g2.setColor(Color.YELLOW) ;             else            g2.setColor(foreGround);        g2.drawString(text,1, getHeight()/ 2 + 9);        g2.setPaint(oldPaint) ;         super.paintComponent(g);    }    public void paintBorder(Graphics g) {        if(isEntered){        g.setColor(Color.BLACK);        g.drawRoundRect(0, 0, getWidth()-1,getHeight()-1, 20, 20);        }    }    public boolean contains(int x, int y) {        if (shape == null || shape.getBounds().equals(getBounds())) {            shape = new RoundRectangle2D.Float(0, 0, getWidth(), getHeight(),                    20, 20);        }        return shape.contains(x, y);    }    @Override    public void mouseClicked(MouseEvent arg0) {        // TODO Auto-generated method stub            }    @Override    public void mouseEntered(MouseEvent arg0) {        // TODO Auto-generated method stub        isEntered = true ;         setCursor(new Cursor(Cursor.HAND_CURSOR)) ;         repaint() ;             }    @Override    public void mouseExited(MouseEvent arg0) {        // TODO Auto-generated method stub        isEntered = false ;         repaint() ;     }    @Override    public void mousePressed(MouseEvent arg0) {        // TODO Auto-generated method stub            }    @Override    public void mouseReleased(MouseEvent arg0) {        // TODO Auto-generated method stub            }    public int getWidth(){        return text.length()*35 ;     }    public int getHeight(){        return height ;     }    public String getString(){        return text;     }    public static void main(String[] args){        JFrame jfm = new JFrame("") ;        jfm.setSize(200,200) ;         jfm.setLocation(new Point(400,400)) ;        XJButton x = new XJButton("尝试") ;        x.setOpaque(false) ;        jfm.getContentPane().add(x) ;                 jfm.setVisible(true) ;         jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;     }}