Results 1 to 3 of 3
  1. #1
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Unhappy JLabel not showing

    Hi, I was thinking to make a game where a button is appearing in random spots and you have to click it, and once you click it you're score goes up. But I don't know how to make the score appear in the JFrame, and I want the score to update everytime the Button is Clicked.
    Here is the code:

    Java Code:
    package clickmegame;
    
    import java.awt.Dimension;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class ClickMeGame {
        public static void main(String[] args) {
        
            //JFRAME   
            JFrame f = new JFrame("Click Me Game!");
            f.setSize(500,500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().setLayout(null);
            
            //JLABEL
            JLabel l = new JLabel("Score:"+Action.score);
            f.add(l);
            
            //JBUTTON
            JButton b = new JButton("Catch Me!");
            f.add(b);
            
            //ADDED ACTION LISTENER
            b.addActionListener(new Action());
    
            
            //GET PREFFERED SIZE -- BUTTON
            Dimension d = b.getPreferredSize();
    
            //GENERATE RANDOM NUMBER
            Random r = new Random();
            int ran = 100;
            int ran2 = 50;
            b.setBounds(ran, ran2, d.width, d.height);
    
            //SET BUTTON IN DIFFERENT LOCATION
            for(int i=0;i<10;i++){
                ran = r.nextInt(300);
                ran2 = r.nextInt(300);
                b.setBounds(ran, ran2, d.width, d.height);
                try {
                    Thread.sleep(1000);
                }catch(Exception e){}
            }
        }
    }
    Thank you

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: JLabel not showing

    - content removed, I double posted -
    Last edited by gimbal2; 03-28-2014 at 10:40 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: JLabel not showing

    You have to research action listeners. Basically you want to update the text of the label when you click the button, its as simple as that.

    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    And btw: I would do the updating of the button position in a Swing Timer, not in a sleeping for loop.

    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)


    EDIT: oops, I managed to double post.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JLabel Array not showing in JFrame
    By kkid in forum AWT / Swing
    Replies: 12
    Last Post: 04-12-2013, 10:21 PM
  2. Replies: 5
    Last Post: 12-04-2010, 12:30 AM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  4. jLabel
    By Matty in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2008, 10:22 PM
  5. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 01:32 PM

Posting Permissions

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