gpt4 book ai didi

java - JFrame 出现空白

转载 作者:行者123 更新时间:2023-12-02 06:44:58 26 4
gpt4 key购买 nike

为了尝试偶然发现这个问题,我已经对代码进行了大量的修改,但我完全不知道我哪里出错了。才用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();
}
});
}

阅读

关于java - JFrame 出现空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18749017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com