将Glabel添加到形成三角形的Java程序中。

huangapple 未分类评论51阅读模式
英文:

adding Glabel to form a triangle java program

问题

以下是您的代码的翻译部分:

这是我的代码

public class test extends GraphicsProgram {
	private static final int LABELGAP = 30;
	private static final int LINEGAP = 20;
	int numLabel = 1;
	double y = 75;
	public void run() {
		for (int i = 0; i < 8; i++) {
			GLabel label = new GLabel("Hello World");
			label.setFont("London-12");
			double x = (getWidth() - label.getWidth() * numLabel - LABELGAP * (numLabel -1)) / 2;
			double step = label.getWidth() + LABELGAP;
			for (int j = 0; j < numLabel; j++) { 					
				add(label, x, y);
				x += step;					
			}			
			numLabel++;	
			y += LINEGAP;
		}
	}
}

但是每行只有一个标签问题出在哪里
我希望每一行都比上一行多一个标签
而且如果我将以下代码放在第二个for循环之外

GLabel label = new GLabel("Hello World");
label.setFont("London-12");
double x = (getWidth() - label.getWidth() * numLabel - LABELGAP * (numLabel -1)) / 2;
double step = label.getWidth() + LABELGAP;

与将它们放在内部不同结果也不同都不是所需的结果)。为什么
英文:

Here is my code:

public class test extends GraphicsProgram {
private static final int LABELGAP = 30;
private static final int LINEGAP = 20;
int numLabel = 1;
double y = 75;
public void run() {
	for (int i = 0; i &lt; 8; i++) {
		GLabel label = new GLabel(&quot;Hello World&quot;);
		label.setFont(&quot;London-12&quot;);
		double x = (getWidth() - label.getWidth() * numLabel - LABELGAP * (numLabel -1)) / 2;
		double step = label.getWidth() + LABELGAP;
		for (int j = 0; j &lt; numLabel; j++) { 					
			add(label, x, y);
			x += step;					
		}			
		numLabel++;	
		y += LINEGAP;
	}
}

}

But there is only one label per line. What's wrong?
I want to have one more label than previous line.
Also, if I put

GLabel label = new GLabel(&quot;Hello World&quot;);
label.setFont(&quot;London-12&quot;);
double x = (getWidth() - label.getWidth() * numLabel - LABELGAP * (numLabel -1)) / 2;
double step = label.getWidth() + LABELGAP;

This part of the code outside of 2nd for loop, the result is different from when I put them inside(neither is the desired result). Why?

huangapple
  • 本文由 发表于 2020年5月4日 11:36:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61584786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定