[英]Images within JButton not displayed
有人可以看看这段代码并告诉我我做错了什么? 根本没有显示图像。 它们在同一个包装中。
谢谢
public class MWindow31Pic extends JFrame implements ActionListener{
private JPanel contPane = (JPanel) this.getContentPane();
private JButton button = new JButton(new ImageIcon("open.jpg"));
boolean clicked = false;
public MWindow31Pic(String title){
super(title);
this.build();
}
public void actionPerformed(ActionEvent event){
if (! clicked) {
button.setIcon(new ImageIcon("close.jpg"));
//button.setText("You clicked ME!!!!");
clicked = true;
}
else{
button.setIcon(new ImageIcon("open.jpg"));
//button.setText("Click Me");
clicked = false;
}
}
public void build(){
// adding JComponents
contPane.add(button);
button.addActionListener(this);
// JFrame settings
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(240,188);
this.setVisible(true);
}
}
您应该像这样创建ImageIcon:
new ImageIcon ( MWindow31Pic.class.getResource ( "close.jpg" ) )
因为你的方式:
new ImageIcon ( "close.jpg" )
image应位于应用程序工作目录内,但不在调用类包内。
您可能还想将图像移动到单独的文件夹中:
new ImageIcon ( MWindow31Pic.class.getResource ( "images/close.jpg" ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.