This is my code
first I setpreffered size of jpanel
after that setpreffred size of frame and in set jpanels prefferder size as parameter

[CODE]
JFrame frame = new JFrame();
frame.setBackground(Color.darkGray);
JPanel jp=new Test();
jp.setPreferredSize(new Dimension(500,500));

    JScrollPane pane=new JScrollPane(jp,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.setPreferredSize(jp.getPreferredSize());
    frame.add(pane);

[/CODE]

Thank u!