当前位置: 代码迷 >> Java相关 >> 这个程序错在哪?
  详细解决方案

这个程序错在哪?

热度:279   发布时间:2007-09-13 13:17:48.0
这个程序错在哪?
import javax.swing.*;
public class ImageIcons extends JFrame{
JButton [] buttons= new JButton[10];
public ImageIcons(){
super("imageicons");
setSize(350,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane= new JPanel();
ImageIcon icon= new ImageIcon("face.gif");
for(int i=0;i<10;i++){
buttons[i]= new JButton(icon);
pane.add(buttons[i]);
}
setContentPane(pane);
show();
}
public static void main(String [] args){
ImageIcons inic = new ImageIcons();
}
}
为啥显示不出十个图形,笑脸
搜索更多相关的解决方案: class  public  import  

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
package com.test;

import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;

/**
*
* @author Administrator
*/
public class TempTest extends JFrame {
public TempTest() {
setTitle("imageicons");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel jp=new JPanel();
final JButton[] buttons=new JButton[10];
Image image=this.getToolkit().getImage(this.getClass().getResource("123.jpg"));
for(int i=0;i<buttons.length;i++){
buttons[i]=new JButton(new ImageIcon(image));
jp.add(buttons[i]);
}
getContentPane().add(jp);
pack();
}

public static void main(String[] args){
new TempTest().setVisible(true);
}
}
----------------解决方案--------------------------------------------------------
对应行改为:buttion[i].seticon(new ImageIcon(this.getClass().getResource(face.gif)))
去掉该行:ImageIcon icon= new ImageIcon("face.gif");
就可以了.
----------------解决方案--------------------------------------------------------

是不是把buttons[i]= new JButton(icon);该行改为buttion[i].seticon(new ImageIcon(this.getClass().getResource(face.gif)));
怎么还出错呀


----------------解决方案--------------------------------------------------------
三搂说的是对的。
----------------解决方案--------------------------------------------------------
  相关解决方案