从自定义按钮网格中提取JOptionPane值

时间:2013-07-05 21:33:31

标签: java swing joptionpane

每天晚上,我和妻子都和Jeopardy一起玩!我厌倦了用笔和纸保持得分。所以我正在创造一个危险!使用JOptionPane。我的目标是保存包含得分的全局静态int变量和包含球员名称的全局String变量。

代码显示一个窗口,其中包含带有美元值和播放器名称的按钮网格。单击美元值,然后单击名称;然后代码应该将选定的美元值添加到所选玩家的分数中。这是我的问题 - 我创建按钮网格窗口很好,但我无法从按钮获取值。以下是我的代码(仅针对单一危险部分):

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;
    }
}

任何和所有帮助将不胜感激!一旦我全部启动并运行,我很乐意为此处的任何其他人发布完整的完整代码,以便将其用于自己的目的。

编辑:此外,如果有人能想出一种方法,我可以在播放器名称下面的对话框中不断显示分数(我意识到这可能意味着在创建一个新的对话框之后每次点击)请告诉我。如果它非常困难,我会在每次点击后将分数打印到控制台,没什么大不了的。

0 个答案:

没有答案