import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*;import javax.swing.border.*;
public class Myinterface {
public static void main(String[] args) {
Myframe f=new Myframe();
}}class Myframe extends JFrame  {
JButton about=new JButton("关于");

public static final int row=8;
public static final int col=8;
public Myframe(){
this.setBounds(300, 100, 800, 600);
this.setTitle("连连看");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Border border = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
        new Color(45, 92, 162),
        new Color(43, 66, 97),
        new Color(45, 92, 162),
        new Color(84, 123, 200));
JPanel toolBar=new JPanel();
toolBar.setPreferredSize(new Dimension(800,40));
toolBar.setBorder(border);
this.getContentPane().add(toolBar,BorderLayout.NORTH);
JPanel ationPanel=new JPanel();
ationPanel.setPreferredSize(new Dimension(660,700));
ationPanel.setBorder(border);
this.getContentPane().add(ationPanel,BorderLayout.CENTER);
JPanel controlPanel=new JPanel();
controlPanel.setPreferredSize(new Dimension(140,700));
controlPanel.setBorder(border);
this.getContentPane().add(controlPanel,BorderLayout.EAST);

toolBar.add(about);
about.addActionListener(new Monintor());

ationPanel.setLayout(new GridLayout(row,col));
JButton [] dots=new JButton[row*col];
for(int i=0;i<row*col;i++)

{
dots[i]=new JButton("fei");
ationPanel.add(dots[i]);


}

      }



class Monintor implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource() == about) {
JOptionPane.showInternalMessageDialog(about, "information", "information", JOptionPane.INFORMATION_MESSAGE);


}
}

}
}
请问这段代码哪有问题想在ationPanel里显示8行8列“fei”按钮,可运行起来总是随机出按钮?请问错在哪里谢谢了!
                     ationPanel.setLayout(new GridLayout(row,col));
         JButton [] dots=new JButton[row*col];                    for(int i=0;i<row*col;i++)

{
dots[i]=new JButton("fei");
ationPanel.add(dots[i]);


}

解决方案 »

  1.   

    忘了说了。我的JDK 是 1.6的。
      

  2.   

    我把所有Button做了一个标记:
    dots[ i ] = new JButton ( "fei"+i );然后我跑了不下20遍了,都很正常。
      

  3.   

    终于找到你说的情况了。
    一定要连续点然后CPU占用100%后,他就处理不了了。这个时候就会出现你的问题。
    所以结论是:
    你在循环代码最后增加this.validate (); 强制刷新界面。问题就解决了。
    ---------------------------------------------
    for ( int i = 0; i < row * col ; i++ ) {
    dots[ i ] = new JButton ( "fei"+i );
    ationPanel.add ( dots[ i ] );
    System.out.println ( i ); }
    this.validate ();
      

  4.   

    估计是因为我的CPU太强了,所以比较难出现你的情况啊。
    哈哈哈哈,意淫一下。