JScrollPane - 到 JFrame 或 JPanel?

JScrollPane - to JFrame or JPanel?

(使用 netbeans) 因此,对于我的项目,我需要添加一个 JscrollPane 以便用户可以看到所有 JTextArea 输出、饼图和我添加的两个按钮。这是我实现 JscrollPane 的代码。但是,它导致程序不再生成输出屏幕。我的问题是我是否需要将 JscrollPane 添加到 JPanel 或 JFrame 中,如果需要,我做错了什么(试图包含尽可能多的我认为相关的代码)

P.S 我应该从 Borderlayout 改为 Boxlayout 吗?这对添加 jscroll 有影响吗?

    JFrame frame1 = new JFrame("Portfolio Results");


        frame1.setSize(800,800);
       // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);







       // output screen declartions 
       frame1.setLayout(new BorderLayout());

       JPanel panel1 = new JPanel();
       frame1.add(panel1,BorderLayout.PAGE_START);


        panel1.setLayout(new BorderLayout());


        JTextArea area1 = new JTextArea();
        area1.setPreferredSize(new Dimension(600,600));
        panel1.add(area1,BorderLayout.PAGE_START);

        JScrollPane scp1 = new JScrollPane(frame1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        frame1.add(scp1);

        //code for Pie chart and two button
         DefaultPieDataset piedata = new DefaultPieDataset();
         piedata.setValue("test", new Integer (100));
         JFreeChart chart = ChartFactory.createPieChart("test", piedata,      true, true, true);

        PiePlot p = (PiePlot)chart.getPlot();
        ChartPanel testpan = new ChartPanel(chart);
        panel1.add(testpan,BorderLayout.CENTER);



       JButton button= new JButton("SAVE");

      // button.setPreferredSize(new Dimension(80,20));
      // Listener listener = new Listener();
    //   button.addActionListener(this);
       panel1.add(button,BorderLayout.PAGE_END);


       JButton pbutton=new JButton("Print");
       panel1.add(pbutton,BorderLayout.SOUTH);

您应该使用要滚动的对象初始化 JScrollPane

在您的示例中,JTextArea 似乎是您想要的对象,因此:

JScrollPane scp1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

来自Oracle docs:

 JScrollPane(Component view) 

Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.

此外,请参阅此 Oracle example