JScrollPane 问题

max*_*mus 0 java layout swing jscrollpane

我制作了一个带有滚动窗格的程序,但它不起作用。请看源码:

JInfoView.java

package view;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class JInfoView extends JPanel {
    private JButton button = new JButton("ADD");
    private JButton buttonDelete = new JButton("DEL");
    private JTextField input = new JTextField("Text", 5);
    private JLabel label = new JLabel("Test");
    public JInfoView() {
        this.setLayout(new FlowLayout());
        this.add(button);
        this.add(buttonDelete);
        this.add(input);
        this.add(label);
    }
}
Run Code Online (Sandbox Code Playgroud)

JMainView.java

package view;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import view.JInfoView;

public class JMainView extends JFrame {
    private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
    private JScrollPane scrollPane = new JScrollPane(mypanel);
    public JMainView() {
        super("Simple Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(new FlowLayout());
        container.add(scrollPane);

        scrollPane.setVisible(true);
        scrollPane.setAutoscrolls(true); 

        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());       
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView()); 
        mypanel.add(new JInfoView());
    }

    public static void main(String[] args) {
        JMainView app = new JMainView();
        app.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

我读过一个教程,它说:

//In a container that uses a BorderLayout:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
setPreferredSize(new Dimension(450, 110));
...
add(scrollPane, BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)

我做了同样的步骤,

private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
private JScrollPane scrollPane = new JScrollPane(mypanel);
Run Code Online (Sandbox Code Playgroud)

然后添加滚动窗格:

container.add(scrollPane);
Run Code Online (Sandbox Code Playgroud)

哪里出错了?编辑:问题是滚动窗格不起作用。我向我的面板添加了许多 JInfoView,但滚动不起作用..

And*_*son 5

如果JScrollPane是在CENTERa中,它似乎可以正常工作BorderLayout。例如

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

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

public class JMainView extends JFrame {
    private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
    private JScrollPane scrollPane = new JScrollPane(mypanel);
    public JMainView() {
        super("Simple Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());
        container.add(scrollPane);

        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
        mypanel.add(new JInfoView());
    }

    public static void main(String[] args) {
        JMainView app = new JMainView();
        // important!
        app.pack();
        // show the scroll bars by compressing the GUI height
        app.setSize(
            (int)app.getSize().getWidth()+30, 
            (int)app.getSize().getHeight()/2);
        app.setVisible(true);
    }
}

class JInfoView extends JPanel {
    private JButton button = new JButton("ADD");
    private JButton buttonDelete = new JButton("DEL");
    private JTextField input = new JTextField("Text", 5);
    private JLabel label = new JLabel("Test");
    public JInfoView() {
        this.setLayout(new FlowLayout());
        this.add(button);
        this.add(buttonDelete);
        this.add(input);
        this.add(label);
    }
}
Run Code Online (Sandbox Code Playgroud)