public void mouseReleased(MouseEvent event) { thisButton.setLocation(thisButton.getLocation()); thisButton.repaint(); } }); this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { moved = true; Point p = thisButton.getLocation(); Point location = new Point(p.x+event.getX()-origin.x, p.y+event.getY()-origin.y); if (parent == null) parent = thisButton.getParent();
if (location.x<0) location.x = 0; else if (location.x+thisButton.getWidth() > parent.getWidth()) location.x = parent.getWidth()-thisButton.getWidth(); if (location.y<0) location.y = 0; else if (location.y+thisButton.getHeight() > parent.getHeight()) location.y = parent.getHeight()-thisButton.getHeight();
thisButton.setLocation(location); thisButton.repaint(); } }); } public static void main(String[] args){ JFrame jFrame = new JFrame(); Container c = jFrame.getContentPane(); MergeButton button = new MergeButton("yujian"); JPanel jp = new JPanel(); jp.add(button); jp.setBackground(Color.WHITE); c.add(jp); jFrame.setResizable(false); jFrame.setVisible(true); jFrame.setBackground(Color.WHITE); jFrame.setSize(600, 500);
先 drawOral()
然后drawString()
通过设置坐标
(
假如圆的位置(0,0,100,100)字的位置可以设为(10,10);
至于字的大小和属性 可以通过 设置字体就行了
字的大小别超过圆就行啊
)
把他们组合起来
就能实现吧
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class DrawOral
{ public static void main(String[] args)
{
new frame();
}
}
class frame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L; public frame()
{
setSize(400,400);
setLocation(400,200);
button bt=new button("测试");
bt.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(frame.this,"hah");
}});
//add(bt);
JPanel panel=new JPanel();
panel.add(bt);
add(panel,BorderLayout.NORTH);
add(new JLabel("点击圆试试"),BorderLayout.CENTER);
this.setVisible(true);
}
}
class button extends JButton
{
/**
*
*/
private static final long serialVersionUID = 1L;
public button(String str)
{
super(str);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawOval(0,0,getHeight(),getHeight());
Graphics2D g2=(Graphics2D)g;
g2.setFont(new Font("Arial",Font.ITALIC,getHeight()/2));
g2.setPaint(Color.RED);
g2.drawString("OK ",0,getHeight()/2);
}
}
看看是不是这种效果
{
public JPanel jPanel = null;
//鼠标的原始点,按按钮坐标算,就是本按钮的左上方为原点
private Point origin = new Point();
//本个按钮
MergeButton thisButton = this;
//父容器,如你将按钮放在一个面板上,面板就是按钮的父容器
Container parent = null;
private boolean moved = false;
//注意, parent不能在构造函数中初始化,因为当按钮创建的时候,他还没有放在一个容器里,
//这个时候按钮是没有父容器的。
public MergeButton ()
{
addListener();
Dimension size = getPreferredSize(); // 这些声明把按钮扩展为一个圆而不是一个椭圆。
size.width = size.height = Math.max(size.width,size.height);
this.setPreferredSize(size);
this.setContentAreaFilled(false);//这个调用使JButton不画背景,而允许我们画一个圆的背景。
} public MergeButton(String text)
{
super(text);
Dimension size = getPreferredSize(); // 这些声明把按钮扩展为一个圆而不是一个椭圆。
size.width = size.height = Math.max(size.width,size.height);
setPreferredSize(size);
setContentAreaFilled(false);//这个调用使JButton不画背景,而允许我们画一个圆的背景。
addListener();
}
protected void paintComponent(Graphics g){
String text =this.getText();
if (getModel().isArmed()) {
g.setColor(Color.lightGray); // 按钮按下时的属性
}else{
g.setColor(this.getBackground());
}
Point location = this.getLocation();
if(!moved){
System.out.println("location.y+text.length()*2+2:"+(location.y+text.length()*2+2));
g.fillOval(0,location.y+text.length()*2+2,this.getSize().width-5,(this.getSize().height)/2);
}else{
//thisButton.getX()
System.out.println("location.y:"+location.y);
// System.out.println("location.y+text.length()*2+2:"+(location.y+text.length()*2+2));
System.out.println("Move location.y+text.length()*2+2:"+(location.y+text.length()*2+2));
g.fillOval(0,location.y,this.getSize().width-5,(this.getSize().height)/2);
}
super.paintComponent(g);//这个调用会画一个标签和焦点矩形,可以在button里面写入数据
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
g.setColor(this.getForeground());
String text = this.getText();
Point location = this.getLocation();
if(!moved){
g.drawOval(0, location.y+text.length()*2+2, this.getSize().width-5,(this.getSize().height)/2);
}else{
//location.x
g.drawOval(0, location.y, this.getSize().width-5,(this.getSize().height)/2);
}
}
private void addListener()
{
//一个匿名类,实现mousePressed,当鼠标按下时,得到鼠标的坐标,
//注意,是按本按钮的坐标系来算
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
origin = event.getPoint();
}
public void mouseReleased(MouseEvent event)
{
thisButton.setLocation(thisButton.getLocation());
thisButton.repaint();
}
}); this.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
moved = true;
Point p = thisButton.getLocation();
Point location = new Point(p.x+event.getX()-origin.x, p.y+event.getY()-origin.y); if (parent == null)
parent = thisButton.getParent();
if (location.x<0)
location.x = 0;
else if (location.x+thisButton.getWidth() > parent.getWidth())
location.x = parent.getWidth()-thisButton.getWidth(); if (location.y<0)
location.y = 0;
else if (location.y+thisButton.getHeight() > parent.getHeight())
location.y = parent.getHeight()-thisButton.getHeight();
thisButton.setLocation(location);
thisButton.repaint();
}
});
}
public static void main(String[] args){
JFrame jFrame = new JFrame();
Container c = jFrame.getContentPane();
MergeButton button = new MergeButton("yujian");
JPanel jp = new JPanel();
jp.add(button);
jp.setBackground(Color.WHITE);
c.add(jp);
jFrame.setResizable(false);
jFrame.setVisible(true);
jFrame.setBackground(Color.WHITE);
jFrame.setSize(600, 500);
}
为什么这个Button在移动的时候,椭圆会消失???