如何画一个上面2个角带圆弧的JButton?

jplayer 2006-06-20 04:52:34
我只要JButton的上面2个角带圆弧,下面两个还是垂直的;
请问该怎么搞定这个问题?
...全文
408 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
datalover 2006-07-07
  • 打赏
  • 举报
回复
mark
syhan 2006-07-07
  • 打赏
  • 举报
回复
继承jbutton
Preamble 2006-07-06
  • 打赏
  • 举报
回复

必须修改 jbutton的外观 paint方法了,不过,本人是画一张带圆弧的button图片,贴在
jbutton上的。
xiangaxiang 2006-06-28
  • 打赏
  • 举报
回复
这样应该可以吧?
xiangaxiang 2006-06-28
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.net.*;

public class RoundButton
extends JButton
{

public RoundButton(String label)
{
super(label);
Dimension size = getPreferredSize();
setPreferredSize(size);
setContentAreaFilled(false);
}

protected void paintComponent(Graphics g)
{
System.out.println("paint componet");
if (getModel().isArmed())
{
System.out.println("true");
g.setColor(getBackground().darker());
}
else
{
g.setColor(getBackground());
}

int w = getSize().width;
int h = getSize().height;
int d = 50;
int r = d / 2;

g.fillArc(0,0,d,d,180,-90);
g.fillArc(w - d - 2, 0, d, d, 0, 90);
g.fillRect(r,0,w-d,r);
g.fillRect(0,r,w,h-r);
// This call will paint the label and the focus rectangle.
super.paintComponent(g);
}

protected void paintBorder(Graphics g)
{
int w = getSize().width;
int h = getSize().height;
int d = 50;
int r = d / 2;

g.setColor(this.getBackground().brighter());
g.drawLine(1, r, 1, h);
g.drawArc(1, 1, d, d, 180, -90);
g.drawLine(r, 1, w - r, 1);
g.drawArc(w - d - 1, 1, d, d, 0, 90);
g.drawLine(w - 1, r, w - 1, h);
g.drawLine(0, h - 1, w - 1, h - 1);

g.setColor(this.getBackground().darker());
g.drawLine(0, r, 0, h - 2);
g.drawArc(0, 0, d, d, 180, -90);
g.drawLine(r, 0, w - r, 0);
g.drawArc(w - d - 2, 0, d, d, 0, 90);
g.drawLine(w - 2, r, w - 2, h - 2);
g.drawLine(0, h - 2, w - 2, h - 2);
}


public static void main(String[] args)
{
JButton button = new RoundButton("圆形按钮");

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setTitle("round button");
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(300, 200);
frame.setVisible(true);
frame.setTitle("123");

}



// // Hit detection.
// Shape shape;
// public boolean contains(int x, int y)
// {
// System.out.println("contains");
// // 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);
// }

}
java_augur 2006-06-26
  • 打赏
  • 举报
回复
真难看!
jorneyR 2006-06-24
  • 打赏
  • 举报
回复
注:上面的代码是可以直接运行的.
jorneyR 2006-06-24
  • 打赏
  • 举报
回复
这是一个圆形按扭的程序,你可以参考一下,就很容易实现你的要求.

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);
}
}
plane10598 2006-06-23
  • 打赏
  • 举报
回复
当然需要组合的画出那个图形!
plane10598 2006-06-23
  • 打赏
  • 举报
回复
重写paint方法!!当然得调用超类的!paintcompent方法!
GALFORDD 2006-06-22
  • 打赏
  • 举报
回复
用java新加出来的皮肤吧 LookAndFell
xiangaxiang 2006-06-22
  • 打赏
  • 举报
回复
如果按钮的大小都是固定的那用几副图片就可以搞定了,否则的话就用UI外观,可能比较麻烦了。
stamp80 2006-06-20
  • 打赏
  • 举报
回复
我一直纳闷为什么lz的需求这么怪异,不禁怀疑是我老了,还是lz太有创新思维了。这个我想只有修改button的实现了

62,623

社区成员

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

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