java关于ImageIcon的用法 怎么总是不行呢 急、急、急!!!

importjava.awt.Button;importjava.awt.GridLayout;importjava.awt.Image;importjava.awt.e... import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

class My extends JFrame implements ActionListener{
private JButton a[][]=new JButton[10][10];
private int count=0,is[]=new int[8];
public My(){
JFrame frame=new JFrame("拼图游戏");
frame.setBounds(200,200, 500,400);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(3,3));
int q=new Random().nextInt(3);
int w=new Random().nextInt(3);
String shuzi[]={"7","6","4","1","5","3","2","8"};
String zimu[]={"a.jpg","b.jpg","c.jpg","d.jpg"};
int m=0,j,t=0;
for(int i=0;i<3;i++){
for(j=0;j<3;j++)
{
if(i==q&&j==w){
a[i][j]=new JButton("9");
a[i][j].setVisible(false);
}
else
{
ImageIcon f=new ImageIcon(zimu[m++]);
a[i][j]=new JButton(shuzi[t++],f);
a[i][j].setHorizontalTextPosition(SwingConstants.CENTER);
a[i][j].setVerticalTextPosition(SwingConstants.CENTER);
a[i][j].setVisible(true);
}
a[i][j].addActionListener(this);
panel.add(a[i][j]);
}
}
frame.add(panel);
frame.setVisible(true);

}
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub

}
}

public class pintu{

public static void main(String args[]) {
My xm=new My();
}

}

运行不了
展开
 我来答
数据库130
推荐于2016-02-24
知道答主
回答量:15
采纳率:0%
帮助的人:7.5万
展开全部

先看看你的运行结果。你一开始这里是这样写的:ImageIcon f=new ImageIcon(zimu[m++]);

这个m++会发生数据越界的,改成m就行。我顺便改了你的class类名。package com.ht.test;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

class Mine extends JFrame implements ActionListener{

private JButton a[][]=new JButton[10][10];

private int count=0,is[]=new int[8];

public Mine(){

JFrame frame=new JFrame("拼图游戏");

    frame.setBounds(200,200, 500,400);

    JPanel panel=new JPanel();

panel.setLayout(new GridLayout(3,3));

int q=new Random().nextInt(3);

int w=new Random().nextInt(3);

String shuzi[]={"7","6","4","1","5","3","2","8"};

String zimu[]={"a.jpg","b.jpg","c.jpg","d.jpg"};

int m=0,j,t=0;

for(int i=0;i<3;i++){

for(j=0;j<3;j++)

{

if(i==q&&j==w){

a[i][j]=new JButton("9");

a[i][j].setVisible(false);

}

else

{

  ImageIcon f=new ImageIcon(zimu[m]);

  a[i][j]=new JButton(shuzi[t++],f);

  a[i][j].setHorizontalTextPosition(SwingConstants.CENTER); 

  a[i][j].setVerticalTextPosition(SwingConstants.CENTER); 

  a[i][j].setVisible(true);

}

 a[i][j].addActionListener(this);

 panel.add(a[i][j]);

}

}

frame.add(panel);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {

// TODO Auto-generated method stub

}

}

public class My{

   

public static void main(String args[]) {

 Mine mine=new Mine();

}

}

jaket5219999
2011-11-05 · TA获得超过194个赞
知道答主
回答量:246
采纳率:0%
帮助的人:220万
展开全部
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

class My extends JFrame implements ActionListener{
private JButton a[][]=new JButton[10][10];
private int count=0,is[]=new int[8];
public My(){
super("拼图游戏");
setBounds(200,200, 500,400);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(3,3));
int q=new Random().nextInt(3);
int w=new Random().nextInt(3);
String shuzi[]={"7","6","4","1","5","3","2","8"};
String zimu[]={"a.jpg","b.jpg","c.jpg","d.jpg","e.jpg","f.jpg","g.jpg","h.jpg"};
int m=0,t=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(i==q&&j==w){
a[i][j]=new JButton("9");
a[i][j].setVisible(false);
}else{
ImageIcon f=new ImageIcon(zimu[m++]);
a[i][j]=new JButton(shuzi[t++],f);
a[i][j].setHorizontalTextPosition(SwingConstants.CENTER);
a[i][j].setVerticalTextPosition(SwingConstants.CENTER);
a[i][j].setVisible(true);
}
a[i][j].addActionListener(this);
panel.add(a[i][j]);
}
}
add(panel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub

}
}

public class pintu{

public static void main(String args[]) {
My xm=new My();

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式