我怎么能在中间设置? [英] How can I set in the midst?

查看:111
本文介绍了我怎么能在中间设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Java中绘制一个矩形。我设置了帧大小(800,400)和可调整大小(假)矩形的x = 50,y = 50宽度= 700高度= 300.为什么它不在中间?谢谢。

I try to draw a rectangular in Java. I set the frame size (800,400) and resizable(false) rectangular's x = 50, y = 50 width = 700 height = 300. Why isn't it in the middle? Thank you.

推荐答案

没有任何其他证据,我你已经覆盖了 paint 类似 JFrame 的方法,并直接绘制到它。

Without any evidence otherwise, I'd guess you've overriden the paint method of something like a JFrame and are painting directly to it.

问题是,框架有装饰(例如边框和标题栏),占据内部空间框架......

The problem is, frames have decoration (a border and title bar for example), which takes up space inside the frame...

从技术上讲,这是对的。矩形被画在框架的中心,但由于框架的装饰,它看起来有点高......

Technically, this is correct. The rectangle is painted in the center of frame, but the because of the frame's decorations, it looks like it's slightly high...

相反,你应该画在取而代之的是框架的内容区域。

Instead, you should be painting onto the frame's content area instead.

此处矩形现在看起来正确居中。在我的测试中,我将第一帧(坏)设置为800x400,我将第二帧的内容窗格的首选大小设置为800x400,这使得帧大小实际为816x438,因为帧的装饰现在之外绘制区域。

Here the rectangle now looks correctly centered. In my tests, I set the first frame (bad) to 800x400, I made the second frame's content pane's preferred size 800x400, which made the frame size actually 816x438, as the frame's decorations are now outside of the paint area.

public class CenterOfFrame {

    public static void main(String[] args) {
        new CenterOfFrame();
    }

    public CenterOfFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                new BadFrame().setVisible(true);

                JFrame goodFrame = new JFrame();
                goodFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                goodFrame.setContentPane(new PaintablePane());
                goodFrame.pack();
                goodFrame.setLocationRelativeTo(null);
                goodFrame.setVisible(true);

            }
        });
    }

    public class BadFrame extends JFrame {

        public BadFrame() {
            setSize(800, 400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            paintTest(g, getWidth() - 1, getHeight() - 1);
        }
    }

    public void paintTest(Graphics g, int width, int height) {
        g.setColor(Color.RED);
        g.drawLine(0, 0, width, height);
        g.drawLine(width, 0, 0, height);
        g.drawRect(50, 50, width - 100, height - 100);
    }

    public class PaintablePane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
            paintTest(g, getWidth() - 1, getHeight() - 1);
        }
    }
}

这是其中之一原因,为什么你不应该覆盖顶级容器的 paint 方法;)

This is, one of many reasons, why you should not override the paint method of top level containers ;)

这篇关于我怎么能在中间设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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