Creating a JButton


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Creating a JButton

  1. #1
    Join Date
    Jul 2004
    Posts
    1

    Creating a JButton

    hi, im trying to create a jbutton where the button will allow the user to redo the program again. this program is to roll two dice and have the sum added up and list the frequencies of the sum of 2-12 poppoing out in 36000 rolls. i have gotten the program itself completed... but i do not know how to implement a jbutton.. can someone help? below is my original code, thanks.

    import javax.swing.*; // Program uses JOptionPane
    import java.awt.*;
    import java.awt.event.*;

    public class exercise7_15 extends JApplet implements ActionListener
    {
    JButton rollButton;
    JTextArea outputArea;

    public void init()
    {

    Container container = getContentPane();

    rollButton = new JButton("Roll Again");
    rollButton.addActionListener(this);
    container.add( rollButton );

    }

    public static void main (String args[])
    {

    int frequency[] = new int[13];

    for (int roll = 1; roll <= 36000; roll++)
    ++frequency[(1 + (int)(Math.random()*6)) + (1 + (int)(Math.random()*6))];

    String output = "Dice Number\tFrequency";


    for (int DiceNumber = 0; DiceNumber < frequency.length; DiceNumber++)
    output += "\n" + DiceNumber + "\t" + frequency[DiceNumber];

    JTextArea outputArea = new JTextArea();
    outputArea = new JTextArea();
    outputArea.setText(output);

    JOptionPane.showMessageDialog(null, outputArea, "Roll Dice 36000 Times", JOptionPane.INFORMATION_MESSAGE);
    }
    public void actionPerformed(ActionEvent actionEvent)
    {


    }

    }

  2. #2
    Join Date
    May 2004
    Posts
    219
    Code:
    public void actionPerformed(ActionEvent evt)
    {
    	if (evt.getSource() == rollButton) 
    	{
    		// stuff
    	}
    }

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    ugh.. i do prefer a separate ActionEvent handler for each button, rather than a massive set of IFs to find out which button was pressed (i know that drain;s example is not thus, but.. left you the OP's imagination, it might well be)

    for the OP: try using NetBeans to make your gui.. it generates a lot of the tedious stuff for you..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    May 2004
    Posts
    219
    I was just going with what he currently had.

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    indeed, and it hence motivated me to write:

    Originally posted by cjard
    (i know that drain;s example is not thus, but.. left you the OP's imagination, it might well be)


    learnin' 'em proper' from t' start is better than 'ittin 'em wi' a big stick at t' end, eh?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center