如何生成一个带圆角的button 同意楼上做法,不过如果改写UI的话可能更好一些(功能更强大,但较复杂) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 谢谢楼上的兄弟可不可以说的具体些我比较菜 下面这段代码就是一个椭圆按钮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 { public EllipseButton(String text) { super(text); this.setUI(EllipseButtonUI.createUI(this)); this.setBorder(null); this.setContentAreaFilled(false); this.setMargin(new Insets(8, 14, 8, 14)); } public static void main(String [] args) { UIManager.put("Button.font", new Font("Arial", 0, 20)); 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); frame.getContentPane().setBackground(new Color(230, 230, 150)); frame.setVisible(true); }}class EllipseButtonUI extends BasicButtonUI { protected static EllipseButtonUI singleton = new EllipseButtonUI(); protected static Stroke thickStroke = new BasicStroke(2.0f); 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); 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(); pressed = !pressed; 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); retur 怎么java编程实现网络邻居的功能 这算不算网上银行可能的安全漏洞??? 有关RCP工程中 打开xml文件的问题 如何实现qq头像的跳动? 能把某个对象强转为接口类型吗?比如.. QQ靚号,不要钱啦!!! 由jpg生成avi的问题 问个低级问题(==和equals)别笑我。真的不懂啊! 轻松拿分(关于JDO的问题) JAVA在中国的悲哀 谁用过lotus screencam做过演示盘?或者什么工具作演示盘比较好 web通过jdbc连接到unix下在的数据库,在unix下....?
可不可以说的具体些
我比较菜
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 {
public EllipseButton(String text) {
super(text);
this.setUI(EllipseButtonUI.createUI(this));
this.setBorder(null);
this.setContentAreaFilled(false);
this.setMargin(new Insets(8, 14, 8, 14));
}
public static void main(String [] args) {
UIManager.put("Button.font", new Font("Arial", 0, 20));
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);
frame.getContentPane().setBackground(new Color(230, 230, 150));
frame.setVisible(true);
}
}
class EllipseButtonUI extends BasicButtonUI {
protected static EllipseButtonUI singleton = new EllipseButtonUI();
protected static Stroke thickStroke = new BasicStroke(2.0f);
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);
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();
pressed = !pressed;
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);
retur