使用Java JButton中的设置Icon bug1 = new ImageIcon(……)为何无法将图片导入?0
zxp40522013.12.24浏览219次分享举报
///以下程序分别在不同的.java文件中,编译没错,但是一链接就会产生以下错误
/
当我把Icon bug1 = new ImageIcon( getClass().getResource("start.png"));
button1 = new JButton(bug1);
button1.setRolloverIcon(bug1);
注释掉,换成
button1 = new JButton("Start");时错误消失了,程序可以正常的显示窗口,也就是说我写的程序无法导入正常图片,求帮助~
PS:我已经将图片start.png放入src文件夹下
又PS:我用的是Eclipse编译器
///
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutTwo extends JFrame{
private JButton button1;
//private JButton button2;
public LayoutTwo() {
super("Botton");
setLayout( new FlowLayout() );
Icon bug1 = new ImageIcon( getClass().getResource("start.png"));
button1 = new JButton(bug1);
button1.setRolloverIcon(bug1);
add( button1 );
ButtonHandler handler = new ButtonHandler();
button1.addActionListener(handler);
}
private class ButtonHandler implements ActionListener{
public void actionPerformed( ActionEvent event){
JOptionPane.showMessageDialog(LayoutTwo.this, String.format("LET'S GO",
event.getActionCommand()));
}
}
}
//
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
LayoutTwo two = new LayoutTwo();
two.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
two.setSize(300, 200);
two.setVisible(true);
}
}