继承JButton,重绘UI调用方式:RoundButton jButton1 = new RoundButton("圆形按钮");RoundButton源代码: import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2001</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class RoundButton extends JButton { public RoundButton(String label) { super(label); // These statements enlarge the button so that it // becomes a circle rather than an oval. Dimension size = getPreferredSize(); //size.width = size.height = Math.max(size.width, // size.height); size.width=100; size.height=46; setPreferredSize(size); // This call causes the JButton not to paint // the background. // This allows us to paint a round background. setContentAreaFilled(false); } // Paint the round background and label. protected void paintComponent(Graphics g) { Graphics2D g2D=(Graphics2D)g; if (getModel().isArmed()) { if (getModel().isPressed()){ g2D.setColor(Color.darkGray); setForeground(Color.white); }else // You might want to make the highlight color // a property of the RoundButton class. g2D.setColor(Color.lightGray); setForeground(Color.black); } else { g2D.setColor(getBackground()); } g2D.fillOval(0, 0, getSize().width-1, getSize().height-1); // This call will paint the label and the // focus rectangle. super.paintComponent(g2D); } // Paint the border of the button using a simple stroke. protected void paintBorder(Graphics g) { Graphics2D g2D=(Graphics2D)g; g2D.setColor(getForeground()); //g2D.setColor(Color.blue); g2D.drawOval(0, 0,getSize().width-1, getSize().height-1); } /* // Hit detection. Shape shape; public boolean contains(int x, int y) { // If the button has changed size, // make a new shape object. if (shape == null || !shape.getBounds().equals(getBounds())) { shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); } return shape.contains(x, y); } */ // Test routine. public static void main(String[] args) { // Create a button with the label "Jackpot". JButton button = new RoundButton("Jackpot"); button.setBackground(Color.green); // Create a frame in which to show the button. JFrame frame = new JFrame(); //frame.getContentPane().setBackground(Color.magenta); frame.getContentPane().add(button); frame.getContentPane().setLayout(new FlowLayout()); frame.setSize(150, 150); frame.setVisible(true); } }
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class RoundButton extends JButton {
public RoundButton(String label) {
super(label); // These statements enlarge the button so that it
// becomes a circle rather than an oval.
Dimension size = getPreferredSize();
//size.width = size.height = Math.max(size.width,
// size.height);
size.width=100;
size.height=46;
setPreferredSize(size); // This call causes the JButton not to paint
// the background.
// This allows us to paint a round background.
setContentAreaFilled(false); } // Paint the round background and label.
protected void paintComponent(Graphics g) { Graphics2D g2D=(Graphics2D)g;
if (getModel().isArmed()) {
if (getModel().isPressed()){
g2D.setColor(Color.darkGray);
setForeground(Color.white);
}else
// You might want to make the highlight color
// a property of the RoundButton class.
g2D.setColor(Color.lightGray);
setForeground(Color.black);
} else {
g2D.setColor(getBackground()); }
g2D.fillOval(0, 0, getSize().width-1,
getSize().height-1); // This call will paint the label and the
// focus rectangle.
super.paintComponent(g2D);
} // Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
Graphics2D g2D=(Graphics2D)g;
g2D.setColor(getForeground());
//g2D.setColor(Color.blue);
g2D.drawOval(0, 0,getSize().width-1,
getSize().height-1); }
/*
// Hit detection.
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size,
// make a new shape object.
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0,
getWidth(), getHeight());
}
return shape.contains(x, y);
}
*/
// Test routine.
public static void main(String[] args) {
// Create a button with the label "Jackpot".
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.green); // Create a frame in which to show the button.
JFrame frame = new JFrame();
//frame.getContentPane().setBackground(Color.magenta);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(150, 150);
frame.setVisible(true);
}
}