当前位置: 代码迷 >> Eclipse >> java拼图程序中的有关问题
  详细解决方案

java拼图程序中的有关问题

热度:10   发布时间:2016-04-23 13:46:11.0
java拼图程序中的问题
按照课本上的程序代码摘抄的,怎么还出现错误,看了好久都不懂,咋整呢,求指教,感激不尽


import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*; 
import javax.swing.border.TitledBorder;




public class MedleyGame extends JFrame{
private JPanel centerPanel;
private JButton emptyButton;
public static void main(String arg[]){
try{
MedleyGame frame=new MedleyGame();
frame.setVisible(true);
}catch (Exception e){
e.printStackTrace();
}
}
public MedleyGame() {
super();
setResizable(false);
setTitle("拼图游戏");
setBounds(100,100,370,525);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


final JPanel topPanel = new JPanel();
topPanel.setBorder(new TitledBorder(null,"",
TitledBorder.DEFAULT_JUSTIFICATION,
[color=#FF0000][/color] TitledBorder.DEFAULT_POSITION,null,null));
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel,BorderLayout.NORTH);
final JLabel modelLabel=new JLabel();
modelLabel.setIcon(new ImageIcon("img/model.jpg"));
topPanel.add(modelLabel,BorderLayout.WEST);
final JButton startButton=new JButton();
startButton.setText("下一局");
startButton.addActionListener(new StartButtonAction());
topPanel.add(startButton,BorderLayout.CENTER);



centerPanel=new JPanel();
centerPanel.setBorder(new TitledBorder(null,"",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,null,null));
centerPanel.setLayout(new GridLayout(0,5));
getContentPane().add(centerPanel,BorderLayout.CENTER);
String[][] stochasticOrder=reorder();
for(int row=0;row<5;row++){
for(int col=0;col<5;col++){
final JButton button = new JButton();
button.setName(row + "" + col);
button.setIcon(new ImageIcon(stochasticOrder[row][col]));
if(stochasticOrder[row][col].equals("img/00.jpg"))
emptyButton=button;
button.addActionListener(new ImgButtonAction());
centerPanel.add(button);

}
}
}




private String[][] reorder(){
String[][] exactnessOrder = new String[5][5];
for(int row=0;row<5;row++){
for(int col=0;col<5;col++){
exactnessOrder[row][col]= "img/" + row + col + ".jpg";
}

}
String[][] stochasticOrder = new String[5][5];
for(int row=0;row<5;row++){
for(int col=0;col<5;col++){
while (stochasticOrder[row][col]==null){
int r=(int)(Math.random()*5);
int c=(int)(Math.random()*5);
if(stochasticOrder[r][c]!=null){
stochasticOrder[row][col]=stochasticOrder[r][c];
stochasticOrder[r][c]=null;

}

}
}
}
return stochasticOrder;
}







class ImgButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String emptyName=emptyButton.getName();
char emptyRow=emptyName.charAt(0);
char emptyCol=emptyName.charAt(1);
JButton clickButton=(JButton) e.getSource();
String clickName;
char clickRow=clickName.charAt(0);
char clickCol=clickName.charAt(0);
if(Math.abs(clickRow-emptyRow)+Math.abs(clickCol-emptyCol)==1){
emptyButton.setIcon(clickButton.getIcon());
clickButton.setIcon(new ImageIcon("img/00.jpg"));
emptyButton=clickButton;
}
}
}



class StartButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String[][] stochasticOrder=reorder();
int i=0;
for(int row=0;row<5;row++){
for(int col=0;col<5;col++){
JButton button = (JButton) centerPanel.getComponent(i++);
  相关解决方案