You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在NetBeans IDE 8.2中用JApplet Form开发掷骰子小游戏UI并输出结果

How to Display Dice Game Results in JApplet

Hey there! Great job getting the core logic of your dice game working—let's get those results showing up in your JApplet. Here's a step-by-step breakdown to hook up your code to the UI:

1. Set Up JApplet UI Components

First, you'll need to add UI elements (like labels) to display the dice values, their sum, and the special scenario. We'll do this in the init() method of your JApplet class:

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DiceGameApplet extends JApplet {
    private JLabel dice1Label;
    private JLabel dice2Label;
    private JLabel sumLabel;
    private JLabel scenarioLabel;
    private JButton rollButton;
    private Dice diceGame;
    private String[] scenarios = {
        "Lucky double! You win a free roll!",
        "Double sixes! Jackpot!",
        "Double ones? Better luck next time!",
        // Add your remaining 17 scenarios here
    };

    @Override
    public void init() {
        // Set up a simple layout for the applet
        setLayout(new FlowLayout());
        diceGame = new Dice();

        // Create UI elements to show results
        dice1Label = new JLabel("Dice 1: ");
        dice2Label = new JLabel("Dice 2: ");
        sumLabel = new JLabel("Sum: ");
        scenarioLabel = new JLabel("");
        rollButton = new JButton("Roll Dice");

        // Add click behavior to the roll button
        rollButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rollDiceAndRefreshUI();
            }
        });

        // Add all components to the applet
        add(dice1Label);
        add(dice2Label);
        add(sumLabel);
        add(scenarioLabel);
        add(rollButton);
    }
}

2. Refine Your Dice Logic to Return Results

Update your existing Dice class to have a method that returns the two dice values and their sum—this makes it easy to feed data to the UI:

import java.util.Random;

public class Dice {
    private Random random;

    public Dice() {
        random = new Random();
    }

    // Returns an array containing [dice1 value, dice2 value, total sum]
    public int[] rollDice() {
        int dice1 = random.nextInt(6) + 1;
        int dice2 = random.nextInt(6) + 1;
        int sum = dice1 + dice2;
        return new int[]{dice1, dice2, sum};
    }
}

3. Connect Logic to UI with a Refresh Method

Add this method to your DiceGameApplet class—it runs the dice roll, then updates all the labels with fresh results:

private void rollDiceAndRefreshUI() {
    int[] rollResults = diceGame.rollDice();
    int dice1 = rollResults[0];
    int dice2 = rollResults[1];
    int sum = rollResults[2];

    // Update labels with current dice values and sum
    dice1Label.setText("Dice 1: " + dice1);
    dice2Label.setText("Dice 2: " + dice2);
    sumLabel.setText("Sum: " + sum);

    // Check for doubles and show a random scenario
    if (dice1 == dice2) {
        int randomScenarioIdx = new Random().nextInt(scenarios.length);
        scenarioLabel.setText("*" + scenarios[randomScenarioIdx] + "*");
    } else {
        scenarioLabel.setText(""); // Clear scenario text if no double
    }
}

4. Run It in NetBeans

  • Create a new "Java Applet" project in NetBeans
  • Replace the default applet class with DiceGameApplet
  • Add the Dice class to the same package
  • Run the project—NetBeans will launch the Applet Viewer, and you can click the button to roll dice and see your results live!

Quick tips:

  • JLabel is perfect for displaying dynamic text like dice values—just use setText() to update it.
  • The button's ActionListener makes the game interactive, letting users roll as many times as they want.
  • Don't forget to fill out all 20 scenarios in the scenarios array to complete the feature!

内容的提问来源于stack exchange,提问作者Benny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:11:40