我做了一个RoundButton的圆型按钮,但是不知道用什么事件,点击这个圆按钮让它变成另外形状,比如方形!希望高人指点~
解决方案 »
- java如何随机产生a-z之间的字母
- 寫一個自動生成文本框的SWT界面的java(有圖),還未完成,請幫忙
- 【急,在线等】如何在JTable中 让重写了渲染器的列,也能是像没写渲染器的列那样 单击选中,双击修改??(现在我这个成了单击就进去修改了)
- 哪位大虾用过JavaMail,来帮小弟找找错了
- 为什么我的界面点"x"后程序会死掉?
- 关于StringBuffer的一个问题
- 我现在要生成一个29位的数字
- 用GregorianCalendar 类怎样得到本周星期一是几月几号
- java菜鸟求助
- jaxp中如何指定分析器?
- 救命啊 为什么vista安装tomcat出问题 有什么解决方法
- 怎么获取获取对象属性?
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);
}
}
if(e.getActionCommand().equals("")){
}
}
里面的事件写下,不胜感激!
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class MyBtn extends JButton { private static int style = 1; public MyBtn() {
setContentAreaFilled(false);
} public static void main(String args[]) {
JFrame j = new JFrame();
j.getContentPane().setLayout(null);
MyBtn mb = new MyBtn();
mb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (style == 1) {
style = 0;
} else {
style = 1;
}
}
});
mb.setText("btn");
mb.setBounds(50, 50, 50, 50);
mb.setFont(new Font("", 0, 10));
j.getContentPane().add(mb);
j.setSize(new Dimension(400, 300));
j.setDefaultCloseOperation(3);
j.setVisible(true);
} protected void paintComponent(Graphics g) {
g.setColor(Color.lightGray);
if (style == 1) {
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
} else {
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
}
super.paintComponent(g);
} protected void paintBorder(Graphics g) {
g.setColor(Color.DARK_GRAY);
if (style == 1) {
g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
} else {
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
}
}
}