要从JButton派生一个自己的按钮却不能重载repaint()方法,请教各位怎么办?

aistill 2002-01-31 10:30:55
要从JButton派生一个自己的按钮,却不能重载repaint()方法,不能把按钮做成自己的样子,请教各位有什么办法吗?
...全文
131 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
gdsean 2002-01-31
怎么不可以重载?!
又没有定义成final
  • 打赏
  • 举报
回复
gdsean 2002-01-31
paintComponent()?
public方法子类都可以去重载,
不过你说不行就不行了,虽然意外
  • 打赏
  • 举报
回复
waterdragonfly 2002-01-31
是不能重载repaint()方法还是不能把按钮做成自己的样子?
  • 打赏
  • 举报
回复
skyyoung 2002-01-31
Have an ImageButton
This implementation of an ImageButton requires JDK1.1 and is a good example of the new Event architecture. This ImageButton needs 2 GIF images representing the normal and pressed state ( ). Also a method to disable the button by using a special filter is given.

[ImageButton.java] import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;

public class ImageButton extends Canvas {
protected ActionListener actionListener = null;
int w,h;
boolean clicked;
boolean down;
boolean enabled;
Image UPimage;
Image DOWNimage;
Image disabledimage;

public ImageButton(URL up_b, URL down_b) {
clicked=false;
down=false;
enabled=true;
InitImage(up_b,down_b);
setSize(w,h);
addMouseListener(new ImageButtonMouseListener());
addMouseMotionListener(new ImageButtonMouseMotionListener());
}

public void InitImage(URL up, URL down) {
MediaTracker tracker;
try {
UPimage = getToolkit().getImage(up);
DOWNimage = getToolkit().getImage(down);
tracker = new MediaTracker(this);
tracker.addImage(UPimage,0);
tracker.addImage(DOWNimage,1);
tracker.waitForAll();
}
catch (InterruptedException e) {
e.printStackTrace();
}
disabledimage=createImage(new FilteredImageSource
(UPimage.getSource(),new ImageButtonDisableFilter()));
w=UPimage.getWidth(this);
h=UPimage.getHeight(this);
}

public void paint(Graphics g) {
if (down) {
g.drawImage(DOWNimage,0,0,this);
}
else {
if (enabled) {
g.drawImage(UPimage,0,0,this);
}
else {
g.drawImage(disabledimage,0,0,this);
}
}
}

public void setEnabled(boolean b) {
enabled=b;
repaint();
}

public boolean isEnabled() {
return (enabled);
}

public void addActionListener(ActionListener l) {
actionListener =
AWTEventMulticaster.add(actionListener,l);
}
public void removeActionListener(ActionListener l) {
actionListener =
AWTEventMulticaster.remove(actionListener, l);
}

public class ImageButtonMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if ((p.x < w)&&(p.y < h)&&(p.x > 0)&&(p.y > 0)&&(enabled==true)) {
clicked=true;
down=true;
repaint();
}
}
public void mouseReleased(MouseEvent e) {
Point p = e.getPoint();
if (down) {
down=false;
repaint();
}
if ((p.x < w)&&(p.y < h)&&(p.x > 0)&&(p.y > 0)&&(clicked==true)) {
ActionEvent ae =
new ActionEvent(e.getComponent(),0,"click");
if (actionListener != null) {
actionListener.actionPerformed(ae);
}
}
clicked=false;
}
}
public class ImageButtonMouseMotionListener extends
MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
if ((p.x < w)&&(p.y < h)&&(p.x > 0)&&(p.y > 0)&&(clicked==true)) {
if (down==false) {
down=true;
repaint();
}
}
else {
if (down==true) {
down=false;
repaint();
}
}
}
}

public Dimension getPreferredSize() {
return (new Dimension(UPimage.getWidth(this),
UPimage.getHeight(this)));
}

public Dimension getMinimumSize() {
return getPreferredSize();
}

class ImageButtonDisableFilter extends RGBImageFilter {
public ImageButtonDisableFilter() {
canFilterIndexColorModel=true;
}
public int filterRGB(int x, int y, int rgb) {
return (rgb & ~0xff000000) | 0x80000000;
}
}
}



[TestImageButton.java] import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;


public class TestImageButton extends Applet
implements ActionListener,ItemListener {
ImageButton ib;
Checkbox c;

public void init() {
setLayout(new FlowLayout());
try {
ib = new ImageButton
(new URL(getCodeBase(), "Gumby.gif"),
new URL(getCodeBase(), "Gumbyblu.gif"));
c = new Checkbox("disable");
ib.addActionListener(this);
c.addItemListener(this);
add(ib);
add(c);
}
catch (Exception e) {
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == ib) System.out.println("Click ImageButton");
}

public void itemStateChanged(ItemEvent ie) {
ib.setEnabled(!ib.isEnabled());
}
}

  • 打赏
  • 举报
回复
aistill 2002-01-31
知道了,重载paintComponent()就可以了!

to gdsean(摇滚java),我试过了不行!!!!!!!!
  • 打赏
  • 举报
回复
相关推荐
发帖
非技术区

2.3w+

社区成员

Java 非技术区
社区管理员
  • 非技术区社区
加入社区
帖子事件
创建了帖子
2002-01-31 10:30
社区公告
暂无公告