日期:2014-05-20 浏览次数:20825 次
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); }
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) ; } }