2013-11-28 99 views
0

我无法让GUI包含我的任何JButton或JTextField。我有两个类包含我的主要方法和框架构造函数的一个“SnowballFight”类。然后我也有“GameBoard”,它设置我的GUI。我的问题是我的GUI出现,但显示为空。Java GUI似乎是空白的(swing + awt)

“打雪仗” 类:

package snowballfight; 
import java.awt.BorderLayout; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JTextField; 
import javax.swing.JPanel; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class SnowballFight extends JFrame implements ActionListener{ 
    public SnowballFight(){ 
     setLayout(new BorderLayout(1,2)); 
    } 
    public static void main(String[] args) { 
     SnowballFight frame = new SnowballFight(); 
     GameBoard game = new GameBoard(frame); 
    } 
    public void actionPerformed(ActionEvent event) { 
    } 
} 

“游戏键盘” 类:

package snowballfight; 
import java.awt.BorderLayout; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JTextField; 
import javax.swing.JPanel; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class GameBoard extends JFrame implements ActionListener{ 
    private JButton[][] game = new JButton[10][10]; 
    private JTextField display = new JTextField("Welcome to the SnowBall fight!"); 
    private Opponent[] opponents = new Opponent[4]; 
    public GameBoard(SnowballFight frame){ 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridLayout(10, 10)); 
     for (int row = 0; row < game.length; row++) { 
      for (int col = 0; col < game[row].length; col++) { 
       game[row][col] = new JButton(); 
       game[row][col].setBackground(Color.gray); 
       game[row][col].addActionListener(this); 
       panel.add(game[row][col]); 
      } 
     } 
     add(display, BorderLayout.NORTH); 
     add(panel, BorderLayout.SOUTH); 
     frame.setTitle("Snowball Fight"); 
     frame.setSize(200, 150); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
    public boolean isGameOver(){ 
     for (int opponent = 0; opponent < opponents.length; opponent++) { 
      if(opponents[opponent].isSoaked() == false){ 
       return false; 
      } 
     } 
     return true; 
    } 
    public void actionPerformed(ActionEvent event) { 
    } 
} 
+2

边注:除非你有一个很好的理由,不要扩展'JFrame'。在你的班级中只需要一个“JFrame”字段。青睐继承。 –

回答

1

不知道为什么有两个帧的延长JFrame,但我觉得你将displaypanel添加到错误的帧。

尝试改变:

add(display, BorderLayout.NORTH); 
    add(panel, BorderLayout.SOUTH); 

到:

frame.add(display, BorderLayout.NORTH); 
    frame.add(panel, BorderLayout.SOUTH); 
+0

你修好了!非常感谢!你是什​​么意思2帧?我有snowballfight框架和GameBoard构造函数采用该框架来添加更多。 –

+0

@DigitalSauce欢迎您! 'GameBoard'不需要扩展'JFrame'。保留一帧。不需要两个。看起来像“SnowballFight”根本不需要。 – tenorsax

+0

我将在以后增加更多的功能,以便snowballFight,但我现在明白为什么gameboard不必扩展JFrame。再次感谢你的帮助。 –

1

你永远不会让GameBoardJFrame可见

game.setVisible(true); 

一些注意事项:

+0

什么? 'game'是一个'JButton [] []''。 *你永远不会让GameBoard JFrame可见*实际上,他做到了:'frame.setVisible(true);' –

+0

在'SnowballFight'游戏是扩展JFrame的类,这个类可以完全消除,其中包含_nothing_'游戏'的可见是包含组件的框架:) – Reimeus

+0

嗯,我明白了,对吧:P –