当前页面: 开发资料首页 → Java 专题 → 椭园按钮
椭园按钮
摘要: 椭园按钮
</td>
</tr>
<tr>
<td width="440" height="2018" valign="top" class="ArticleTeitle">
此程序的运行效果图:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
public class EllipseButton extends JButton
{
// Constructor - only support this version for demo
public EllipseButton(String text)
{
super(text);
// Create the private UI for the component. The UI
// is a delegate object that handles paint requests for
// the component.
this.setUI(EllipseButtonUI.createUI(this));
// Get rid of the rectangular button border
this.setBorder(null);
// Set this so the background of the ellipse will not
// be filled in with the background color
this.setContentAreaFilled(false);
// Add a nice chunky margin to give room for the ellipse
this.setMargin(new Insets(8, 14, 8, 14));
}
// Test frame for our button
public static void main(String [] args)
{
// Put a bigger button font in the UIManager
UIManager.put("Button.font", new Font("Arial", 0, 20));
// Create a frame and add the button
JFrame frame = new JFrame();
frame.setLocation(100, 100);
frame.setSize(200, 80);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Elliptical Button");
frame.getContentPane().setLayout(new GridBagLayout());
EllipseButton button = new EllipseButton("Elliptical");
frame.getContentPane().add(button);
// Set the background to a tasteful color and show the frame
frame.getContentPane().setBackground(new Color(230, 230, 150));
frame.setVisible(true);
}
}
// This UI delegate is extended from the basic button UI that is
// the base class for all button UI delegates in all PLAFs.
class EllipseButtonUI extends BasicButtonUI
{
// Create a singleton instance to share among all instances
// of .
protected static EllipseButtonUI singleton =
new EllipseButtonUI();
protected static Stroke thickStroke = new BasicStroke(2.0f);
// Return the singleton instance for all component instances
public static ComponentUI createUI(JComponent c)
{
return singleton;
}
public void paint(Graphics g, JComponent c)
{
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Get the button's area from the component
// reference.
AbstractButton b = (AbstractButton) c;
Rectangle viewRect = new Rectangle();
viewRect.x = 0;
viewRect.y = 0;
viewRect.width = b.getWidth() - 1;
viewRect.height = b.getHeight() - 1;
// Shrink everything by a pixel so the extra pixels created
// by anti-aliasing will fit
viewRect.grow(-2, -2);
// Create a Java2D ellipse shape that is bounded by
// the button's visible rectangle
Ellipse2D ellipse = new Ellipse2D.Float();
ellipse.setFrame(viewRect.getX(), viewRect.getY(),
viewRect.getWidth(), viewRect.getHeight());
ButtonModel model = b.getModel();
boolean pressed = (model.isArmed() && model.isPressed()) ||
model.isSelected();
pressed = !pressed;
if (pressed)
{
Color background = UIManager.getColor("Button.select");
g2.setPaint(background == null ? Color.gray : background);
}
else
g2.setPaint(UIManager.getColor("control"));
// Fill the ellipse with the background color
g2.fill(ellipse);
Arc2D arc = new Arc2D.Float();
arc.setFrame(viewRect.getX(), viewRect.getY(),
viewRect.getWidth(), viewRect.getHeight());
arc.setArcType(Arc2D.OPEN);
// To draw the highlights diagonally, use this method
// to set the start and end of the arc to be the upper
// right and lower left of the bounding rectangle.
arc.setAngles(viewRect.getWidth(), 0, 0, viewRect.getHeight());
// Set the stroke to draw a thick line
g2.setStroke(thickStroke);
// Paint the upper left side of the arc. If the button
// is pressed, paint it with shadow, otherwise paint with
// highlight
g2.setPaint(pressed ?
UIManager.getColor("controlDkShadow") :
UIManager.getColor("controlHighlight"));
g2.draw(arc);
// Flip the angles so the arc represents the other
// portion of the ellipse. Paint the lower right side of
// the arc. If the button is pressed, paint it with
// highlight, otherwise paint with shadow.
arc.setAngles(0, viewRect.getHeight(), viewRect.getWidth(), 0);
g2.setPaint(pressed ?
UIManager.getColor("controlHighlight") :
UIManager.getColor("controlShadow"));
g2.draw(arc);
super.paint(g, c);
// To be a good citizen, change the rendering hints
// of the graphic context back to the default.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
// Return the 'preferred' size of the component based on the
// size of the text. Cheat a little by calling the
// superclass and tacking on enough extra space to ensure
// there is sufficient room for the elliptical border.
public Dimension getPreferredSize(JComponent c)
{
// Call the superclass
AbstractButton b = (AbstractButton) c;
Dimension dim = super.getPreferredSize(c);
// Increment the preferred height and width by the
// size of the margin
dim.height += (b.getMargin().top + b.getMargin().bottom);
dim.width += (b.getMargin().left + b.getMargin().right);
return dim;
}
}
</td>
<td width="244" valign="top" class="ArticleTeitle">
</td>
</tr>
<tr>
<td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑返回目录
前一篇: 等待提示框
后一篇: Ant实践