java - JScrollPane问题

标签 java swing layout jscrollpane

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

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);

然后添加滚动 Pane :

container.add(scrollPane);

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

最佳答案

如果 JScrollPane 位于 BorderLayoutCENTER 中,似乎工作正常。例如

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);
    }
}

关于java - JScrollPane问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6068576/

相关文章:

Java,网格布局问题。难以放置元件

css - 使用 jqui_sortable 排序后 div 元素之间的间距扭曲

html - 如何使 2 个 div float ,左边的 div 具有固定的右边距?

java - 为什么Java没有复制构造函数?

java - Scala 和 Eclipse : Export as Jar File, 但找不到 main 方法

java - Bamboo 和 Artifactory 之间的语义版本控制?

Java在滚动条出现时动态改变组件

java - 尝试复制矩阵时出现 ArrayIndexOutOfBoundsException

java - 如何验证输入的文本字段是否是java swing中的手机号码

html - 如何在 <div> 中垂直顶部对齐 <nav>?