想在字符串后紧跟一个长方形方块表示进度,不过写出来的效果跟我自己想要的不一样,方块位置不对,写了测试用的小程序让大家看看是哪里想错了。。
代码如下:import java.awt.*;public class Test extends Frame {
private int promptX = 5, promptY = 45;
public static void main(String[] args) {
new Test().launchFrame();
}
public void launchFrame() {
setLocation(300 ,100);
setSize(800, 600);
setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawString("画个方块:", this.promptX, this.promptY);
g.fillRect(this.promptX + "画个方块:".length(), this.promptY, 100, 20);
}
}
代码如下:import java.awt.*;public class Test extends Frame {
private int promptX = 5, promptY = 45;
public static void main(String[] args) {
new Test().launchFrame();
}
public void launchFrame() {
setLocation(300 ,100);
setSize(800, 600);
setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawString("画个方块:", this.promptX, this.promptY);
g.fillRect(this.promptX + "画个方块:".length(), this.promptY, 100, 20);
}
}
g.drawString("画个方块:", this.promptX+6, this.promptY+12);
g.fillRect(this.promptX + "画个方块:".length()*12, this.promptY, 100, 20);看API, drawString的x,y是最左边字符的baseline,就是左下角
fileRect的x,y是左上角
另外,*12是假设字符是12个点阵地方块字,"画个方块:".length()是字符串的长度,不是字符串显示在屏幕上的长度。貌似这个是你想说的问题吧?