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

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

问题描述

我尝试用 Java 绘制一个矩形.我设置了框架大小 (800,400) 和 resizable(false) 矩形的 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天全站免登陆