简体   繁体   English

如何在null布局中强制调整JComponent的大小?

[英]How to resize JComponent forcefully in a null layout?

I am trying to resize a panel in a container panel where the container panel's layout is set to null. 我正在尝试在容器面板的布局设置为null的容器面板中调整面板的大小。

I am setting the location and changing the size like this: 我正在设置位置并更改大小,如下所示:

panelCapture.setLocation(
        scale.scale(((capture.getTotalBounds().width + 500)  / 2) - (capture.getTotalBounds().width  / 2)),
        scale.scale(((capture.getTotalBounds().height + 500) / 2) - (capture.getTotalBounds().height / 2))
);

Dimension pcSize    = panelCapture.getSize();
Dimension pcNewSize = new Dimension(scale.scale(pcSize.width), scale.scale(pcSize.height));

panelCapture.setSize(pcNewSize);
panelCapture.setPreferredSize(pcNewSize);

The scaling multiplies it by my current scale which is 1.0f, 1.1f, etc. 缩放比例乘以我当前的比例缩放比例,即1.0f,1.1f等。

Changing the position seems to work but changing the size does not. 更改位置似乎可行,但更改尺寸无效。

Instead of using setLocation and setSize, try setBounds(...) or setPreferredSize(...) . 而不是使用setLocation和setSize,请尝试setBounds(...)setPreferredSize(...) You may also need a call to repaint() after that. 之后,您可能还需要调用repaint()。 You may want to refer to the tutorial on using absolute/null layout here . 您可能要在此处参考有关使用绝对/空布局的教程。

Here's a sample I wrote to change the size of a JPanel inside another JPanel which has a null layout (using only setBounds). 这是我编写的用于更改另一个具有空布局(仅使用setBounds)的JPanel内的JPanel大小的示例。

public class Capture
{
    JPanel capture;
    JPanel panelCapture;
    JTextField scaleField;
    JButton changeScale;

    static final int PARENT_WIDTH = 800;
    static final int PARENT_HEIGHT = 600;

    static final int CHILD_WIDTH = 100;
    static final int CHILD_HEIGHT = 100;

    public static void main(String [] args)
    {
        Capture c = new Capture();
        c.doStuff();
    }

    public void doStuff()
    {
        capture = new JPanel();
        capture.setLayout(null);
        capture.setPreferredSize(new Dimension(PARENT_WIDTH, PARENT_HEIGHT));

        scaleField = new JTextField();
        scaleField.setBounds(100, 550, 200, 25);
        capture.add(scaleField);

        changeScale = new JButton("Scale");
        changeScale.setBounds(325, 550, 100, 25);
        changeScale.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                panelCapture.setBounds(getBounds(CHILD_WIDTH * Float.parseFloat(scaleField.getText()), CHILD_HEIGHT * Float.parseFloat(scaleField.getText())));
            }
        });
        capture.add(changeScale);

        panelCapture = new JPanel();
        panelCapture.setBackground(Color.blue);
        panelCapture.setBounds(getBounds(CHILD_WIDTH, CHILD_HEIGHT));
        capture.add(panelCapture);

        JFrame frame = new JFrame();
        frame.setContentPane(capture);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private Rectangle getBounds(float width, float height)
    {
        int left = (int) (PARENT_WIDTH - width) / 2;
        int top = (int) (PARENT_HEIGHT - height) / 2;
        return new Rectangle(left, top, (int) width, (int) height);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM