Java 滚动条不可见

Java scrollbar not visible

我是 java 的新手。我正在尝试为我的 Jpanel 实现滚动功能,但学校窗格不可见。试了很多方法都没用。

public class test extends ContentPanel {

JPanel secondary;
JPanel customerType;
JPanel primary;


JPanel labelPanel;
JLabel lCustNo;
JLabel lCustName;
JLabel lTelNo;
JLabel lAddress;
JLabel lNationality;
JLabel lResident;
JLabel lVisitor;
JLabel lCustomerType;
JLabel lIdCard;
JLabel lBankName;
JLabel lPassportNo;
JLabel lVisitStart;
JLabel lVisitEnd;

JTextField tCustNo;
JTextField tCustName;
JTextField tTelNo;
JTextField tAddress;
JTextField tNationality;
JTextField tIdCard;
JTextField tBankName;
JTextField tPassportNo;
JTextField tVisitStart;
JTextField tVisitEnd;

JPanel radioButton;

JRadioButton rResident;
JRadioButton rVisitor;
ButtonGroup custType;

JScrollPane scrollBar;

JSeparator s1;
public test (String title, JPanel parent) {
    super(title, parent);
    secondary = new JPanel(new GridLayout(2,0));
    secondary.setBounds(10, 10, 500, 800);
    labelPanel = new JPanel(new GridLayout(5,2,300,20));
    radioButton = new JPanel(new GridLayout(1, 4));
    customerType = new JPanel(new GridLayout(3,4, 30, 20));
    primary = new JPanel(new BorderLayout());
    lCustNo = new JLabel("Customer No: ");
    lCustName = new JLabel("Name: ");
    lTelNo = new JLabel("Telephone Number: ");
    lAddress = new JLabel("Address: ");
    lNationality = new JLabel("Nationality: ");
    lResident = new JLabel("Resident");
    lVisitor = new JLabel("Visitor");
    lCustomerType = new JLabel("Customer Type");
    lIdCard = new JLabel("Id Card");
    lBankName = new JLabel("Bank Name");
    lPassportNo = new JLabel("Passport Number");
    lVisitStart = new JLabel("Visit Start");
    lVisitEnd = new JLabel("Visit End");
    tCustNo = new JTextField(10);
    tCustName = new JTextField(10);
    tTelNo = new JTextField(10);
    tAddress = new JTextField(10);
    tNationality = new JTextField(10);
    tIdCard = new JTextField(10);
    tBankName = new JTextField(10);
    tPassportNo = new JTextField(10);
    tVisitStart = new JTextField(10);
    tVisitEnd = new JTextField(10);
    rResident = new JRadioButton();
    rVisitor = new JRadioButton();
    custType = new ButtonGroup();
    customerType.add(lPassportNo);
    customerType.add(tPassportNo);
    customerType.add(lVisitStart);
    customerType.add(tVisitStart);
    customerType.add(lIdCard);
    customerType.add(tIdCard);
    customerType.add(lBankName);
    customerType.add(tBankName);
    customerType.add(lVisitEnd);
    customerType.add(tVisitEnd);

    custType.add(rResident);
    custType.add(rVisitor);

    radioButton.add(lVisitor);
    radioButton.add(rVisitor);
    radioButton.add(lResident);
    radioButton.add(rResident);

    labelPanel.add(lCustNo);
    labelPanel.add(tCustNo);
    labelPanel.add(lCustName);
    labelPanel.add(tCustName);
    labelPanel.add(lTelNo);
    labelPanel.add(tTelNo);
    labelPanel.add(lAddress);
    labelPanel.add(tAddress);
    labelPanel.add(lNationality);
    labelPanel.add(tNationality);

    secondary.add(customerType);
    secondary.add(labelPanel);;
    primary.add(secondary,BorderLayout.CENTER);
    scrollBar= new JScrollPane(primary);
    scrollBar.setBounds(10, 10, 400, 555);
    this.add(scrollBar);

}

}

我的代码在上面。 class 扩展了 contentPanel 并且 contentPanel 扩展了 JPanel。有人请帮助我。

谢谢。

您的 scrollBar 不可见,因为没有必要。如果您更改 JScrollPane 设置,例如:

scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);  
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

你会看到你拥有它,但不会使用它。这是因为您的 scrollBar 没有动态扩展的内容(例如 JTextArea)。您的 scrollBar 内部有 JPanel,其大小适合其所有组件,因此在文本组件中添加一些文本是不够的。

要使滚动条可见,您可以更改 JSrollPane 的大小。但是,如果您 JPanel 使用默认值 FlowLayoutsetBound() 将不起作用。要在 JPanel 中使用绝对定位,您需要使用 null 布局,这通常不被推荐并且被视为糟糕的编程习惯。

尝试删除 setBound() 行并使用例如 scrollBar.setPreferredSize(new Dimension(400,555)); 代替。您应该会看到差异。