2015-10-11 83 views
0

我目前在设计任务的GUI时遇到问题。虽然该项目目前的功能有一段时间,我希望它显示用户必须等待播放器,所以我想gamePanel不可见在第一个和waitingForPlayerPanel显示“等待其他球员“。未显示Java Swing JPanel

由于某种原因不幸,由于某种原因waitingForPlayerPanel未被显示,尽管在hideGamePanel中设置为可见。如果有帮助,我目前正在Ubuntu 14.04上执行此项目。

这是我写的代码片段。

public class ClientGame extends JFrame implements ActionListener, Runnable { 
    private Socket server = null; 
    private BufferedReader in = null; 
    private PrintWriter out = null; 

    private static final Color[] playerColours = { Color.GREEN, Color.RED }; 

    private JButton[][] myButtons = new JButton[3][3]; 
    private JLabel waitingForPlayer = new JLabel("Waiting for other player"); 
    private JPanel waitingForPlayerPanel = new JPanel(); 
    private JPanel gamePanel = new JPanel(); 

    ClientGame() { 
     try { 
      this.server = new Socket("127.0.0.1", 4444); 
      this.in = new BufferedReader(new InputStreamReader(this.server.getInputStream())); 
      this.out = new PrintWriter(this.server.getOutputStream(), true); 
     } catch (IOException e) { 
      JOptionPane.showMessageDialog(null, "Alert", 
        "Couldn't connect to the server.", JOptionPane.ERROR_MESSAGE); 
      System.exit(-1); 
     } 
    } 

    public void initUI() { 
     this.waitingForPlayerPanel.setLayout(new BorderLayout()); 
     this.waitingForPlayerPanel.setBackground(Color.white); 
     this.waitingForPlayerPanel.add("Center", this.waitingForPlayer); 
     this.getContentPane().add(this.waitingForPlayerPanel); 

     this.gamePanel.setLayout(new GridLayout(3,3)); 
     this.gamePanel.setSize(450,450); 
     this.gamePanel.setBackground(Color.white); 
     this.getContentPane().add(this.gamePanel); 
     for(int i=0;i<3;i++) { 
      for(int j=0;j<3;j++) { 
       this.myButtons[i][j] = new JButton(); 
       this.myButtons[i][j].setPreferredSize(new Dimension(150, 150)); 
       this.myButtons[i][j].addActionListener(this); 
       this.gamePanel.add(this.myButtons[i][j]); 
      } 
     } 

     this.hideGamePanel(); 

     this.setVisible(true); 

     WindowListener l = new WindowAdapter() { 
      @Override 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }; 

     this.addWindowListener(l); 
    } 

    private void hideGamePanel() { 
     this.gamePanel.setVisible(false); 
     this.waitingForPlayerPanel.setVisible(true); 
     this.pack(); 
    } 

    private void showGamePanel() { 
     this.gamePanel.setVisible(true); 
     this.waitingForPlayerPanel.setVisible(false); 
     this.pack(); 
    } 
    // ... 
} 
+0

嗯,我觉得我学习更多在这里比我可以指导,但你需要'this.'上?你不应该试试'super()'。 –

+0

我看不到你在做什么,我应该在哪里使用'super()。'? – PurityLake

+1

使用'CardLayout'在面板之间切换 –

回答

1

问题解决了使用CardLayout

public class ClientGame extends JFrame implements ActionListener, Runnable { 
    private Socket server = null; 
    private BufferedReader in = null; 
    private PrintWriter out = null; 

    // IDs for CardLayout 
    private static final String WAITING_PANEL_ID = "waiting"; 
    private static final String GAME_PANEL_ID = "game"; 

    private JButton[][] myButtons = new JButton[3][3]; 
    private JLabel waitingForPlayer = new JLabel("Waiting for other player"); 
    private JPanel cardPanel = new JPanel(); 
    private JPanel waitingForPlayerPanel = new JPanel(); 
    private JPanel gamePanel = new JPanel(); 
    private int squares = 9; 

    protected boolean isTurn = false; 

    ClientGame() { 
     try { 
      this.server = new Socket("127.0.0.1", 4444); 
      this.in = new BufferedReader(new InputStreamReader(this.server.getInputStream())); 
      this.out = new PrintWriter(this.server.getOutputStream(), true); 
     } catch (IOException e) { 
      JOptionPane.showMessageDialog(null, "Alert", 
        "Couldn't connect to the server.", JOptionPane.ERROR_MESSAGE); 
      System.exit(-1); 
     } 
    } 

    public void initUI() { 
     this.cardPanel.setLayout(new CardLayout()); 

     this.waitingForPlayerPanel.setLayout(new BorderLayout()); 
     this.waitingForPlayerPanel.setBackground(Color.white); 
     this.waitingForPlayerPanel.add(BorderLayout.CENTER, this.waitingForPlayer); 
     this.cardPanel.add(this.waitingForPlayerPanel, WAITING_PANEL_ID); 

     this.gamePanel.setLayout(new GridLayout(3,3)); 
     this.gamePanel.setSize(450,450); 
     this.gamePanel.setBackground(Color.white); 
     this.cardPanel.add(this.gamePanel, GAME_PANEL_ID); 
     for(int i=0;i<3;i++) { 
      for(int j=0;j<3;j++) { 
       this.myButtons[i][j] = new JButton(); 
       this.myButtons[i][j].setPreferredSize(new Dimension(150, 150)); 
       this.myButtons[i][j].addActionListener(this); 
       this.gamePanel.add(this.myButtons[i][j]); 
      } 
     } 

     this.getContentPane().add(this.cardPanel); 

     this.hideGamePanel(); 

     this.setVisible(true); 

     WindowListener l = new WindowAdapter() { 
      @Override 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }; 

     this.addWindowListener(l); 
    } 

    // changes made here 
    private void hideGamePanel() { 
     CardLayout cl = (CardLayout)this.cardPanel.getLayout(); 
     cl.show(this.cardPanel, WAITING_PANEL_ID); 
     this.pack(); 
    } 

    // changes made here 
    private void showGamePanel() { 
     CardLayout cl = (CardLayout)this.cardPanel.getLayout(); 
     cl.show(this.cardPanel, GAME_PANEL_ID); 
     this.pack(); 
    } 
}