JAVA如何制作梯形按钮?
解决方案 »
- Java 绘图问题
- 抽象类是否一定要有抽象方法
- if else 都不会写了,越学越拉到了。。。来个人看看
- 关于应用程序的路由选择问题
- 在线等...
- 请问谁知道:ArrayList中的get()方法和LinkedList中的get()方法有什么不同?
- 有谁用java开发过类似组态软件的应用软件?不知道用java开发难度大吗?
- JSpinner如何格式化输入?就是限制输入数据的范围,参与有分
- notify(),waite,老调重谈.
- 请问怎样才能把图片,mp3等文件存入SQL Server2000,要求用"java.sql.*"实现;
- java中使用Runtime.getRuntime().exec调用mysqldump的问题
- 请问把十进制数12345转为16进制数并打印, 程序该怎么写?
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
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) {
// Èç¹û°´Å¥¸Ä±äÁ˳ߴ罫ÖØд´½¨Ò»¸öShape
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) {
JButton button1 = new RoundButton("http://www.cn-java.com");
JButton button2 = new RoundButton("»¶Ó­Äã³£À´×ö¿Í");
button1.setBackground(Color.green);
button2.setBackground(Color.yellow);
// ´´½¨Ò»¸öFramÀ´ÏÔʾÕâ¸ö°´Å¥
JFrame frame = new JFrame();
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(450, 350);
frame.setVisible(true);
//²âÊÔµ¥»÷ʼþ
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("you click button1!");
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("you click button2!");
}
}); }
}
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
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);
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) {
JButton button1 = new RoundButton("http://www.cn-java.com");
JButton button2 = new RoundButton("»¶Ó­Äã³£À´×ö¿Í");
button1.setBackground(Color.green);
button2.setBackground(Color.yellow);
JFrame frame = new JFrame();
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(450, 350);
frame.setVisible(true); button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("you click button1!");
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("you click button2!");
}
}); }
}