Java 图形用户界面中的Pascal三角形
您好,您能帮助我如何在JTextArea中打印pascal三角形吗?输出为一般输出中的打印。这是我的构造函数Java 图形用户界面中的Pascal三角形,java,Java,您好,您能帮助我如何在JTextArea中打印pascal三角形吗?输出为一般输出中的打印。这是我的构造函数 public static int ComputePascal(int rows) { for(int i =0;i<rows;i++) { int number = 1; System.out.format("%"+(rows-i)*2+"s",""); for(int j=0;j<=i;j++) {
public static int ComputePascal(int rows) {
for(int i =0;i<rows;i++) {
int number = 1;
System.out.format("%"+(rows-i)*2+"s","");
for(int j=0;j<=i;j++) {
System.out.format("%4d",number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
return rows;
}
我没有Java方面的经验,所以如果我错了,请纠正我,但我建议创建一个JTextArea,然后将打印到System.out的行传递到这个JTextArea
JTextArea textArea = new JTextArea();
//your code here
//add this instead of System.out with your own output in the ()
textArea.append("Pascal Triangle: " + number);
以下是您应该做的:
- 创建一个包含
- 创建并将JtextArea添加到框架中
- 使用包含值的字符串设置JTextArea
为了帮助您使用swing api:sir它将直接打印pascal三角形,而不是atriangle@GoranReymundo所以我误解了你的问题,因为你要求在JTextArea而不是控制台中输出,如果我不够清楚,你应该只使用
textArea.append()
并用打印输出代码填充它以将数据获取到JTextAreasir我想使用ActionListener调用构造函数ComputePascal()..if(e.getSource()==b2){这是我的操作侦听器sir String dee=f1.getText();int dev=Integer.parseInt(dee);int hard=ComputePascal(dev);String ans=String.valueOf(硬);area.setText(ans);我不是专家,但你的问题让人困惑,正如你在回答中看到的:你想要什么还不完全清楚。这可能有三件事:1.将输出打印到文本区域?2.格式化输出?3.两者都可以?将我的输出打印到文本区域sirok你必须将系统.out
替换为文本区域。据我所知,追加(您必须在之前定义它)@Zorian像这样,先生?textArea.append.println()或textArea.append()?textArea.append(“Hello World”);
并用要打印的文本和变量替换“Hello World”
JTextArea textArea = new JTextArea();
//your code here
//add this instead of System.out with your own output in the ()
textArea.append("Pascal Triangle: " + number);