JScrollPane与JPanel没什么关系

时间:2013-06-27 11:39:12

标签: java swing jframe jpanel jscrollpane

我将添加到JFrame JSrollPane。 ScrollPane包含JPanels。但是当我向ScrollPane添加第一个JPanel时,我遇到了问题,当我将JPanel添加到JFrame时看不到任何内容我看到了JPanels。那么我犯错误的地方?这里代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class AddingJPanels {

    public static void main(String... args) {
        JFrame jF = new JFrame();
        PanelMain pM = new PanelMain();

        Panel p = new Panel("sas");
        JPanel jp = makeJPanel(10);
        p.setPreferredSize(new Dimension(600,600));
        JScrollPane scroll = new JScrollPane();
        scroll.add(jp);
        JScrollBar verticalPane = scroll.getVerticalScrollBar();
        verticalPane.setValue(verticalPane.getMinimum());
        verticalPane.setValue(20);
        //scroll.setPreferredSize(new Dimension(570, 300));
        scroll.setPreferredSize(new Dimension(400,500));
        pM.add(scroll);
        //JTabbedPane tB = new JTabbedPane();
        //tB.addTab(":]", null, pM, "Tab 3");
        jF.add(jp);
        jF.setSize(new Dimension(500,500));
        jF.setVisible(true);
    }






    static JPanel makeJPanel(int i){
        JPanel jPl = new JPanel();
        jPl.setLayout(new GridLayout(i,0));
        JLabel lebel;

        for(int j=0;j<i;++j){
            JPanel p = new JPanel();
            p.setLayout(new GridLayout(2,2));
            JButton b = new JButton("asa");
            p.add(b);
            p.setBorder(BorderFactory.createLineBorder(Color.black));
            p.setPreferredSize(new Dimension(400,400));
            lebel = new JLabel("Napis: "+j);
            p.add(lebel);
            JTextField jTF = new JTextField("Nic",20);
            p.add(jTF);
            jPl.add(p);
        }
        return jPl;
    }
}

class Frame extends JFrame {
    public Frame() {
        super("Frame");
        this.setPreferredSize(new Dimension(200, 200));

    }

    public void see() {
        this.setVisible(true);
    }
}

class PanelMain extends JPanel {
    JButton b = new JButton("press me");

    public PanelMain() {
        this.add(b);
        b.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Pressed");
            }

        });
    }
}

class Panel extends JPanel {
    JLabel l;

    public Panel(String s) {
        l = new JLabel(s);
        this.add(l);
    }
}

当我制作jF.add(滚动)时没有效果。

3 个答案:

答案 0 :(得分:2)

add()不适用于JScrollPane。您需要使用setViewport()或者在contstructor中传递一个组件。

JScrollPane scroll = new JScrollPane(jp);

JScrollPane scroll = new JScrollPane();
scroll.setViewport(jp);

答案 1 :(得分:1)

JScrollPane scroll = new JScrollPane(jp);

答案 2 :(得分:1)

使用:

scroll.setViewportView(jp);