简体   繁体   English

将面板添加到面板

[英]Adding Panel to Panel

I anted to add a JPanel to my already existing JPanel so I could have a small window with a JTextField on top with a name and a scrollable JTextArea below it with some description. 我事先将一个JPanel添加到我现有的JPanel中,所以我可以有一个小窗口,上面有一个JTextField,上面有一个名称,下面是一个可滚动的JTextArea,上面有一些描述。 I made a class that extends JPanel with the following constructor: 我制作了一个使用以下构造函数扩展JPanel的类:

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class LocationWindow extends JPanel {
    public JTextField name;
    public JTextArea desc;
    public JScrollPane scroll;

    public LocationWindow(){
        super();
        setBorder (new TitledBorder(new EtchedBorder(), "Display Area"));
        setLayout(new BorderLayout());
        setVisible(true);
        setBounds(30, 40, 700, 290);
        name = new JTextField(10);
        name.setText("name");
        desc = new JTextArea(5,10);
        scroll = new JScrollPane(desc);
        scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
        desc.setEditable (true);
        desc.setLineWrap(true);
        desc.setText("random text");
        add(name);
        add(desc);
        add(scroll);
        validate();
    }
}

It almost works, as it gives me the window with the borders and a scroll, but both the JTextField and JTextArea are missing. 它几乎可以正常工作,因为它为我提供了带有边框和滚动条的窗口,但是JTextField和JTextArea都丢失了。

As you are using BorderLayout for the JPanel, 当您为JPanel使用BorderLayout时

  setLayout(new BorderLayout());   

the components will always be added to the center if you dont specify the position. 如果您不指定位置,则组件将始终添加到中心。 add(scroll); is same as add(scroll,BorderLayout.CENTER); add(scroll,BorderLayout.CENTER); as you're adding all via add the last added component only be visible.Refer this as well 因为你要添加的所有通过添加,最后添加的成分仅是visible.Refer 这个问题,以及

The next is you are adding JTextArea seperately so it will be removed from ScrollPane.Just add scrollpane to Panel no need to add all components.[Add the parent component alone] 接下来是您要单独添加JTextArea,以便将其从ScrollPane中删除。只需将滚动窗格添加到Panel中,无需添加所有组件。[单独添加父组件]

    add(name,BorderLayout.NORTH);
    //add(desc);Noo need to add desc as it is already added in JScrollPane
    add(scroll,BorderLayout.CENTER);

There is no need for setVisible for JPanel.JPanel needs to be embedded in Container like JFrame to be visible JPanel不需要setVisible.JPanel需要像JFrame一样嵌入到Container中才能可见

         //setVisible(true);Wont do anything

So call like this 所以这样叫

    JFrame frame = new JFrame();
    frame.add(new LocationWindow());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

You can use below code to adding panel to panel. 您可以使用以下代码在面板之间添加面板。

 public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    LocationWindow loc = new LocationWindow();
    frame.add(loc);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

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

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