Results 1 to 10 of 10
  1. #1
    Nakinsige is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    Hi am pretty new to this game but i am asking for some counsel in finishing up a project am working on that is in GUI but supposed to display different random texts when clicked. Like "You have clicked me" , "Slow down" , "You can make it " , "Life is good" . Below is the code so far it displays only one mesage when clicked.


    Java Code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class gui implements ActionListener {
     JButton button;
    
     public static void main (String [] args)
     {
        gui thing = new gui();
       thing.go();
     }
    
     public void go (){
     JFrame frame = new JFrame();
     button = new JButton("Course work 1");
    
     button.addActionListener(this);
    
     frame.getContentPane().add(button);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(500, 500);
     frame.setVisible(true);
    
    }
    
     public void actionPerformed(ActionEvent event) {
    
       button.setText("You have Clicked me");
       
    
        }
     
     
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    Quote Originally Posted by Nakinsige View Post
    Hi am pretty new to this game but i am asking for some counsel in finishing up a project am working on that is in GUI but supposed to display different random texts when clicked. Like "You have clicked me" , "Slow down" , "You can make it " , "Life is good" . Below is the code so far it displays only one mesage when clicked.


    Java Code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class gui implements ActionListener {
     JButton button;
    
     public static void main (String [] args)
     {
        gui thing = new gui();
       thing.go();
     }
    
     public void go (){
     JFrame frame = new JFrame();
     button = new JButton("Course work 1");
    
     button.addActionListener(this);
    
     frame.getContentPane().add(button);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(500, 500);
     frame.setVisible(true);
    
    }
    
     public void actionPerformed(ActionEvent event) {
    
       button.setText("You have Clicked me");
       
    
        }
     
     
    }
    A suggestion that could possibly work:

    Give your class an array of Strings that it can possibly print.
    Give the class a Random variable (java.util.Random) and initialize it.
    In your button's ActionListener use a method of the Random class to give you a random int between 0 and the size of your String array - 1, and then use this int as an index to the String array to get that corresponding String from the array and then display it.

    Make sense?
    Last edited by Fubarable; 01-07-2012 at 11:37 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    You have an instance variable button, perhaps you consider having an instance variable which is an array of Strings. Then it would just be a matter of picking an index at random and displaying the string.

    -----

    A couple of points of style: Classes in Java begin with a capital letter, like Gui. And instance variables like button should be private unless you have a reason for them not to be.

    In fact button is never referred to in your code outside of the main() method. I would declare it as a local variable in that method: you can always move it later.

  4. #4
    Nakinsige is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    Arrays is what i was thinking. Let me try that and will let you know.

  5. #5
    Masamune1059 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    i'd do something like this, but im very new to java too

    ---EDIT---

    Java Code:
    String[] random = new String[]{"You have clicked me" , "Slow down" , "You can make it " , "Life is good"};
    int i = (int) ((Math.random())*4);
    button.setText(random[i]);
    Last edited by Masamune1059; 01-08-2012 at 05:36 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    Java Code:
    int i = (int) ((Math.random())/2.5);
    2.5?

    Fubarable suggested using an instance of the Random class. You might like to consider the advantages and disadvantages.

  7. #7
    Masamune1059 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    i could be wrong but i thought Math.random() gave a double between 0.0 and 9.9recurring. So dividing this by 2.5 would give a max or 3.9999997 (or something like that) then turning that into a int would give 0, 1, 2 or 3 to fit with the size of the array. Like i said im quite new though.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    Math.random returns a value between 0 and .999...

    If you divide the resulting number by 2.5, you will always get something < 1. Casting anything < 1 to int will always give 0. You sure that should be a divide?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    java.util.Random#nextInt(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Masamune1059 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.

    doh! i got the decimal place in the wrong place, gonna edit previous post so it works right

Similar Threads

  1. Replies: 8
    Last Post: 11-05-2011, 04:20 PM
  2. Replies: 1
    Last Post: 06-22-2009, 02:39 AM
  3. [SOLVED] JButton does not display ImageIcon properly
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 03:47 AM
  4. JButton to display JTable
    By Nemesis777 in forum New To Java
    Replies: 0
    Last Post: 12-08-2008, 12:16 PM
  5. Display success message in same page when submit button is clicked
    By am17mu in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 10:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •