jbutton按钮上的文本显示在椭圆中

huy8989 2009-10-07 03:38:39
开发一个自定义按钮,拓展jbutton,就是在按钮上画一个椭圆,然后在里面显示文本,这个怎么弄呢?
...全文
210 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangjian320 2012-05-07
  • 打赏
  • 举报
回复
public class MergeButton extends JButton
{

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在移动的时候,椭圆会消失???
贫僧草头 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lz12366 的回复:]
Java code

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import j……
[/Quote]


。。。这个设计真够难看。
lz12366 2009-10-08
  • 打赏
  • 举报
回复


import java.awt.Graphics;
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);

}
}
看看是不是这种效果
lz12366007 2009-10-08
  • 打赏
  • 举报
回复
顶楼上
w3329307 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lz12366 的回复:]
应该是
先 drawOral()
然后drawString()
通过设置坐标

假如圆的位置(0,0,100,100)

字的位置可以设为(10,10);
至于字的大小和属性 可以通过 设置字体就行了
字的大小别超过圆就行啊

把他们组合起来
就能实现吧

[/Quote]

和想的差不多,但就是没有试过
lz12366 2009-10-07
  • 打赏
  • 举报
回复
应该是
先 drawOral()
然后drawString()
通过设置坐标

假如圆的位置(0,0,100,100)

字的位置可以设为(10,10);
至于字的大小和属性 可以通过 设置字体就行了
字的大小别超过圆就行啊

把他们组合起来
就能实现吧
rumlee 2009-10-07
  • 打赏
  • 举报
回复
继承jbutton类,然后重写paint方法,我觉得理论上应该是可行的,不过我没有试过。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧