JButton背景图片 [英] JButton background image
本文介绍了JButton背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我正在尝试为JButton实现Action侦听器,代码如下所示:
Hi i am trying to implement Action listener for JButton and code look like following:
ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
one = new JButton("",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);
使用上面的JButton它看起来很好
Using the above JButton it looks fine
当我为按钮添加特定值时,例如
When i add specific value to button for e.g.
ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
//Check this
one = new JButton("one",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);
如下图所示
我有什么方法可以避免这种情况并设置值。
Is there any way i can avoid this and set the value too.
感谢您的帮助。
推荐答案
就个人而言,我会使用 Action
API 。
Personally, I would be using the Action
API.
它将允许您定义动作命令的层次结构(如果这是您想要的)以及定义自包含的响应命令。
It will allow you defined a hierarchy of action commands (if that's what you want) as well as define self contained response to the commands.
你可以......
public class OneAction extends AbstractAction {
public OneAction() {
ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
putValue(LARGE_ICON_KEY, imageForOne);
}
public void actionPerfomed(ActionEvent evt) {
// Action for button 1
}
}
然后您只需使用按钮...
Then you would simply use with your button...
one = new JButton(new OneAction());
one.setPreferredSize( new Dimension(78, 76));
例如......
这篇关于JButton背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文