首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JScrollPane问题

JScrollPane问题
EN

Stack Overflow用户
提问于 2011-05-20 15:04:38
回答 2查看 2.7K关注 0票数 0

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

JInfoView.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}

我读过一篇教程,上面写道:

代码语言:javascript
复制
//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);

我也做过同样的步骤,

代码语言:javascript
复制
private JPanel mypanel = new JPanel(new GridLayout(0, 1, 30, 50));
private JScrollPane scrollPane = new JScrollPane(mypanel);

然后添加了scrollpane:

代码语言:javascript
复制
container.add(scrollPane);

哪里出错了?编辑:问题是滚动窗格不工作。我给mypanel添加了很多JInfoView,但是滚动不起作用..

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-20 15:23:05

您忘记了像这样添加scrollPane.setPreferredSize调用:

代码语言:javascript
复制
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);
    }
}
票数 2
EN

Stack Overflow用户

发布于 2011-05-20 15:38:24

如果JScrollPaneBorderLayoutCENTER中,它似乎可以正常工作。例如。

代码语言:javascript
复制
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);
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6068576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档