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"); } } } }