JFrame出现空白



我已经玩了很多代码,试图偶然发现这个问题,但我完全不知道我哪里出错了。只用java编程了几个月,所以对它仍然很陌生。

   package eliminationgame;
   import javax.swing.*;
   import javax.swing.JFrame;
    import java.awt.event.*;
   import java.awt.GridLayout;
   import java.awt.BorderLayout;

   public class EliminationGame extends JFrame implements ActionListener{
   // creates buttons that represent numbers to be eliminated
    JButton numberOne = new JButton("1");
    JButton numberTwo = new JButton("2");
    JButton numberThree = new JButton("3");
    JButton numberFour = new JButton("4");
    JButton numberFive = new JButton("5");
    JButton numberSix = new JButton("6");
    JButton numberSeven = new JButton("7");
    JButton numberEight = new JButton("8");
    JButton numberNine = new JButton("9");
    JButton numberTen = new JButton("10");
    JButton numberEleven = new JButton("11");
    JButton numberTwelve = new JButton("12");
    // creates buttons for rolling dice, resetting game and opening instructions
    JButton rollDice = new JButton("Roll Dice");
    JButton resetGame = new JButton("Reset");
    JButton gameInstructions = new JButton("How to Play");
    // creates array to hold previous game scores, and an integer to hold current game 
    int[] previousScores;
    int currentScore = 78;  
    // creates lables for current score, previous scores, and both dice
    JLabel displayCurrentScore = new JLabel("Current Score: " + currentScore );
    JLabel displayPreviousScores = new JLabel("Previous Scores: ");
    JLabel diceOne = new JLabel();
    JLabel diceTwo = new JLabel();
    JPanel pnlEast;
    JPanel pnlSouth;
    JPanel pnlWest;
    JPanel pnlCenter;       
    JFrame appWindow;


public void EliminationGame()
{
   // sets roll dice to be the default button
   //appWindow.getRootPane().setDefaultButton(rollDice);
   // creates components and panels
    pnlEast = new JPanel();
    pnlSouth = new JPanel();
    pnlWest = new JPanel();
    pnlCenter = new JPanel();

    pnlEast.setLayout(new GridLayout(3,3));
    pnlEast.add(numberOne).setEnabled(false);
    pnlEast.add(numberTwo).setEnabled(false);
    pnlEast.add(numberThree).setEnabled(false);
    pnlEast.add(numberFour).setEnabled(false);
    pnlEast.add(numberFive).setEnabled(false);
    pnlEast.add(numberSix).setEnabled(false);
    pnlEast.add(numberSeven).setEnabled(false);
    pnlEast.add(numberEight).setEnabled(false);
    pnlEast.add(numberNine).setEnabled(false);
    pnlEast.add(numberTen).setEnabled(false);
    pnlEast.add(numberEleven).setEnabled(false);
    pnlEast.add(numberTwelve).setEnabled(false);

    pnlSouth.setLayout(new GridLayout(1,3));
    pnlSouth.add(resetGame);
    pnlSouth.add(rollDice);
    pnlSouth.add(gameInstructions);

    pnlWest.setLayout(new GridLayout (1,3));
    pnlWest.add(displayCurrentScore);
    pnlWest.add(displayPreviousScores);

    pnlCenter.setLayout(new GridLayout (1,3));
    pnlCenter.add(diceOne);
    pnlCenter.add(diceTwo);


    // adds action listener to buttons
    numberOne.addActionListener(this);
    numberTwo.addActionListener(this);
    numberThree.addActionListener(this);
    numberFour.addActionListener(this);
    numberFive.addActionListener(this);
    numberSix.addActionListener(this);
    numberSeven.addActionListener(this);
    numberEight.addActionListener(this);
    numberNine.addActionListener(this);
    numberTen.addActionListener(this);
    numberEleven.addActionListener(this);
    numberTwelve.addActionListener(this);
    rollDice.addActionListener(this);
    gameInstructions.addActionListener(this);
    resetGame.addActionListener(this);
    appWindow = new JFrame("Elimination");
    appWindow.add(pnlSouth, BorderLayout.SOUTH);  
    appWindow.add(pnlWest, BorderLayout.WEST);
    appWindow.add(pnlCenter, BorderLayout.CENTER);
    appWindow.add(pnlEast, BorderLayout.EAST);

    appWindow.pack();
    appWindow.setVisible(true);
}


public static void main(String[] args) 
{
    EliminationGame g1 = new EliminationGame();
    g1.pack();
    g1.setVisible(true);


}
    public void actionPerformed(ActionEvent thisEvent)
{
    String strButtonName = thisEvent.getActionCommand();
    if (strButtonName.equalsIgnoreCase("numberOne"))
    {
        currentScore = currentScore - 1;
        numberOne.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberTwo"))
    {
        currentScore = currentScore - 2;
        numberTwo.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberThree"))
    {
        currentScore = currentScore - 3;
        numberThree.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberFour"))
    {
        currentScore = currentScore - 4;
        numberFour.setEnabled(false);
    }
    else
    if  (strButtonName.equalsIgnoreCase("numberFive"))
    {
        currentScore = currentScore - 5;
        numberFive.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberSix"))
    {
        currentScore = currentScore - 6;
        numberSix.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberSeven"))
    {
        currentScore = currentScore - 7;
        numberSeven.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberEight"))
    {
       currentScore = currentScore - 8;
       numberEight.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberNine"))
    {
        currentScore = currentScore - 9;
        numberNine.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberTen"))
    {
        currentScore = currentScore - 10;
        numberTen.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberEleven"))
    {
        currentScore = currentScore - 11;
        numberEleven.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("numberTwelve"))
    {
        currentScore = currentScore - 12;
        numberTwelve.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("resetGame"))
    {
        currentScore = 78;
        numberOne.setEnabled(false);
        numberTwo.setEnabled(false);
        numberThree.setEnabled(false);
        numberFour.setEnabled(false);
        numberFive.setEnabled(false);
        numberSix.setEnabled(false);
        numberSeven.setEnabled(false);
        numberEight.setEnabled(false);
        numberNine.setEnabled(false);
        numberTen.setEnabled(false);
        numberEleven.setEnabled(false);
        numberTwelve.setEnabled(false);
    }
    else
    if (strButtonName.equalsIgnoreCase("gameInstructions"))
    {
    }
    else
    if (strButtonName.equalsIgnoreCase("rollDice"))
    {
    }

     }
}

JFrame为空,因为尚未向显示的窗口中添加任何组件。相反,它们被添加到另一个JFrame但由于构造函数中的void方法而未显示。

EliminationGame 的构造函数中删除 void 关键字,以便显示框架确实包含组件

public EliminationGame() {

然后只需显示appWindow JFrame而不是上一个EliminationGame实例

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            EliminationGame g1 = new EliminationGame();
        }
    });
}

  • 为类提供构造函数
  • 初始线程

最新更新