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