我是 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);
}
}