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();
}
// ...
}
嗯,我覺得我學習更多在這裏比我可以指導,但你需要'this.'上?你不應該試試'super()'。 –
我看不到你在做什麼,我應該在哪裏使用'super()。'? – PurityLake
使用'CardLayout'在面板之間切換 –