Java – how to display variable values in jlabel

I am a novice in Java programming I want to display the value of my variable in the output window, not in the console view

import java.awt.BorderLayout;

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

public class ShowAFrame {

    public static void main(String[] args) {

        // Variables in this code
        int one = 12;
        int two = 22;
        int total = one + two;
        System.out.println(total);

        JFrame myFrame = new JFrame("Test GUI");
        myFrame.setVisible(true);
        myFrame.setBounds(300,200,700,400);
        JLabel myText = new JLabel("I'm a label in the window",SwingConstants.CENTER);
        myFrame.getContentPane().add(myText,BorderLayout.CENTER);

    }

}

Solution

Do something like this:

label.setText(String.valueOf(variable));

Variables can be int, float, double, long

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>