java有些标签不能显示Jlabel,jLabel不会显示

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

);

}

解决方案

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...

21H7p.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值