简体   繁体   English

添加到JPanel时,Java Swing组件的大小不正确

[英]Java Swing Components does not have correct Size when added to JPanel

It's my first Post here, so forgive me please if i'm doing something wrong. 这是我的第一篇帖子,请原谅我,如果我做错了。

My Problem is: 我的问题是:

I am trying to add Components to a JPanel with defined values for Size etc. But when i add them to the Panel, they do absolutely not have the Size and Location they should have. 我正在尝试将组件添加到具有Size等定义值的JPanel中。但是当我将它们添加到Panel时,它们绝对没有它们应该具有的大小和位置。 For example: 例如:

public class Console extends JFrame {
    private JPanel mainPanel = new JPanel();
    private JTextArea textField = new JTextArea();
    private JTextArea textField2 = new JTextArea();

    public Console() {
        this.setSize(500,300);

        this.mainPanel.setSize(this.getWidth(),this.getHeight());

        this.textField.setEditable(false);
        this.textField.setSize(this.mainPanel.getWidth(), 100);
        this.textField.setPreferredSize(new Dimension(this.mainPanel.getWidth(),this.mainPanel.getHeight()));
        this.textField.setLocation(0, 0);
        this.textField.setText("some text");
        this.textField.setVisible(true);

        this.textField2.setSize(this.mainPanel.getWidth(),200);
        this.textField2.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
        this.textField2.setLocation(0,this.mainPanel.getHeight()-this.textField.getHeight());
        this.textField2.setText("blabla");
        this.textField2.setVisible(true);

        this.mainPanel.add(textField);
        this.mainPanel.add(textField2);
        this.mainPanel.setVisible(true);

        this.add(this.mainPanel);
        // I know you should not call setVisible() in the Constructor, just for making Code more simple here.
        this.setVisible(true);
    }
}

When i start the Application, both JTextArea's are really small and somewhere in the middle (not as set above) while the mainPanel is correct.I tried to call setSize() and setPreferredSize() in different Places in the Code, but it didn't work. 当我启动应用程序时,两个JTextArea都非常小,位于中间(不是上面设置),而mainPanel是正确的。我试图在代码中的不同位置调用setSize()和setPreferredSize(),但它没有'工作。 I know it is better to use a LayoutManager for doing this as far as i heard but to be honest, i do not get how to use it correctly. 据我所知,最好使用LayoutManager来做到这一点,但说实话,我不知道如何正确使用它。 I checked it on Oracle Doc's but i would appreciate it if someone could post a clean Solution for this, Thanks in Advance. 我在Oracle Doc上检查了它,但如果有人可以为此发布一个干净的解决方案我会很感激,谢谢你提前。

You need to set a proper Layout for your Container . 您需要为Container设置适当的Layout You setLayout for a Container like JFrame , JPanel and so on. 你可以为像JFrameJPanel等容器设置setLayout You don't add other components to layout, but to a container. 您不会将其他组件添加到布局,而是添加到容器。 Then it would layout them accordingly. 然后它会相应地布局它们。 It is how it works. 它是如何工作的。

With proper layout you would not need to call setLocation() . 使用适当的布局,您不需要调用setLocation() Also setVisible(true) is excessive, because true is default values for those components in your code. 此外, setVisible(true)过多,因为true是代码中这些组件的默认值。

Better not to extend JFrame , extend JPanel instead and add it to JFrame. 最好不要扩展JFrame ,而是扩展JPanel并将其添加到JFrame。

Please, learn about EDT and SwingUtilities.invoketLater() you need to use it. 请了解您需要使用它的EDT和SwingUtilities.invoketLater()

Also you can save some bytes, not typing this. 您也可以保存一些字节,而不是键入this.字节this. all the time. 每时每刻。

这都是关于Swing布局的布局

For your JTextArea problem, use: 对于您的JTextArea问题,请使用:

       JTextArea j=new JTextArea();
       j.setColumns(20);
       j.setRows(5);

Change the values of setColumns() and setRows() to vary the size; 更改setColumns()setRows()以改变大小; + the suggestion given about Layout Managers . +关于Layout Managers的建议。

Hope this works ;) 希望这有效;)

尝试使用jpanel的绝对布局。

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

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