swing 中如何改变按钮的形状,用什么方法
解决方案 »
- hibernate高手进来
- 再请大家帮看看这个JDBC连SQL SEVER报的错
- 面试未解题目系列 六....希望得到答案会什么答什么我最后整理[今天雷锋日人人皆雷锋]
- 请问如何控制一个application中所有object的事件?
- socket点对点的通迅,如何判断联接超时??
- 用java编译好的应用程序,能否运行在没有java虚拟机的机器上????
- 【统计】有多少人现在用IBM的Eclipse写java应用?????
- 一个字符串“我的文档/list_name/123342”,如何知道里面“/”有多少个?
- 看Collection API是看得我云里雾里,谁能教我说清楚道明白?分数不封顶,教会我这个笨蛋就结
- 今天第一次用Jbuilder4,给我的感觉是太烂了,光标都无法正确定位,这是什么原因?希望高手赐教!
- 图片缩略图是基于什么原理
- commons-net 处理中文文件的问题
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.net.*;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);
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) {
if (getModel().isArmed()) {
// You might want to make the highlight color
// a property of the RoundButton class.
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1, getSize().height-1); // This call will paint the label and the focus rectangle.
super.paintComponent(g);
} // Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.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.
JButton button = new RoundButton("圆形按钮");
JButton b2;
ImageIcon ic;
button.setBackground(Color.green);
// Create a frame in which to show the button.
ic = new ImageIcon("Red.GIF");
b2=new JButton(ic);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.yellow);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(b2);
frame.setSize(300, 200);
frame.setVisible(true);
}
}