Swing问题:关于getY()和getHeight()
getY()到底是什么意思呢?和getHeight()有什么区别呢?我看getY()的解释是返回组件原点的当前 y 坐标。但是组件原点又是什么呢?
一个书上的例子:
class FontPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
String message = "Hello , World!";
Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
double x = (getWidth() - bounds.getWidth())/2;
double y = (getHeight() - bounds.getHeight())/2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(message, (int)x, (int) baseY);
g2.setPaint(Color.GRAY);
g2.draw(new Line2D.Double(x, baseY, x+bounds.getWidth(), baseY));
Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
g2.draw(rect);
}
}
请问那根线(基线)的位置是如何算出来的?
谢谢
----------------解决方案--------------------------------------------------------
getY()是得到组件的Y座标
getHeight()是得到组件的高度
----------------解决方案--------------------------------------------------------
回复 2# 的帖子
请问该Y座标是以什么为原点的?而组件的高度又是从哪里开始算的?谢谢 ----------------解决方案--------------------------------------------------------
Y坐标是以父组件的左上角做为原点
组件的高度就是组件的高度了,从上面到下面的高度
----------------解决方案--------------------------------------------------------
回复 4# 的帖子
哦,明白了,谢谢,呵呵 ----------------解决方案--------------------------------------------------------