如何将滚动窗格添加到我的框架

时间:2014-04-16 14:05:32

标签: java

我想在gui中添加一个滚动窗格到我框架的右侧。有人可以  给我代码?我不知道是否有必要导入除javax.swing之外的其他内容。以下是主要内容。我希望你能读懂它。谢谢。

import javax.swing.JFrame;
import javax.swing.*;


public class School {

    public static void main (String[] args) {

        Test t= new Test();
        JFrame f = new JFrame ("Calculus");

        f.add(t);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize (530, 700);
        f.setVisible (true); 

    }
} 

1 个答案:

答案 0 :(得分:0)

创建JPanel,然后将其添加到JScrollPane,然后将此JScrollPane添加到您的JFrame

例如:

JFrame f = new JFrame ("Calculus");
JPanel panel = new JPanel();
Test t= new Test();
panel.add(t);// add the component you want to be scrolling to the scroll pane.
JScrollPane scrollFrame = new JScrollPane(panel, 
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.add(scrollFrame);