我已经创建了一个图像按钮,但它没有显示图像。该文件位于src\MyPackage文件夹中。我怎样才能绘制地图呢?
这是我的密码:
jpAnnotation=new JPanel();
jpAnnotation.setLayout(new FlowLayout(FlowLayout.LEADING));
JButton btnUnderline =new JButton(new ImageIcon ("UnderlineIcon.gif"));
btnUnderline.setSize(50, 260);
btnUnderline.setAlignmentX(JButton.LEFT_ALIGNMENT);
btnUnderline.setHorizontalAlignment(JButton.LEFT);
btnUnderline.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
ActionEvent ae = new ActionEvent(bean, 0, "Underline");
bean.actionPerformed(ae);
}
});
jpAnnotation.add(btnUnderline); 发布于 2014-03-27 22:29:22
只是一个小小的代码片段:
btnUnderline.setIcon(
new ImageIcon(getClass().getResource("/path/to/UnderlineIcon.gif")));简略解释
使用此语句加载图像,您不必关心文件的正确URL,因为您会自动获得正确的URL。
这是基于从类路径而不是从文件系统路径加载资源!
发布于 2014-03-27 22:25:40
试试这个:
btnUnderline.setIcon( new ImageIcon( "C:\\YourFolder\src\MyPackage\UnderlineIcon.gif" ) );当然,如果你在使用Windows。或者,您可以将gif移动到执行代码所在的同一个目录。
https://stackoverflow.com/questions/22700488
复制相似问题