usr*_*und 5 java swing jbutton actionlistener imageicon
您好我正在尝试为JButton实现Action侦听器,代码如下所示:
ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
one = new JButton("",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);
Run Code Online (Sandbox Code Playgroud)
使用上面的JButton看起来很好
当我为按钮添加特定值时,例如
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);
Run Code Online (Sandbox Code Playgroud)
它看起来像下图

有什么办法可以避免这种情况并设定价值.
感谢您的帮助.
就个人而言,我会使用ActionAPI.
它将允许您定义操作命令的层次结构(如果这是您想要的)以及定义对命令的自包含响应.
你可以...
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
}
}
Run Code Online (Sandbox Code Playgroud)
然后你只需使用你的按钮......
one = new JButton(new OneAction());
one.setPreferredSize( new Dimension(78, 76));
Run Code Online (Sandbox Code Playgroud)
例如...
| 归档时间: |
|
| 查看次数: |
23874 次 |
| 最近记录: |