[英]How to show a variable value in JLabel
我是Java编程的新手。 我想在输出窗口中显示我的变量的值,而不是在控制台视图中。 代码如下:
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);
}
}
像这样做 :
label.setText(String.valueOf(variable));
变量可以是int,float,double,long。
只需将total(您的结果变量)值传递给JLabel constrcutor即可。
JLabel myText = new JLabel("I'm a label in the window, output : "+total,
SwingConstants.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.