java - JScrollPane - 到 JFrame 还是 JPanel?

标签 java netbeans jscrollpane

(使用 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 .

关于java - JScrollPane - 到 JFrame 还是 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40882267/

相关文章:

java - 将 JScrollPane 添加到 JList

java - 在 servlet 中获取过滤器初始化参数

java - 与线程的可变干扰

java - 在双引号内分割字符串

java - 在 Netbeans 中设置 SDK 位置

ide - 禁用NetBeans中的自动构建

java - 时间和日期检查

css - 开始使用 LESS

java - 添加 JScrollpane,其中 JPanel 比 JFrame 大

java - JTable调整列宽或高