在設(shè)置界面時,可能會遇到一個較小的容器窗體中顯示一個較大部分的內(nèi)容的情況,這時候可以使用 JScrollPane 面板。
JScrollPane 面板是帶滾動條的面板,它也是一種容器,但是 JScrollPane 只能放置一個組件,并不可以使用布局管理器。如果需要在 JScrollPane 面板上放置多個組件,需要將多個組件放置在 JPanel 上,然后將 JPanel 面板作為一個整體組件添加在 JScrollPane 組件上。這點大家一定要注意!下面我們通過一個實例來了解下它的使用方法和技巧。
源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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(); } } |
建議朋友們在學(xué)習(xí)的過程中盡量不要復(fù)制代碼,要親自動手打,特別是初學(xué)者。一方面可以加深理解,一方面還可以練習(xí)打代碼的速度,作為程序員來說,不僅要有好的編程習(xí)慣和水平,也要有速度。
以上就是關(guān)于Java常用面板JScrollPane的實例和對大家學(xué)習(xí)編程的小小建議,喜歡的朋友請繼續(xù)關(guān)注服務(wù)器之家!
原文鏈接:http://www.2cto.com/kf/201405/297419.html