這篇文章將為大家詳細講解有關(guān)怎么在Java中利用JScrollPane實現(xiàn)一個面板滾動功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
JScrollPane 面板是帶滾動條的面板,它也是一種容器,但是 JScrollPane 只能放置一個組件,并不可以使用布局管理器。如果需要在 JScrollPane 面板上放置多個組件,需要將多個組件放置在 JPanel 上,然后將 JPanel 面板作為一個整體組件添加在 JScrollPane 組件上。這點大家一定要注意!下面我們通過一個實例來了解下它的使用方法和技巧。
源碼:
public class JscrollPaneDemo extends JFrame{ private JPanel contentPane; private JScrollPane scrollPane; private JTextArea textArea; public JscrollPaneDemo(){ contentPane=new JPanel(); contentPane.setBorder(new EmptyBorder(5,5,5,5)); contentPane.setLayout(new BorderLayout(0,0)); this.setContentPane(contentPane); scrollPane=new JScrollPane(); contentPane.add(scrollPane,BorderLayout.CENTER); textArea=new JTextArea(); //scrollPane.add(textArea); scrollPane.setViewportView(textArea); this.setTitle("滾動面板使用"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(100, 100, 250, 200); this.setVisible(true); } public static void main(String []args){ @SuppressWarnings("unused") JscrollPaneDemo example=new JscrollPaneDemo(); } }