用eclipse做拼图

ljm19891118 2009-11-10 05:58:53
代码如下
感觉接口的类都没有用上
大家帮忙看看
做个拼图

package penzi;

import javax.swing.border.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class MedleyGame extends JFrame{
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=clickButton.getName();
char clickRow=clickName.charAt(0);
char clickCol=clickName.charAt(1);

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++);
button.setIcon(new ImageIcon(stochasticOrder[row][col]));
if(stochasticOrder[row][col].equals("img/00.jpg"));
emptyButton=button;
}
}
}
}

public MedleyGame(){


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,TitledBorder.DEFAULT_POSITION,null,null ));
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel,BorderLayout.NORTH);
final JLabel madelLabel=new JLabel();
madelLabel.setIcon(new ImageIcon("盆子.jpg"));
topPanel.add(madelLabel,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"))
centerPanel.add(button);
}
}

}


private JPanel centerPanel;
private JButton emptyButton;
public static void main(String args[]) {



try{
MedleyGame frame=new MedleyGame();

frame.setVisible(true);

}catch(Exception e){
e.printStackTrace();
}

}




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(exactnessOrder[r][c]!=null) {
stochasticOrder[row][col]=exactnessOrder[r][c];
exactnessOrder[r][c]=null;
}
}
}
}
return stochasticOrder;
}




}







...全文
80 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧