java关于ImageIcon的用法 怎么总是不行呢 急、急、急!!!
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();
}
}
运行不了 展开
先看看你的运行结果。你一开始这里是这样写的: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();
}
}
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();
}
}
广告 您可能关注的内容 |