import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;public class RoundButton extends JButton {
public RoundButton(String label) {
super(label);// 这些声明把按钮扩展为一个圆而不是一个椭圆。
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width,
size.height);
setPreferredSize(size);
//这个调用使JButton不画背景,而允许我们画一个圆的背景。
setContentAreaFilled(false);
}// 画圆的背景和标签
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {// 你可以选一个高亮的颜色作为圆形按钮类的属性
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1,
getSize().height-1);//这个调用会画一个标签和焦点矩形。
super.paintComponent(g);
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width-1,
getSize().height-1);
}
// 侦测点击事件
Shape shape;
public boolean contains(int x, int y) {// 如果按钮改变大小,产生一个新的形状对象。
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0,
getWidth(), getHeight());
}
return shape.contains(x, y);
}// 测试程序
public static void main(String[] args) {
// 产生一个带‘Jackpot’标签的按钮。
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.green);// 产生一个框架以显示这个按钮。
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.yellow);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(150, 150);
frame.setVisible(true);
}
}
import java.awt.geom.*;
import javax.swing.*;public class RoundButton extends JButton {
public RoundButton(String label) {
super(label);// 这些声明把按钮扩展为一个圆而不是一个椭圆。
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width,
size.height);
setPreferredSize(size);
//这个调用使JButton不画背景,而允许我们画一个圆的背景。
setContentAreaFilled(false);
}// 画圆的背景和标签
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {// 你可以选一个高亮的颜色作为圆形按钮类的属性
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1,
getSize().height-1);//这个调用会画一个标签和焦点矩形。
super.paintComponent(g);
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width-1,
getSize().height-1);
}
// 侦测点击事件
Shape shape;
public boolean contains(int x, int y) {// 如果按钮改变大小,产生一个新的形状对象。
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0,
getWidth(), getHeight());
}
return shape.contains(x, y);
}// 测试程序
public static void main(String[] args) {
// 产生一个带‘Jackpot’标签的按钮。
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.green);// 产生一个框架以显示这个按钮。
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.yellow);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(150, 150);
frame.setVisible(true);
}
}
解决方案 »
- 怎样才算熟练掌握数据结构、常用算法
- swing的一段代码,貌似是焦点的问题,求指教
- 帮我看看咋回事
- 急!!! 这几天正在做一个项目,其中有一个要求是读取服务器上的一个文本文件,不知道该怎么做,请大家给指点一下
- java小程序 那位帮忙看看 不知道错在那里
- 怎样获取某一点的颜色
- jboss-3.2.1_tomcat-4.1.24.zip 那里有下载呀!
- 有谁知道怎么在java的界面(比如一个Button)中显示非标准字符,如x²这样的东东
- 随便说几句!
- 求解:书上一个例子有一条语句不理解(附程序),欢迎讨论、在线等待…………
- 小问题请教高手:try中的语句为何不执行!!!
- 请问在java里怎么读用c写在文件里的数据结构,来着给份
参考代码:
button = new JButton(createImageIcon("buttons/b1.gif", description));
button.setPressedIcon(createImageIcon("buttons/b1p.gif", description));
button.setRolloverIcon(createImageIcon("buttons/b1r.gif", description));
button.setDisabledIcon(createImageIcon("buttons/b1d.gif", description));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setMargin(new Insets(0,0,0,0));
button.getAccessibleContext().setAccessibleName(description);
p3.add(button);
buttons.add(button);
p3.add(Box.createRigidArea(HGAP10));