java imageicon问题 为什么图片不能显示 5
importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.Actio...
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[]={"C:\1.jpg","C:\2.jpg","C:\3.jpg","C:\4.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 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[]={"C:\1.jpg","C:\2.jpg","C:\3.jpg","C:\4.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();
}
} 展开
1个回答
2015-06-01
展开全部
C:\\1.jpg
.......
C:\\4.jpg
.......
C:\\4.jpg
追问
还是不行 问一下啊 图片的大小和按钮的大小需要匹配吗?
追答
路径一定要对,并且,最好提前载入好。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询