我使用的是JBuilder2006.做了个类,继承JPanel,上面有一些控件(JLabel,JText,JButton...)。然后再在另一个窗体的内容面板new 出几个来,如果那个窗体是流式布局的话,就什么也不显示,但如果是网格布局的话,能出来,但是new 一个两个还好,我如果要new 很多的话,就不行了,加滚动面板也不行,我希望能有滚动条,而且是流式布局那样,不论new 多少都行,怎么解决? 在线等,谢谢了

解决方案 »

  1.   

    我想是不是应该在窗体中用pack()函数,这样会不会就不用滚动条了.
    我也是新手
      

  2.   

    package utilbeans;import java.awt.BorderLayout;import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class Frame1 extends JFrame {
        public Frame1() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    private void jbInit() throws Exception {
            getContentPane().setLayout(null);
            jScrollPane1.setBounds(new Rectangle(4, 7, 522, 494));
            this.getContentPane().add(jScrollPane1);
            jButton1.setText("jButton1");
            jPanel1.setLayout(flowLayout1);
            jScrollPane1.getViewport().add(jPanel1);
            jButton2.setText("jButton2");
            jButton3.setText("jButton3");
            jButton4.setText("jButton4");
            jButton5.setText("jButton5");
            jButton6.setText("jButton6");
            jButton7.setText("jButton7");
            jButton8.setText("jButton8");
            jButton9.setText("jButton9");
            jButton10.setText("jButton10");
            jPanel1.add(jButton1);
            jPanel1.add(jButton2);
            jPanel1.add(jButton3);
            jPanel1.add(jButton4);
            jPanel1.add(jButton5);
            jPanel1.add(jButton6);
            jPanel1.add(jButton7);
            jPanel1.add(jButton8);
            jPanel1.add(jButton9);
            jPanel1.add(jButton10);    }    public static void main(String[] args) {
            Frame1 frame = new Frame1();
            frame.show();
        }    JScrollPane jScrollPane1 = new JScrollPane();
        JPanel jPanel1 = new JPanel();
        FlowLayout flowLayout1 = new FlowLayout();
        JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        JButton jButton3 = new JButton();
        JButton jButton4 = new JButton();
        JButton jButton5 = new JButton();
        JButton jButton6 = new JButton();
        JButton jButton7 = new JButton();
        JButton jButton8 = new JButton();
        JButton jButton9 = new JButton();
        JButton jButton10 = new JButton();}
      

  3.   

    谢谢3楼的,但我说的是自己定义的一个面版,不是以有的控件,(JButton).package com.hydrology.form;import java.awt.BorderLayout;import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class Frame1 extends JFrame {
        public Frame1() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    private void jbInit() throws Exception {
            getContentPane().setLayout(null);
            jScrollPane1.setBounds(new Rectangle(4, 7, 522, 494));
            this.getContentPane().add(jScrollPane1);
            jButton1.setText("jButton1");
            jPanel1.setLayout(flowLayout1);
            jScrollPane1.getViewport().add(jPanel1);
            jButton2.setText("jButton2");
            jButton3.setText("jButton3");
            jButton4.setText("jButton4");
            jButton5.setText("jButton5");
            jButton6.setText("jButton6");
            jButton7.setText("jButton7");
            jButton8.setText("jButton8");
            jButton9.setText("jButton9");
            jButton10.setText("jButton10");
            jPanel1.add(jButton1);
            jPanel1.add(jButton2);
            jPanel1.add(jButton3);
            jPanel1.add(jButton4);
            jPanel1.add(jButton5);
            jPanel1.add(jButton6);
            jPanel1.add(jButton7);
            jPanel1.add(jButton8);
            jPanel1.add(jButton9);
            jPanel1.add(jButton10);
            jPanel1.add(panel1);
            jPanel1.add(panel2);
        }    public static void main(String[] args) {
            Frame1 frame = new Frame1();
            frame.setVisible(true);
        }    JScrollPane jScrollPane1 = new JScrollPane();
        JPanel jPanel1 = new JPanel();
        FlowLayout flowLayout1 = new FlowLayout();
        JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        JButton jButton3 = new JButton();
        JButton jButton4 = new JButton();
        JButton jButton5 = new JButton();
        JButton jButton6 = new JButton();
        JButton jButton7 = new JButton();
        JButton jButton8 = new JButton();
        JButton jButton9 = new JButton();
        JButton jButton10 = new JButton();
        Panel1 panel1=new Panel1();
        Panel1 panel2=new Panel1();}
    如我自己做的一个子界面package com.hydrology.form;import javax.swing.*;
    import java.awt.Rectangle;public class Panel1 extends JPanel
    {
        public Panel1()
        {
            try
            {
                jbInit();
            } catch (Exception ex)
            {
                ex.printStackTrace();
            }    }    private void jbInit() throws Exception
        {
            this.setLayout(null);
            jButton1.setBounds(new Rectangle(73, 72, 117, 59));
            jButton1.setText("jButton1");
            jToggleButton1.setText("jToggleButton1");
            jToggleButton1.setBounds(new Rectangle(73, 174, 157, 57));
            this.add(jButton1);
            this.add(jToggleButton1);
        }    JButton jButton1 = new JButton();
        JToggleButton jToggleButton1 = new JToggleButton();}
      

  4.   

    不好意思,没说清楚,流式布局好像只对JButton,那种已经有的控件有用,像我做一个学生资料的显示,用一个ViewStu继承JPanel,在上面有一些信息,有很多控件(JTable,JLabel...),我从数据库读取学生的信息,在上面显示出来,我希望的是有几条数据,就在一个窗体上new 出几个ViewStu的面板,但用流式布局就显示不出来,我现在用的是网格布局,能显示出来,但网格只能显示一个屏幕的大小,不能家滚动JScrollPanel,怎么解决,要代码说一下
      

  5.   

    jbInit() 函数里面底部添加以下几句再试试看能不能显示。
    setSize(300,300);//数字自己根据你自己需要修改
    setVisible(true);