Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从自定义按钮网格提取JOptionPane值_Java_Swing_Joptionpane - Fatal编程技术网

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

Java 从自定义按钮网格提取JOptionPane值,java,swing,joptionpane,Java,Swing,Joptionpane,每天晚上我和妻子都和Jeopardy一起玩!我厌倦了用笔和纸来记分。所以我在制造危险!用JOptionPane。我的目标是保存包含分数的全局静态int变量和包含球员姓名的全局字符串变量 代码显示一个窗口,其中有一个按钮网格,其中包含美元值和玩家名称。单击美元值,然后单击名称;然后,代码应将选定的美元值添加到选定玩家的分数中。这是我的问题——我创建按钮网格窗口很好,但无法从按钮中获取值。以下是我的代码(仅限于单个危险部分): 公共类危险{ 私人弦乐演奏者1; 私人弦乐演奏者2; 私有静态int p

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

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

公共类危险{
私人弦乐演奏者1;
私人弦乐演奏者2;
私有静态int player1Score=0;
私有静态int player1FinalBid=0;
私有静态int player2Score=0;
私有静态int player2FinalBid=0;
私有静态int clueValue=0;
私有静态字符串选择播放器;
公共游戏{
showMessageDialog(null,“这…是…\nJopArdy!”);
getNames();
单一危险();
双重危险();
最终危险();
}
私有void getNames(){
player1=JOptionPane.showInputDialog(“请输入参赛者姓名\n第1层:”;
如果(player1.equals(“”){
player1=“玩家1”;
}
player2=JOptionPane.showInputDialog(“请输入参赛者姓名\n第2层:”;
if(player2.等于(“”){
player2=“玩家2”;
}
}
私人无效单一危险(){
JOptionPane.showMessageDialog(null,
“为下一轮\n准备好\n\n当前分数:\n”
+player1+“:$”+player1Score+“\n”+player2+“:$”
+player2Score);
JDialog dialog=null;
JOptionPane optionPane=新的JOptionPane();
optionPane.setMessage(“单危险轮”);
optionPane.setMessageType(JOptionPane.INFORMATION\u消息);
JPanel moneyPanel=新JPanel();
moneyPanel.setLayout(新网格布局(5,1));
字符串[]moneyButtonText={“$200”、“$400”、“$600”、“$800”、“$1000”};
JButton[]moneyButtons=新JButton[moneyButtonText.length];
for(int i=0;i
任何和所有的帮助都将不胜感激!一旦我把它全部安装好并运行起来,我将很高兴为这里的任何其他人发布完整的代码,这些人希望将其用于他们自己的目的


编辑:如果有人能想出一种方法,我可以让分数不断显示在球员姓名下方的对话框中(我意识到这可能意味着每次单击后都会创建一个新对话框)请让我知道。如果这非常困难,我会在每次单击后将分数打印到控制台上,这没什么大不了的。

创建连续的对话框流并不是Swing UI的设计目的。你应该有一个顶级的JFrame不断出现,这应该是你应用程序的主要UI。看一看在这里
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;
    }
}