为什么我的坐标不匹配我的JFrame大小? [英] Why aren't my coordinates matching my JFrame size?

查看:77
本文介绍了为什么我的坐标不匹配我的JFrame大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JPanel中进行一些绘制,但是封闭的JFrame大小似乎与我要求绘制坐标的位置不匹配.

在我的示例代码中,JFrame大小设置为(700,700),最后一点绘制为(600,600).我希望此点距右边缘和底边缘100像素,但不是(请参见屏幕截图).

这是我正在使用的代码:

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);

    }

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.

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);


    }

}

解决方案

Only a guess:

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大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆