我对java很新,我试图使用JLabel显示乘法表。用户将输入两个整数。 m将是乘法应显示的次数,n将是时间表。 p =应该在m x n时显示的答案这是我的代码
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TimesTable extends JFrame
{
public TimesTable(int m, int n, int p)
{
setTitle("TimesTable");
//Container contents = getContentsPane();
//contents.setLayout(new GridLayout(0, 3, 20, 10));
//for loop to calculate the times table
for(int num = 1; num >= m; m++)
{
multiplyAnswer = numOfMultiply * multiplyBy;
Container contents = getContentsPane();
contents.setLayout(new GridLayout(0, 3, 20, 10));
contents.add(new JLabel(m));
contents.add(new JLabel("x"));
contents.add(new JLabel(n));
contents.add(new JLabel("="));
contents.add(new JLabel(p));
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args)
{
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
TimesTable theTimesTable = new TimesTable(m, n, p);
theTimesTable.setVisible(true);
}
但我一直收到错误。我该怎么办?
答案 0 :(得分:2)
您需要将int转换为字符串,因为您要查找的标签构造函数需要一个字符串。有多种方法可以做到这一点。例如,您可以使用Integer.toString:
contents.add(new JLabel(Integer.toString(m)));
或者,您可以通过将其与空字符串连接来隐式转换它,正如其他答案所示,即:
contents.add(new JLabel("" + m));
答案 1 :(得分:0)
contents.add(new JLabel(""+m));
答案 2 :(得分:0)
试试这个..
contents.add(new JLabel(m+""));
contents.add(new JLabel("x"));
contents.add(new JLabel(n+""));
contents.add(new JLabel("="));
contents.add(new JLabel(p+""));