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 4 of 4

Thread: JLabel gets the size of the window when I press a button

  1. #1
    Junior Member
    Join Date
    Sep 2022
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JLabel gets the size of the window when I press a button

    Hello, I wanna create a timer in Java, starting with the run button and the label of the time, but when I press the button, the label gets the size of the window, and I don't know why, what's wrong? (I wanted upload captures, but I couldn't for some reason).
    import javax.swing.*;
    import java.awt.*;
     
    public class Temporizador extends JFrame {
        private final JLabel tiempo;
        private final JButton iniciar;
     
        public static void main(String[] args) {
            new Temporizador();
        }
     
        public Temporizador() {
            setTitle("Temporizador");
            setSize(800, 600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(false);
            setLocationRelativeTo(null);
            setVisible(true);
     
            tiempo = new JLabel();
            tiempo.setLocation(0, 30);
            tiempo.setSize(200, 60);
            tiempo.setPreferredSize(new Dimension(200, 60));
            tiempo.setOpaque(true);
            tiempo.setBackground(Color.WHITE);
            tiempo.setText("00:00:00");
     
            iniciar = new JButton();
            iniciar.setText("Correr");
            iniciar.setLocation(0, 0);
            iniciar.setSize(100, 30);
            iniciar.setPreferredSize(new Dimension(100, 30));
            iniciar.setVisible(true);
            iniciar.addActionListener((event) -> {
                tiempo.setText("00:00:01");
            });
     
            add(iniciar);
            add(tiempo);
        }
    }
    Last edited by HerlySQR; September 27th, 2022 at 06:42 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,892
    Thanks
    58
    Thanked 2,692 Times in 2,642 Posts

    Default Re: JLabel gets the size of the window when I press a button

    when I press the button, the label gets the size of the window,
    Try adding the label to a panel and then adding the panel to the frame.

    Also don't call setVisible until after the gui has been updated.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    HerlySQR (September 22nd, 2022)

  4. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JLabel gets the size of the window when I press a button

    It worked, but I can't relocate the panel, how can I do it?
    import javax.swing.*;
    import java.awt.*;
     
    public class Temporizador extends JFrame {
        private final JLabel tiempo;
        private final JButton iniciar;
     
        public static void main(String[] args) {
            new Temporizador();
        }
     
        public Temporizador() {
            setTitle("Temporizador");
            setSize(800, 600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(false);
            setLocationRelativeTo(null);
     
            tiempo = new JLabel();
            tiempo.setLocation(0, 30);
            tiempo.setSize(200, 60);
            tiempo.setPreferredSize(new Dimension(200, 60));
            tiempo.setOpaque(true);
            tiempo.setBackground(Color.WHITE);
            tiempo.setText("00:00:00");
     
            JPanel panel = new JPanel();
            panel.add(tiempo);
     
            iniciar = new JButton();
            iniciar.setText("Correr");
            iniciar.setLocation(0, 0);
            iniciar.setSize(100, 30);
            iniciar.setPreferredSize(new Dimension(100, 30));
            iniciar.setVisible(true);
            iniciar.addActionListener((event) -> {
                tiempo.setText("00:00:01");
            });
     
            add(iniciar);
            add(panel);
     
            setVisible(true);
        }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,892
    Thanks
    58
    Thanked 2,692 Times in 2,642 Posts

    Default Re: JLabel gets the size of the window when I press a button

    You can use setLocation on a component if the container it is in does not have a layout manager. Set the layout manager to null to remove it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    HerlySQR (September 22nd, 2022)

Similar Threads

  1. Continuing to create an object with the press of a button.
    By phunnydoode in forum Object Oriented Programming
    Replies: 7
    Last Post: February 18th, 2014, 12:53 PM
  2. Rezise JLabel when resizing window
    By iHank in forum AWT / Java Swing
    Replies: 1
    Last Post: December 2nd, 2013, 10:55 AM
  3. Auto Update JLabel on JButton Press?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 06:55 PM
  4. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  5. press a button, get textfield content
    By chopficaro in forum AWT / Java Swing
    Replies: 1
    Last Post: May 2nd, 2010, 12:28 PM

Tags for this Thread