1

每天晚上我和我的妻子都和 Jeopardy 一起玩!我厌倦了用笔和纸记分。所以我正在制造危险!与 JOptionPane。我的目标是保存包含分数的全局静态 int 变量和包含球员姓名的全局字符串变量。

该代码显示一个带有按钮网格的窗口,其中包含美元值和玩家姓名。您单击美元值,然后单击名称;然后代码应将选定的美元值添加到选定玩家的分数中。这是我的问题——我可以很好地创建按钮网格窗口,但我无法从按钮中获取值。下面是我的代码(最多只有一个危险部分):

public class Jeopardy {

private String player1;
private String player2;

private static int player1Score = 0;
private static int player1FinalBid = 0;

private static int player2Score = 0;
private static int player2FinalBid = 0;

private static int clueValue = 0;
private static String selectedPlayer;

public void play(){

    JOptionPane.showMessageDialog(null, "This... Is...\nJEOPARDY!");

    getNames();
    singleJeopardy();
    doubleJeopardy();
    finalJeopardy();
}

private void getNames(){
    player1 = JOptionPane.showInputDialog("Please enter the contestant names\nPlayer 1: ");
    if(player1.equals("")){
        player1 = "Player 1";
    }

    player2 = JOptionPane.showInputDialog("Please enter the contestant names\nPlayer 2: ");
    if(player2.equals("")){
        player2 = "Player 2";
    }
}

private void singleJeopardy() {
    JOptionPane.showMessageDialog(null,
            "Get ready for the\nJeopardy round\n\nCurrent score:\n"
                    + player1 + ": $" + player1Score + "\n" + player2 + ": $"
                    + player2Score);
    JDialog dialog = null;
    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage("Single Jeopardy Round");
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);

    JPanel moneyPanel = new JPanel();
    moneyPanel.setLayout(new GridLayout(5, 1));
    String[] moneyButtonText = { "$200", "$400", "$600", "$800", "$1000" };
    JButton[] moneyButtons = new JButton[moneyButtonText.length];
    for (int i = 0; i < moneyButtonText.length; i++) {
        moneyButtons[i] = new JButton(moneyButtonText[i]);
        moneyPanel.add(moneyButtons[i]);
    }

    JPanel contestantPanel = new JPanel();
    contestantPanel.setLayout(new GridLayout(1, 2));
    String[] contestantButtonText = { player1, player2 };
    JButton[] contestantButtons = new JButton[contestantButtonText.length];
    for (int i = 0; i < contestantButtonText.length; i++) {
        contestantButtons[i] = new JButton(contestantButtonText[i]);
        contestantPanel.add(contestantButtons[i]);
    }

    optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
    optionPane.add(contestantPanel, 1);
    optionPane.add(moneyPanel, 1);
    dialog = optionPane.createDialog(null, "Jeopardy!");
    dialog.setVisible(true);

    //I need the "clueValue" variable set to the selected dollar amount.
    //I need the "selectedPlayer" variable set to the selected player.

    if(selectedPlayer.equals(player1)){
        player1Score = player1Score + clueValue;
    }else if(selectedPlayer.equals(player2)){
        player2Score = player2Score + clueValue;
    }
}

任何和所有的帮助将不胜感激!一旦我把它全部启动并运行起来,我很乐意在这里为任何想要将其用于自己目的的人发布完整的完整代码。

编辑:另外,如果有人能想出一种方法,让我可以在玩家姓名下方的对话框中不断显示分数(我意识到这可能意味着每次点击后都会创建一个新对话框),请告诉我。如果难度很大,我会在每次点击后将分数打印到控制台,没什么大不了的。

4

0 回答 0