Java JScrollPane问题

Java JScrollPane问题,java,swing,layout,jscrollpane,Java,Swing,Layout,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 JBu

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

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);
    }
}
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);
    }
}
我读过一篇教程,上面说:

//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);
我也做了同样的步骤

private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
private JScrollPane scrollPane = new JScrollPane(mypanel);
然后添加滚动窗格:

container.add(scrollPane);
哪里有错误? 编辑: 问题是滚动窗格不工作。我在mypanel中添加了许多JInfoView,
但是scroll不起作用。

您忘记添加
scrollPane。setPreferredSize
如下调用:

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);
        scrollPane.setPreferredSize(new Dimension(300, 400)); //========== this was missed

        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());
        this.pack();
    }

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

如果
JScrollPane
位于
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);
    }
}

你没有描述什么不起作用。不要让我们复制你的代码并测试它,然后看到错误。请描述@Mat:what不起作用的问题?抱歉,我刚刚编辑了这个问题。@maximus和new'JPanel(new GridLayout(0,1,30,50))的定义;'与添加的“mypanel.add(new JInfoView());”的编号不对应mKorbel为什么不呢?新的GridLayout(0、1、30、50)意味着可以有任意数量的行,而只有一列。是吗?你的意思是吗?@denisk你的回答是不是意味着,在向mypanel添加新元素时,scrollpane的首选大小会发生变化?@maximus:我不知道它是否会发生变化(我想不会),只需指定Calling
setPreferredSize(Dimension)
是不可取的(绝大多数情况下)在这种情况下也没有必要。