日期:2014-05-20  浏览次数:20757 次

求高手解决关于将Jpanel放到JScrollPanel滑块不出现,下面是我的代码
Java code
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Administrator
 */
public class PrintTest extends JFrame{
    JMenu menu=new JMenu("打印");
    JMenuBar bar=new JMenuBar();
    JMenuItem Print=new JMenuItem("打印表");
        JButton btnPrint=new JButton("打印");
        JLabel labname=new JLabel("姓名:");
        JLabel labname1=new JLabel("朱宏堂");
        JLabel labsex=new JLabel("性别:");
        JLabel labsex1=new JLabel("男");
        JLabel labdept=new JLabel("部门:");
        JLabel labdept1=new JLabel("09计算机");
        JLabel labNum=new JLabel("学号:");
        JLabel labNum1=new JLabel("0982038:");
        JLabel labData=new JLabel("发证日期:");
        JLabel labData1=new JLabel("2007年12月");
        Container c=new Container();
        JPanel pan0=new JPanel();
        JPanel pan1=new JPanel();
        JPanel pan2=new JPanel();
        JPanel pan3=new JPanel();
        JPanel pan4=new JPanel();
        JPanel pan5=new JPanel();
        JPanel pan6=new JPanel();
        JPanel pan7=new JPanel();
        JPanel pan8=new JPanel();
        JPanel pan9=new JPanel();
        JPanel pan10=new JPanel();
        JPanel pan11=new JPanel();
       
    //    JPanel pan5=new JPanel();
        public PrintTest(){
            menu.add(Print);
            JScrollPane JSpan=new JScrollPane(pan0);

            bar.add(menu);
            setJMenuBar(bar);
            c=this.getContentPane();
            c.setLayout(new BorderLayout());
            c.add(JSpan);//网格
            pan0.setLayout(new GridLayout(5,2,5,5));
            pan1.setLayout(null);
            pan0.setBackground(Color.GRAY);
            
            pan0.add(pan1);
            pan0.add(pan2);
            pan0.add(pan3);
            pan0.add(pan4);
            pan0.add(pan5);
            pan0.add(pan6);
            pan0.add(pan7);
            pan0.add(pan8);
            pan0.add(pan9);
            pan0.add(pan10);

            labname.setBounds(new Rectangle(250,100,100,40));
            labname1.setBounds(new Rectangle(350,100,150,40));
            labsex.setBounds(new Rectangle(250,120,150,40));
            labsex1.setBounds(new Rectangle(350,120,150,40));
            labdept.setBounds(new Rectangle(250,140,150,40));
            labdept1.setBounds(new Rectangle(350,140,150,40));
            labNum.setBounds(new Rectangle(250,160,150,40));
            labNum1.setBounds(new Rectangle(350,160,150,40));
            labData.setBounds(new Rectangle(250,180,150,40));
            labData1.setBounds(new Rectangle(350,180,150,40));

            pan1.add(labname);
            pan1.add(labname1);
            pan1.add(labsex);
            pan1.add(labsex1);
            pan1.add(labdept);
            pan1.add(labdept1);
            pan1.add(labNum);
            pan1.add(labNum1);
            pan1.add(labData);
            pan1.add(labData1);
  



            
        }
        public static void main(String[] args) {
        PrintTest pt=new PrintTest();
        pt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pt.setVisible(true);
        pt.setSize(500,400);
        pt.setLocation(300,100);
    }


}



------解决方案--------------------
面板pan0把滑动面板Jpan全部占了当然不会出现滑块,而且在Jpan里又不会创建新的内容
------解决方案--------------------
自定义JPanel类实现Scrollable接口
或者JPanel使用setPreferredSize()来设置它的大小