Why aren#39;t my coordinates matching my JFrame size?(为什么我的坐标与我的 JFrame 大小不匹配?)
问题描述
我想在 JPanel 中进行一些绘图,但封闭的 JFrame 大小似乎与我要求绘制坐标的位置不匹配.
I want to do some drawing in a JPanel but the enclosing JFrame size doesn't seem to match where I've asked the coordinates to be drawn.
在我的示例代码中,JFrame 大小设置为 (700, 700),最后一个点绘制在 (600, 600).我希望这个点在距离右边缘和下边缘 100 像素处绘制,但事实并非如此(请看截图).
In my example code, the JFrame size is set to (700, 700) and the last point is drawn at (600, 600). I would expect this point to be drawn 100 pixels away from the right and bottom edges but it isn't (please see screenshot).
这是我正在使用的代码:
Here's the code I'm using:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Scratch extends JPanel {
static int frameWidth = 700;
static int frameHeight = 700;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(frameWidth, frameHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Scratch scratch = new Scratch();
frame.getContentPane().add(scratch);
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
g.drawRect(100, 100, 1, 1);
g.drawString("100", 100, 100);
g.drawRect(200, 200, 1, 1);
g.drawString("200", 200, 200);
g.drawRect(300, 300, 1, 1);
g.drawString("300", 300, 300);
g.drawRect(400, 400, 1, 1);
g.drawString("400", 400, 400);
g.drawRect(500, 500, 1, 1);
g.drawString("500", 500, 500);
g.drawRect(600, 600, 1, 1);
g.drawString("600", 600, 600);
}
}
推荐答案
只是猜测:
public class Scratch extends JPanel {
static int frameWidth = 700;
static int frameHeight = 700;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Scratch scratch = new Scratch();
scratch.setSize(frameWidth, frameHeight);
frame.getContentPane().add(scratch);
frame.pack();
frame.setVisible(true);
}
这篇关于为什么我的坐标与我的 JFrame 大小不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我的坐标与我的 JFrame 大小不匹配?


- 未找到/usr/local/lib 中的库 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01