Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:45:37+00:00 2026-06-02T00:45:37+00:00

I am making a board game and I need to action a button which

  • 0

I am making a board game and I need to action a button which operates like a dice. In my frame, I have the throw dice button and next to it is a JLabel which would show the result of the dice. The problem is that I have a coding for dice in a different class and the button in another. I have an action class which would hold the code for action listener. How can I implement it? Below is my code.

GameView Class:

 public void gui()
    {
        BorderLayout borderlayout = new BorderLayout();      
        
        //Creates the frame, set the visibility to true, sets the size and exit on close
        JFrame frame = new JFrame("Games");
        frame.setVisible(true);
        frame.setSize(600,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        //Creates the Throw Dice button
        Panel p = new Panel();
        p.setLayout(new GridLayout());
        p.add(new Button("Throw Dice"));
        p.add(new Label("Dice Draw")); //This is where I want the dice result to be shown when hit the button
        p.setBackground(new Color(156, 93, 82));
        frame.add(p, BorderLayout.SOUTH);

Dice Class:

public class Dice
{   
    //stores the dice result in the variable
    private int diceResult;

    /**
     * Constructor for objects of class Dice
     */
    public Dice()
    {
        this.diceResult = diceResult;
    }

    /**
     * Generates a random number between 1 and 5
     */
    public void randomGenerator ()
    {
        Random dice = new Random();
        diceResult = dice.nextInt(5)+1;
        System.out.println(diceResult);
    }
}

Action Class:

public class Action
{
    public void actionPerformed (ActionEvent e) 
    {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:45:39+00:00Added an answer on June 2, 2026 at 12:45 am

    In simple example like this i can recommend you to use advantages of anonymous class which implements ActionListener like this.

        BorderLayout borderlayout = new BorderLayout();      
    
        //Creates the frame, set the visibility to true, sets the size and exit on close
        JFrame frame = new JFrame("Games");
        frame.setVisible(true);
        frame.setSize(600,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
        //Creates the Throw Dice button
        Panel p = new Panel();
        p.setLayout(new GridLayout());
    
        final Dice dice = new Dice();
        Button button = new Button("Throw Dice");
        final Label label = new Label("Dice Draw");
    
        button.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                dice.randomGenerator();
                label.setText(Integer.toString(dice.getDiceResult()));
            }
        });
    
        p.add(button);
        p.add(label); //This is where I want the dice result to be shown when hit the button
        p.setBackground(new Color(156, 93, 82));
        frame.add(p, BorderLayout.SOUTH);
    

    Also you must add getDiceResult method to your Dice class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a board game and I need to put the player 1
I'm working on making a board game for chess and checkers (and a few
I'm just getting started on making a board game using Cocoa/Objective-C. My plan is
I am trying to make a board game, I have Player 1 label and
Hey everybody I'm making a memory game where it's a 4x4 board and you
I need help with a program I'm making. It's a version of Conway's Game
To learn tkinter I'm making a simple Go game program. I now would like
I am working on developing a Chess game. I want to have the board
I'm in the process of making a board game in Flash; as part of
I'm making a game with Python->PyGame->Albow and ran into a problem with board generation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.