在JFrame中将固定大小的画布居中 [英] Centering a fixed size canvas in a JFrame

查看:158
本文介绍了在JFrame中将固定大小的画布居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建电路编辑器(类似于任何具有基本菜单和具有指定尺寸的画布的常规绘画软件).我目前正在尝试将以前不可滚动的画布(JPanel)转换为可滚动的画布.

I'm in the process of creating a circuit editor (similar to any regular paint software with a basic menu and a canvas with specifiable dimensions). I am currently trying to transform the previously unscrollable canvas (JPanel) to a scrollable one.

当前明显的设计错误是,尽管滚动条似乎正确地反映了画布的内部尺寸(当然可以比JFrame大),这是由于在画布中添加了JPanel主面板BorderLayoutCENTER,它总是与JFrame一起调整大小.

The obvious design error at the moment is that while the scrollbars seem to correctly reflect the internal size of the canvas (which can of course be way bigger than the JFrame), due to the canvas JPanel being added in the CENTER of the BorderLayout of the master panel, it always resizes along with the JFrame.

public final class MainFrame extends JFrame 
{
    public MainFrame()
    {            
        JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        // Populate Menu Panel
        // ...

        JPanel canvasPanel = new JPanel();
        canvasPanel.setBackground(Color.white);
        Dimension canvasDims = new Dimension(800,600);        
        canvasPanel.setPreferredSize(canvasDims);
        canvasPanel.setMinimumSize(canvasDims);
        canvasPanel.setMaximumSize(canvasDims);

        JScrollPane canvasScrollPane = new JScrollPane(
            canvasPanel,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        JPanel masterPanel = new JPanel(new BorderLayout());
        masterPanel.add(menuPanel, BorderLayout.NORTH);
        masterPanel.add(canvasScrollPane, BorderLayout.CENTER);

        setContentPane(masterPanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 700);
        setMinimumSize(new Dimension(500, 500));
        setLocationRelativeTo(null);
        setVisible(true); 
    }

我已经阅读了很多有关居中JPanels和滚动条的帖子,但是由于这两个复杂性加在一起,事情可能会更加复杂,因为我还没有找到解决方案.

I've read quite a few posts regarding centering JPanels and scrollbars but with both complexities added together, things might be a bit more complicated as I haven't yet found a solution.

我真正想要实现的是将画布的JPanel固定为用户可能指定的任何大小,并居中居中,以及滚动条的行为如人们所期望的那样,就像在心爱的窗口中那样'油漆:

What I'm really trying to achieve is to have the canvas' JPanel fixed in whatever size the user might have specified and centered in the middle as well as the scrollbars behaving as one would expect like in the beloved windows' paint:

您将如何进行/修复我的设计?任何帮助将不胜感激!

How would you go about doing this/fixing my design? Any help would be greatly appreciated!

P.S.新年快乐:)

P.S. Happy new Year :)

推荐答案

JPanel固定为用户可能指定的任何大小,并居中居中,并且滚动条的行为符合人们的预期

JPanel fixed in whatever size the user might have specified and centered in the middle as well as the scrollbars behaving as one would expect

因此,您需要嵌套面板,以便可以以其首选的尺寸显示画布面板,而父面板将根据框架的尺寸进行调整.

So you need to nest panels so the canvas panel can be displayed at its preferred size, while the parent panel resizes with the size of the frame.

使用GridBagLayoutJPanel是实现此目的的简单方法.然后,使用默认的GridBagConstraints将画布面板添加到该面板.

An easy way to do this is with a JPanel that uses a GridBagLayout. Then you add the canvas panel to this panel using the default GridBagConstraints.

因此,面板的基本结构为:

So the basic structure of the panels would be:

JPanel canvas = new JPanel();
canvas.setPreferredSize( new Dimension(300, 300) );
canvas.setBackground(Color.RED);

JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(canvas, new GridBagConstraints() );

frame.add(new JScrollPane(wrapper));

注意:不需要您的"masterPanel". JFrame内容窗格的默认布局管理器是BorderLayout,因此您只需将"menuPanel"和"scrollPane"直接添加到具有适当BorderLayout约束的框架中即可.

Note: there is no need for your "masterPanel". The default layout manager for the content pane of a JFrame is a BorderLayout, so you just add the "menuPanel" and "scrollPane" directly to the frame with the proper BorderLayout constraints.

这篇关于在JFrame中将固定大小的画布居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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