Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I make a Jbutton invoke a method

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I make a Jbutton invoke a method

    Hi, I'm new to Java and I am trying to write the code required for function roll to be called when rollButton is pressed and I have no idea how to do this. So I would appreciate if someone could help me. Thanks


    [CODE]mport javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Random;


    [CODE][CODE][CODE][CODE]public class Dice extends JFrame implements ActionListener {
    private JButton rollButton;
    private JLabel leftDice,rightDice;
    private Random randomGen;
    public Dice() {
    super("Dice machine");
    rollButton= new JButton("Roll!");
    leftDice=new JLabel("",JLabel.CENTER);
    rightDice=new JLabel("",JLabel.CENTER);
    Container pane=this.getContentPane();
    pane.add(leftDice,BorderLayout.NORTH);
    pane.add(rightDice,BorderLayout.CENTER);
    pane.add(rollButton,BorderLayout.SOUTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,100);
    setVisible(true);
    randomGen=new Random(); // init number generator
    roll();
    }
    private void roll() {
    // generate random numbers between 1 and 6
    int left=randomGen.nextInt(5)+1;
    int right=randomGen.nextInt(5)+1;
    leftDice.setText(""+left);
    rightDice.setText(""+right);
    }

    public void actionPerformed(ActionEvent e){
    //not sure what to write here exactly


    }
    public static void main (String[] args) {
    new Dice();
     

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How do I make a Jbutton invoke a method

    This was answered yesterday. Please don't double post on this forum.

    Regards,
    Jim

Similar Threads

  1. How do I invoke a method to an ActionListener
    By susan28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2019, 09:09 PM
  2. how can i make a jbutton to close a jframe??
    By FastFuture in forum AWT / Java Swing
    Replies: 44
    Last Post: June 2nd, 2013, 12:05 AM
  3. Timer expires invoke method
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 11th, 2011, 05:52 PM
  4. Make JTextArea accept input from JButton
    By crazed8s in forum AWT / Java Swing
    Replies: 9
    Last Post: December 5th, 2010, 08:38 PM
  5. how to invoke a method?????
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: December 2nd, 2010, 10:48 PM