2013-05-10 69 views
0

我有一个Jframe,其中有一些面板作为实例变量,其中一个面板是网格板(我正在实施动作线游戏)。我的游戏结束后,我有一个“再次播放”按钮,我想重新初始化我的电路板面板。我尝试了很多东西,例如从内容窗格中删除我的面板并重新初始化它,但目前为止没有任何工作。下面是一些我尝试过的事情(我没有尝试一次全部)在按下按钮后重新初始化我的JPanel

public class Frame extends JFrame implements MouseListener{ 

JLabel l = new JLabel(); 
Panel1 Boards; 
Panel2 newGame; 
Panel3 winner; 
Point lastCheckerSelected; 
Board game = new Board(); 
    public Frame() { 
    setResizable(false); 
    setTitle("Lines Of Action"); 
    setBounds(290, 350, 1000, 700); 
    setLayout(null); 
    winner=new Panel3(); 
    winner.playAgain.addMouseListener(this); 
    getContentPane().add(winner); 

    Boards= new Panel1(); 

    getContentPane().add(Boards); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    l.setIcon(new ImageIcon(
      "E:\\background0213.jpg")); 
    l.setBounds(0 ,0 , 1000 , 700); 
    getContentPane().add(l); 
    validate(); 
    newGame=new Panel2(); 
    newGame.b.addMouseListener(this); 
    getContentPane().add(newGame); 

    for(int i=0;i<8;i++){ 
     for(int j=0;j<8;j++){ 
      Boards.x[i][j].addMouseListener(this); 
      Boards.y[i][j].addMouseListener(this); 
     } 
    } 

} 
public void mouseClicked(MouseEvent e) { 
    if(e.getSource().equals(newGame.b)) { 
      Boards.setVisible(true); 
      newGame.setVisible(false); 
      game=new Board(); 
    } 
if(this.game.getWinner()==1) { 
    winner.setVisible(true); 
    winner.whiteWins.setVisible(true); 
} 
if(this.game.getWinner()==2) { 
    winner.setVisible(true); 
    winner.greywins.setVisible(true); 
} 
if(e.getSource().equals(winner.playAgain)) { 
      //this.getContentPane().remove(Boards); 
     // this.game= new Board();           
     // Boards = new Panel1();  
     // this.getContentPane().add(Boards); 
      //Boards.setVisible(true); 
     // validate(); 
     // Boards.repaint(); 


     } 
} 

public static void main(String[] args) { 
    Frame frame = new Frame(); 
    frame.setVisible(true); 



} 

我还是不能让我的新面板出现(从内容窗格中删除的委员会小组都会使它消失,这是很好,但新一不出现) 这里是我的面板= 1,包含的板

public class Panel1 extends JPanel { 
JButton[][] x = new JButton[8][8]; 
JButton[][] y=new JButton[8][8]; 
Graphics g; 
public Panel1() { 
    setBounds(0, 30 ,400 ,400); 
    setLayout(new GridLayout(8, 8)); 
    setOpaque(false); 
    for (int i = 0; i < 8; i++) { 
     for (int j = 0; j < 8; j++) { 
      x[i][j] = new JButton(); 
      x[i][j].setSize(50, 50); 
      if ((i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0)) { 
       x[i][j].setBackground(Color.DARK_GRAY.darker()); 
      // x[i][j].setBackground(new Color(111,89,81,150)); 
      } 
      else { 
       // Color.OPAQUE = 2; 
       x[i][j].setBackground(Color.red.darker().darker()); 
      // x[i][j].setBackground(new Color(223,37,32,150)); 
      } 

      x[i][j].setEnabled(false); 
      add(x[i][j]); 
     } 
    } 
for(int i = 0; i < 8 ;i++){ 
    for(int j=0;j < 8;j++){ 
     y[i][j]=new JButton(); 
    x[i][j].add(y[i][j]); 
    // y[i][j].setSize(100,100); 

    // y[i][j].setEnabled(false); 
    // y[i][j].setOpaque(false); 
    // y[i][j].setContentAreaFilled(false); 
//  y[i][j].setBorderPainted(false); 
     y[i][j].setVisible(false); 

    } 
} 
for(int i=1;i<7;i++){ 
// y[0][i].setOpaque(true); 
    y[0][i].setBackground(Color.white); 
    y[0][i].setEnabled(true); 
    y[0][i].setVisible(true); 
// y[7][i].setOpaque(true); 
    y[7][i].setBackground(Color.white); 
    y[7][i].setEnabled(true); 
    y[7][i].setVisible(true); 

} 
for(int i=1;i<7;i++){ 
// y[i][0].setOpaque(true); 
    y[i][0].setEnabled(true); 
    y[i][0].setBackground(new Color(102,125,153)); 
    y[i][0].setVisible(true); 
// y[i][7].setOpaque(true); 
    y[i][7].setEnabled(true); 
    y[i][7].setBackground(new Color(102,125,153)); 
    y[i][7].setVisible(true); 
} 
// addMouseListener(this); 
    setVisible(false); 
} 




} 
+1

请您给我们更多更清晰的代码吗? – 629 2013-05-10 14:17:06

+0

当你尝试每一个会发生什么?另外,如果您尝试每个窗口并调整窗口大小,会发生什么? – wchargin 2013-05-10 14:18:04

回答

1

你也可以试试这个:

if(e.getSource().equals(winner.playAgain)) 
{ 
    Boards.removeAll(); 
    revalidate(); 
    repaint(); 
} 

我不认为你需要创建一个新的Panel1实例。

+0

这也删除了我的板子也不是我真正想要的:( – user2227557 2013-05-10 14:46:21

+0

你在那个JPanel中有什么组件? – 629 2013-05-10 14:49:54

+0

public class Panel1 JButton [] [8] [8]; – user2227557 2013-05-10 14:53:48

1

试试这四个在一起。需要查看更多的代码才能确保这项工作。

if(e.getSource().equals(winner.playAgain)) { 
    this.getContentPane().remove(Boards); 
    Boards = new Panel1(); 
    this.getContentPane().add(Boards); 
    this.invalidate(); 
    this.validate(); 
    this.repaint(); 
} 
+0

如果重新验证和验证有区别吗? 我不能键入this.revalidate,因为它说它的undefined类型框架OO,它不适用于验证:(它仍然删除旧板,但我看不到一个新的板,所以我可以再次发挥 – user2227557 2013-05-10 14:45:45

+1

你可以(重新)验证一个'JComponent',比如'JPanel',用于[示例](http://stackoverflow.com/a/5812981/230513) – trashgod 2013-05-10 14:50:18

+0

@ user2227557你正在使用'JFrame'吗?那么就没有'revalidate'。 'invalidate'然后再'验证'它有时候你需要添加'repaint'。 – johnchen902 2013-05-10 15:00:28

1

我有一个Jframe,其中有一些面板作为实例变量,其中一个面板是网格板(我正在实现动作线游戏)。 我的游戏结束后,我有一个“再次播放”按钮,我想 重新初始化我的电路板面板。我尝试了很多东西,例如从内容窗格中删除我的 面板并重新初始化它,但到目前为止没有任何工作 。

我认为CardLayout是最好的选择

0

的组件在屏幕可见后,如果您删除,并添加组件,然后你必须调用Component.repaint()Component.validate()再次调用重绘。请在您的actionPerformed()之内执行此操作playAgainButton()

相关问题