swing 實現帶滾動類的組件四步
1.有一個空白的面板JPanel
2.添加一個滾動面板JScrollPane
3.面板與組件聯系起來setViewportView(component)
4.在面板上添加滾動面板。
下面分別就這四步給出代碼。
為了方便程序中調用封裝一個顯示滾動文字的panel.
public class PrintColorPanel extends JPanel { /** * O */ private static final long serialVersionUID = 786968642266699123L; public JTextPane textPane; public JScrollPane scrollPane; /** * Create the panel. */ public PrintColorPanel(int width,int height) { setLayout(null); //設置面板大小 setSize(new Dimension(width,height)); scrollPane = new JScrollPane(); scrollPane.setBounds(5, 5, width-10, height-10); //設置滾動面板大小 textPane = new JTextPane(); textPane.setEditable(false);
//將滾動面板與textPane聯系起來 scrollPane.setViewportView(textPane); //添加到默認的panel上 add(scrollPane); } }
新建panel進行測試調用。
import java.awt.BorderLayout; public class TestMain extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TestMain frame = new TestMain(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public TestMain() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 337); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); //添加滾動面板 PrintColorPanel pcp = new PrintColorPanel(400,280); getContentPane().add(pcp); } }
運行圖示:

到此為止只是將滾動的面板顯示了出來。因為里面沒有文字所以沒有出現滾動的效果。因為后期我們要在面板中顯示文字所以重新封裝下滾動面板。
import java.awt.Color; public class PrintColorPanel extends JPanel { /** * O */ private static final long serialVersionUID = 786968642266699123L; public JTextPane textPane; public JScrollPane scrollPane; /** * Create the panel. */ public PrintColorPanel(int width,int height) { setLayout(null); setSize(new Dimension(width,height)); scrollPane = new JScrollPane(); scrollPane.setBounds(5, 5, width-10, height-10); textPane = new JTextPane(); Style def = textPane.getStyledDocument().addStyle(null, null); StyleConstants.setFontFamily(def, "verdana"); StyleConstants.setFontSize(def, 12); Style normal = textPane.addStyle("normal", def); Style s = textPane.addStyle("red", normal); Style s1 = textPane.addStyle("green", normal); StyleConstants.setForeground(s, Color.RED); StyleConstants.setForeground(s1, new Color(0,205,0)); textPane.setParagraphAttributes(normal, true); textPane.setEditable(false); scrollPane.setViewportView(textPane); add(scrollPane); } /** * 追加字和顏色 */ public void appendSting(String str){ try { textPane.getDocument().insertString(textPane.getDocument().getLength(), str, textPane.getStyle("normal")); textPane.setCaretPosition(textPane.getDocument().getLength()); } catch (BadLocationException e) { e.printStackTrace(); } } /** * 追加字和顏色 */ public void appendSting(String str,String color){ try { textPane.getDocument().insertString(textPane.getDocument().getLength(), str, textPane.getStyle(color)); textPane.setCaretPosition(textPane.getDocument().getLength()); } catch (BadLocationException e) { e.printStackTrace(); } } public void setTextPaneNull(){ textPane.setText(""); } }
再次進行測試:
import java.awt.BorderLayout; public class TestMain extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TestMain frame = new TestMain(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public TestMain() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 337); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); //添加滾動面板 PrintColorPanel pcp = new PrintColorPanel(400,280); getContentPane().add(pcp); for(int i=0;i<1000;i++){ if(i%2==0){ pcp.appendSting(i+"是偶數\n", "red"); }else{ pcp.appendSting(i+"是奇數\n", "green"); } } } }

