子组件setSize的地方在哪里

发布于 2024-09-17 11:45:18 字数 1627 浏览 4 评论 0原文

我有从 JPanel 扩展的课程。该类包含其他几个 JComponent 和大量绘画。我所做的(我知道这是不正确的)是,我覆盖了绘画的paintComponent,到目前为止一切顺利。

但我还在该方法中设置了所有子组件的位置和大小,因为它们依赖于 JPanel 的 getWidth 和 getHeight。我知道这不是放置组件的地方,但是我在哪里做这种事情呢?在构造函数时未设置大小。那么如何正确呢?因为只要我在paintComponent方法中有location和size方法,CPU就永远不会停止计算某些东西。

编辑:我所做的看起来像这样;

public class abc extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        /** all the painting stuff */
        textfield.setLocation(
            this.getWidth() * someValue,
            this.getHeight() * someotherValue);
        // also depends on getHeight() and getWidth()
        textfield.setSize(new Dimension(getHeight(), getWidth());
    }
}    

一切工作正常,除了 CPU 使用率始终在 10% 左右。如果两条线路都退出,CPU 使用率就会降至 0%。所以现在我使用了您推荐的布局管理器。一切看起来都很好,但 CPU 负载没有改变,仍然保持在 10% 左右。代码,现在的样子:

public class abc extends JPanel {

    public abc() {
        GridBagLayout gblayout = new GridBagLayout();

        this.setLayout(gblayout);

        textfield = new JTextField();
        textfield.setHorizontalAlignment(JTextField.CENTER);
        textfield.setBackground(new Color(0, 0, 0, 0));
        textfield.setBorder(null);
        textfield.setText("0");

        gblayout.setConstraints(textfield, new GridBagConstraints(
            0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
            GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 1, 1));

        this.add(textfield);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        /** still the same paintings */ }
}

I have class extended from JPanel. This class contains several other JComponents and a lot of painting. What I did and I know it isn't correct is, I overrided the paintComponent for the paintings, so far so good.

But I also set the location and the size for all subcomponents in this method, because they depend on the getWidth and getHeight from the JPanel. I know this isn't the place for the components, but where do I do this kind of stuff. At constructor time the sizes aren't set. So how do it correct? Because as long as I have the location and size method in the paintComponent method, the CPU never stops calculating something.

Edit: what i did looked like this;

public class abc extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        /** all the painting stuff */
        textfield.setLocation(
            this.getWidth() * someValue,
            this.getHeight() * someotherValue);
        // also depends on getHeight() and getWidth()
        textfield.setSize(new Dimension(getHeight(), getWidth());
    }
}    

Everything worked fine, except that I always had a CPU usage around 10%. If commeted both lines out, the CPU usage dropped to 0%. So now I used a Layoutmanager like you recommended. And still everything lookes fine, but the CPU load didn't change it still stayed by around 10%. The code, how it looks now:

public class abc extends JPanel {

    public abc() {
        GridBagLayout gblayout = new GridBagLayout();

        this.setLayout(gblayout);

        textfield = new JTextField();
        textfield.setHorizontalAlignment(JTextField.CENTER);
        textfield.setBackground(new Color(0, 0, 0, 0));
        textfield.setBorder(null);
        textfield.setText("0");

        gblayout.setConstraints(textfield, new GridBagConstraints(
            0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
            GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 1, 1));

        this.add(textfield);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        /** still the same paintings */ }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-09-24 11:45:19

如果您向我们展示一些代码并可能解释一下您想要做什么,那将会非常有帮助,但据我了解,您试图在一个 JPanel 中做太多事情。如果您打算在其上绘图等,您应该有一个单独的面板来覆盖paintComponent(),以及另一个(或多个)面板来容纳其他组件。

事实上,调整各个组件的大小几乎是不可能的,因为它们必须遵守为其父组件(通常是 JPanel)选择的布局,因此您也必须牢记这一点。

It will really help if you show us some code and maybe explain what you are trying to do but from what I understand, you're trying to do too much in one JPanel. You should have a separate panel that overrides paintComponent() if you intend to draw on it, etc and another one (or more) to hold your other components.

It is very hard, in fact, almost impossible to resize individual components because they have to obey the layout chosen for their parent component (usually a JPanel) so you have to keep that in mind as well.

荆棘i 2024-09-24 11:45:19

组件的大小和位置由布局管理器设置,一个frame初始化后,调用pack()来调整所有组件的大小。

您应该首先设置最小、最大和首选尺寸,然后让布局管理器完成其工作。您可以在paintComponent()中获取组件的宽度和高度,以根据其大小进行渲染。

The size and position of components are set by the layout manager, After a frame is initialized, pack() is called to adjust the size of all components.

You should initially set the min, max and preferred size, and let the layout manager do its job. You can get the width and height of the component in paintComponent() to render it accordingly to its size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文