简体   繁体   中英

Java JScrollPane ve

I want to make a Java-Code, where I can insert as many Panels as I want. 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.

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.

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. 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.

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 ). When you want to have your components appear on multiple rows, you should try to force FlowLayout to do it by constraining its width; rather, use a layout that is designed to place components on different rows. The best choice is 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.

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. Finally, GridBagConstraints.NORTH keeps that child placed at the top of that stretched grid cell, no matter how high the grid cell is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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