java椭圆按钮怎么设置,最好举一个简单的例子,只要有窗体中一个椭圆按钮就可以,最好有注释,谢谢,好的
1个回答
2010-12-14
展开全部
椭圆按钮要重新绘制按钮才行,对你来说可能会比较难。
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class EllipseButton extends JButton {
static final long serialVersionUID=80;
public EllipseButton(String text){
super(text);
// 创建自己的UI
this.setUI(EllipseButtonUI.createUI(this));
// 取消按钮的边框
this.setBorder(null);
// 不用内容窗格的背景
this.setContentAreaFilled(false);
// 设定边缘
this.setMargin(new Insets(8, 14, 8, 14));
}
public EllipseButton(ImageIcon image){
super(image);
// 创建自己的UI
this.setUI(EllipseButtonUI.createUI(this));
// 取消按钮的边框
this.setBorder(null);
// 不用内容窗格的背景
this.setContentAreaFilled(false);
// 设定边缘
this.setMargin(new Insets(-1,-1,-1,-1));
}
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;
public class EllipseButtonUI extends BasicButtonUI {
protected static EllipseButtonUI singleton = new EllipseButtonUI();
// 创建Stroke用于画按钮
protected static Stroke thickStroke = new BasicStroke(2.0f);
public static ComponentUI createUI(JComponent c) {
return singleton;
}
public void paint(Graphics g, JComponent c) {
// 获得Graphics2D的对象,并开启抗锯齿处理
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// 得到按钮的大小
AbstractButton b = (AbstractButton) c;
Rectangle viewRect = new Rectangle();
viewRect.x = 0;
viewRect.y = 0;
viewRect.width = b.getWidth() - 1;
viewRect.height = b.getHeight() - 1;
// 缩小矩形以显示抗锯齿效果
viewRect.grow(-2, -2);
// 在按钮矩形内创建椭圆
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();
// 根据按钮按下与否设置画笔颜色
if (pressed) {
Color background = UIManager.getColor("Button.select");
g2.setPaint(background == null ? Color.gray : background);
} else
g2.setPaint(UIManager.getColor("control"));
// 填充椭圆按钮
g2.fill(ellipse);
// 根据椭圆按钮的大小来设定边框大小.
Arc2D arc = new Arc2D.Float();
arc.setFrame(viewRect.getX(), viewRect.getY(), viewRect.getWidth(),
viewRect.getHeight());
arc.setArcType(Arc2D.OPEN);
// 设定边框指定弧度的区域
arc.setAngles(viewRect.getWidth(), 0, 0, viewRect.getHeight());
g2.setStroke(thickStroke);
// 根据按钮按下与否设定画笔的颜色
g2.setPaint(pressed ? UIManager.getColor("controlDkShadow") : UIManager
.getColor("controlHighlight"));
g2.draw(arc);
arc.setAngles(0, viewRect.getHeight(), viewRect.getWidth(), 0);
g2.setPaint(pressed ? UIManager.getColor("controlHighlight")
: UIManager.getColor("controlShadow"));
g2.draw(arc);
super.paint(g, c);
// 将图形上下文恢复原来的抗锯齿属性的设置
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
public Dimension getPreferredSize(JComponent c) {
AbstractButton b = (AbstractButton) c;
Dimension dim = super.getPreferredSize(c);
// 调整高度和宽度,为了更好的显示效果
dim.height += (b.getMargin().top + b.getMargin().bottom);
dim.width += (b.getMargin().left + b.getMargin().right);
return dim;
}
}
public class Test extends JFrame {
public Test() {
EllipseButton b = new EllipseButton("按钮");
add(b);
setSize(150, 100);
}
/**
* @param args
*/
public static void main(String[] args) {
new Test().setVisible(true);
}
}
==================================
老大,我都调试过了才发出来的。无语了......
你看代码了没?
里面是3个类?你要分别创建3个类,把代码分别放到各自的类里,哎~~~~~~
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class EllipseButton extends JButton {
static final long serialVersionUID=80;
public EllipseButton(String text){
super(text);
// 创建自己的UI
this.setUI(EllipseButtonUI.createUI(this));
// 取消按钮的边框
this.setBorder(null);
// 不用内容窗格的背景
this.setContentAreaFilled(false);
// 设定边缘
this.setMargin(new Insets(8, 14, 8, 14));
}
public EllipseButton(ImageIcon image){
super(image);
// 创建自己的UI
this.setUI(EllipseButtonUI.createUI(this));
// 取消按钮的边框
this.setBorder(null);
// 不用内容窗格的背景
this.setContentAreaFilled(false);
// 设定边缘
this.setMargin(new Insets(-1,-1,-1,-1));
}
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;
public class EllipseButtonUI extends BasicButtonUI {
protected static EllipseButtonUI singleton = new EllipseButtonUI();
// 创建Stroke用于画按钮
protected static Stroke thickStroke = new BasicStroke(2.0f);
public static ComponentUI createUI(JComponent c) {
return singleton;
}
public void paint(Graphics g, JComponent c) {
// 获得Graphics2D的对象,并开启抗锯齿处理
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// 得到按钮的大小
AbstractButton b = (AbstractButton) c;
Rectangle viewRect = new Rectangle();
viewRect.x = 0;
viewRect.y = 0;
viewRect.width = b.getWidth() - 1;
viewRect.height = b.getHeight() - 1;
// 缩小矩形以显示抗锯齿效果
viewRect.grow(-2, -2);
// 在按钮矩形内创建椭圆
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();
// 根据按钮按下与否设置画笔颜色
if (pressed) {
Color background = UIManager.getColor("Button.select");
g2.setPaint(background == null ? Color.gray : background);
} else
g2.setPaint(UIManager.getColor("control"));
// 填充椭圆按钮
g2.fill(ellipse);
// 根据椭圆按钮的大小来设定边框大小.
Arc2D arc = new Arc2D.Float();
arc.setFrame(viewRect.getX(), viewRect.getY(), viewRect.getWidth(),
viewRect.getHeight());
arc.setArcType(Arc2D.OPEN);
// 设定边框指定弧度的区域
arc.setAngles(viewRect.getWidth(), 0, 0, viewRect.getHeight());
g2.setStroke(thickStroke);
// 根据按钮按下与否设定画笔的颜色
g2.setPaint(pressed ? UIManager.getColor("controlDkShadow") : UIManager
.getColor("controlHighlight"));
g2.draw(arc);
arc.setAngles(0, viewRect.getHeight(), viewRect.getWidth(), 0);
g2.setPaint(pressed ? UIManager.getColor("controlHighlight")
: UIManager.getColor("controlShadow"));
g2.draw(arc);
super.paint(g, c);
// 将图形上下文恢复原来的抗锯齿属性的设置
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
public Dimension getPreferredSize(JComponent c) {
AbstractButton b = (AbstractButton) c;
Dimension dim = super.getPreferredSize(c);
// 调整高度和宽度,为了更好的显示效果
dim.height += (b.getMargin().top + b.getMargin().bottom);
dim.width += (b.getMargin().left + b.getMargin().right);
return dim;
}
}
public class Test extends JFrame {
public Test() {
EllipseButton b = new EllipseButton("按钮");
add(b);
setSize(150, 100);
}
/**
* @param args
*/
public static void main(String[] args) {
new Test().setVisible(true);
}
}
==================================
老大,我都调试过了才发出来的。无语了......
你看代码了没?
里面是3个类?你要分别创建3个类,把代码分别放到各自的类里,哎~~~~~~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询