Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

java - jLabel won't show

I am slightly confused, I have a jFrame of which I have made in Netbeans. This jFrame has a jLabel, of which is set to setVisible(false); from the beginning. Whenever a specific method is called, I then set the jLabel to setVisible(true); and then use a timer to set it to false again after 2 seconds. Apparently it won't work and I am unable to figure out why. I am aware of the repaint(); method, but can figure out how to make that work either.

I know the actual method for setting the visibility is called, as I have set it to print a line with the current state, which it does.

My actual code is the one below.

public JFram() {
        initComponents();
        setResizable(false);
        jLabel2.setVisible(false);
    }

static void tesMethod() {
            try {
         //function that does something
            } finally {
                new JFram().showHide(); //call function which is supposed to change the vissibility of jLabel
            }
    }

    void showHide() {
            jLabel2.setVisible(true);
            System.out.println("reached show");
            new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                     jLabel2.setVisible(false);
                     System.out.println("reached timer");
                    }
                 }, 
             2000 
            );
    }

The code below here is how I tried to use the repaint(); method.

void showHide() {
            jLabel2.setVisible(true);
            jLabel2.repaint();
            System.out.println("reached show");
            new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                     jLabel2.setVisible(false);
                     jLabel2.repaint();
                     System.out.println("reached timer");
                    }
                 }, 
             2000 
            );
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think your problem lies mainly in you using a java.util.Timer instead of a javax.swing.Timer and probably you're blocking the Event Dispatch Thread (EDT).

You could try this code and compare it with yours, I also don't see where you're adding your JLabel to your frame.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ShyLabel {

    private JFrame frame;
    private JLabel label;
    private Timer timer;
    private boolean isVisible;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ShyLabel().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        String labelText = "I'm a shy label that hides every 2 seconds";

        isVisible = true;
        frame = new JFrame(getClass().getSimpleName());
        label = new JLabel(labelText);
        timer = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(isVisible ? "" : labelText);
                isVisible = !isVisible;
            }
        });

        timer.setInitialDelay(2000);
        timer.start();

        frame.add(label);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The below image is produced by the above code, however because of the time I recorded the GIF it looks really fast instead of taking 2 seconds as it should be...

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...