简体   繁体   English

Java JScrollPane ve

[英]Java JScrollPane ve

I want to make a Java-Code, where I can insert as many Panels as I want. 我想制作一个Java代码,可以在其中插入任意数量的面板。 So that I can scroll down to see the Panels. 这样我就可以向下滚动以查看面板。 I'm so far right now: But my problem is, I can't scroll down. 我现在为止很远:但是我的问题是,我无法向下滚动。 I tested the JScrollPane with JTextAreas which worked just fine. 我用JTextAreas测试了JScrollPane,它工作得很好。

Picture of my Program 我的程序图片

package test;

import java.awt.*;
import javax.swing.*;

public class Scrollbar {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JTextField tFId = new JTextField("ID: ", 5);
        JTextField tFName = new JTextField("Name: ", 5);
        JTextField tFHersteller = new JTextField("Hersteller: ", 5);
        JTextField tFId2 = new JTextField("ID: ", 5);
        JTextField tFName2 = new JTextField("Name: ", 5);
        JTextField tFHersteller2 = new JTextField("Hersteller: ", 5);
        JTextField tFId3 = new JTextField("ID: ", 5);
        JTextField tFName3 = new JTextField("Name: ", 5);
        JTextField tFHersteller3 = new JTextField("Hersteller: ", 5);

        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        panel.setLayout(new FlowLayout());
        panel.setPreferredSize(new Dimension(200, 85));
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        JScrollPane scrollPanel = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        panel1.setLayout(new FlowLayout());
        panel1.add(tFId);
        panel1.add(tFName);
        panel1.add(tFHersteller);

        panel2.setLayout(new FlowLayout());
        panel2.add(tFId2);
        panel2.add(tFName2);
        panel2.add(tFHersteller2);

        panel3.setLayout(new FlowLayout());
        panel3.add(tFId3);
        panel3.add(tFName3);
        panel3.add(tFHersteller3);

        frame.add(scrollPanel);
        frame.setVisible(true);
    }
}

You are over-using FlowLayout. 您正在过度使用FlowLayout。

Different layouts have diferent behaviors. 不同的布局具有不同的行为。 First, you need to remove this line: 首先,您需要删除以下行:

frame.setLayout(new FlowLayout());

The default layout for a frame's content pane is a BorderLayout. 框架内容窗格的默认布局是BorderLayout。 You want to leave it that way, because a component added to a BorderLayout with no constraints will be placed in the center, where it will stretch to fill the entire space. 您希望以这种方式保留它,因为添加到BorderLayout且没有任何约束的组件将放置在中心,该组件将在其中伸展以填充整个空间。

Second, you want to remove these: 其次,您要删除这些:

panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(200, 85));

Setting the preferred size interferes with the JScrollPane's ability to manage its view (that is, panel ). 设置首选大小会干扰JScrollPane管理其视图(即panel )的能力。 When you want to have your components appear on multiple rows, you should try to force FlowLayout to do it by constraining its width; 当您希望组件显示在多行中时,应尝试通过限制FlowLayout的宽度来强制执行它。 rather, use a layout that is designed to place components on different rows. 而是使用旨在将组件放置在不同行上的布局。 The best choice is GridBagLayout : 最好的选择是GridBagLayout

panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(panel1, gbc);
panel.add(panel2, gbc);
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
panel.add(panel3, gbc);

The use of GridBagConstraints.REMAINDER in a constraint will tell the GridBagLayout to make a child component take up an entire row. 在约束中使用GridBagConstraints.REMAINDER将告诉GridBagLayout使子组件占据整个行。

The use of weighty = 1 tells the GridBagLayout that the grid cell of the child about to be added should take up all extra vertical space, when the panel is larger than its children. weighty = 1的使用告诉GridBagLayout当面板大于其子项时,要添加的子项的网格单元应占用所有额外的垂直空间。 Finally, GridBagConstraints.NORTH keeps that child placed at the top of that stretched grid cell, no matter how high the grid cell is. 最后,无论网格单元有多高,GridBagConstraints.NORTH都将其子项放置在拉伸网格单元的顶部。

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

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