青飞木鸟 青飞木鸟
关注数: 0 粉丝数: 35 发帖数: 922 关注贴吧数: 0
跪求吧里的朋友们,能帮我看看麽?结果很奇怪,多谢! public class MMM { public static void main(String[] args) { String a[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14", "15","16","17","18","19","20","21","22","23","24","25","26","27"}; SrartListener Sl=new SrartListener(); show Win=new show(a,Sl); location lo=new location(Win); Sl.getslo(lo); } } package javaapplication1; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JLabel; import static javax.swing.SwingConstants.CENTER; import javax.swing.SwingUtilities; class show extends JFrame{ JLabel label[][]=new JLabel[10][10]; String name[]=new String[28]; int i,j; int locx,locy; public show(String a[],MouseListener ML) { super(); MMM.sign=false; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); name[0]="START"; if(a.length==27){ for(i=0;i<27;i++){ name[i+1]=a[i]; } } else{ System.out.println(a.length+"Wrong!结束或提示");////////////// } this.setBounds(10,10,700,700); setLayout(new GridLayout(10,10,5,5)); int num=0; System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~"); for(i=0;i<10;i++){ for(j=0;j<10;j++){ label[i][j]=new JLabel(); label[i][j].setOpaque(true); if(i==1&& j>0 && j<8){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else if(j==8 && i>0 && i<8){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else if(i==8 && j<9 && j>1){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else if(j==1 && i<9 && i>1){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else{} add(label[i][j]); } } label[1][1].addMouseListener(ML); setVisible(true); } void Setloc(int a,int b){ locx=a; locy=b; this.label[1][1].setText("Fly"); /////还有这儿。为什么他label无法操作它了呢? this.label[locx][locy].setBackground(Color.yellow); /////就这里,在location中调用嘚~~ System.out.println(a+""+b); SwingUtilities.updateComponentTreeUI (this); } } package javaapplication1; import static java.lang.Thread.sleep; /** * * @author Administrator */ public class location { int nowLocx=1,nowLocy=1; show s; // boolean infoSend=false; location(show win){ this.s=win; } void move() throws InterruptedException{ nowLocx=1;nowLocy=1; for(;MMM.sign==true;){ sleep(500); if (nowLocx==1 && nowLocy>0 && nowLocy<8){nowLocy++; s.Setloc(nowLocx,nowLocy);} else if(nowLocy==8 && nowLocx>0 && nowLocx<8){nowLocx++; s.Setloc(nowLocx,nowLocy);} else if(nowLocx==8 && nowLocy<9 && nowLocy>1){nowLocy--; s.Setloc(nowLocx,nowLocy);} else if(nowLocy==1 && nowLocx<9 && nowLocx>1){nowLocx--; s.Setloc(nowLocx,nowLocy);} else{System.out.println("Wrong!");} } } int getLocx(){ return nowLocx; } int getLocy(){ return nowLocy; } } package javaapplication1; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.logging.Level; import java.util.logging.Logger; import static javaapplication1.MMM.sign; /** * * @author Administrator */ public class SrartListener implements MouseListener { location lo; void getslo(location n){lo=n;} @Override public void mouseClicked(MouseEvent e) { if(sign==false){ sign=true; try { lo.move(); } catch (InterruptedException ex) { Logger.getLogger(SrartListener.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } 运行后点左上角的start 挣扎了一下午也没挣脱~~~难以理解是为何,跪求大神能教下麽?
此处有一大段JAVA代码,来贴吧跪求高人相助!! public class MMM { public static void main(String[] args) { String a[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14", "15","16","17","18","19","20","21","22","23","24","25","26","27"}; SrartListener Sl=new SrartListener(); show Win=new show(a,Sl); location lo=new location(Win); Sl.getslo(lo); } } package javaapplication1; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JLabel; import static javax.swing.SwingConstants.CENTER; import javax.swing.SwingUtilities; class show extends JFrame{ JLabel label[][]=new JLabel[10][10]; String name[]=new String[28]; int i,j; int locx,locy; public show(String a[],MouseListener ML) { super(); MMM.sign=false; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); name[0]="START"; if(a.length==27){ for(i=0;i<27;i++){ name[i+1]=a[i]; } } else{ System.out.println(a.length+"Wrong!结束或提示");////////////// } this.setBounds(10,10,700,700); setLayout(new GridLayout(10,10,5,5)); int num=0; System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~"); for(i=0;i<10;i++){ for(j=0;j<10;j++){ label[i][j]=new JLabel(); label[i][j].setOpaque(true); if(i==1&& j>0 && j<8){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else if(j==8 && i>0 && i<8){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else if(i==8 && j<9 && j>1){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else if(j==1 && i<9 && i>1){ label[i][j].setText(name[num++]); label[i][j].setBackground(Color.black); label[i][j].setForeground(Color.white); label[i][j].setHorizontalAlignment(CENTER); } else{} add(label[i][j]); } } label[1][1].addMouseListener(ML); setVisible(true); } void Setloc(int a,int b){ locx=a; locy=b; this.label[1][1].setText("Fly"); /////还有这儿。为什么他label无法操作它了呢? this.label[locx][locy].setBackground(Color.yellow); /////就这里 System.out.println(a+""+b); SwingUtilities.updateComponentTreeUI (this); } } package javaapplication1; import static java.lang.Thread.sleep; /** * * @author Administrator */ public class location { int nowLocx=1,nowLocy=1; show s; // boolean infoSend=false; location(show win){ this.s=win; } void move() throws InterruptedException{ nowLocx=1;nowLocy=1; for(;MMM.sign==true;){ sleep(500); if (nowLocx==1 && nowLocy>0 && nowLocy<8){nowLocy++; s.Setloc(nowLocx,nowLocy);} else if(nowLocy==8 && nowLocx>0 && nowLocx<8){nowLocx++; s.Setloc(nowLocx,nowLocy);} else if(nowLocx==8 && nowLocy<9 && nowLocy>1){nowLocy--; s.Setloc(nowLocx,nowLocy);} else if(nowLocy==1 && nowLocx<9 && nowLocx>1){nowLocx--; s.Setloc(nowLocx,nowLocy);} else{System.out.println("Wrong!");} } } int getLocx(){ return nowLocx; } int getLocy(){ return nowLocy; } } package javaapplication1; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.logging.Level; import java.util.logging.Logger; import static javaapplication1.MMM.sign; /** * * @author Administrator */ public class SrartListener implements MouseListener { location lo; void getslo(location n){lo=n;} @Override public void mouseClicked(MouseEvent e) { if(sign==false){ sign=true; try { lo.move(); } catch (InterruptedException ex) { Logger.getLogger(SrartListener.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } 不要沉哦,相信吧里肯定有大神。运行后点击start~~ 挣扎了一下午也没挣脱
1 下一页