How to update a JLabel text?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 08:14:53
AM_Hawk

Try using setText(); with your JLabel.

This will create a new jLabel and set its text.

JLabel label = new JLabel();
label.setText("____");

You will need to add this label to something like a JFrame.

If you want to quick and easy, here is the code to make a simple window with a label.

import javax.swing.JFrame;
import javax.swing.JLabel;

public class App {

  public static void main(String[] args) {
    JFrame frame = new JFrame("Swing Frame");

    JLabel label = new JLabel("This is a Swing frame", JLabel.CENTER);
    label.setText("____");  // Look familiar?  <----------

    frame.add(label);

    frame.setSize(350, 200); // width=350, height=200
    frame.setVisible(true); // Display the frame
  }

}

To update the text in a label you use label.setText("New text").

However, without seeing the code, it's hard to say why it doesn't update, as there may be other things wrong.

Gayan Liyanaarachchi
public void updatemylabel(String text){

JLabel.setText("ex."+text);

//place this method inside your Jframe class extend from javax.swing.Jframe
}

JLabel.setText("ex."+text);
super.update(this.getGraphics());

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!