在Java Swing应用程序中,如何创建和使用JScrollPane

・2 分钟阅读

摘要:一个简单的Java JScrollPane源代码例子。

下面是一个完整的Java类的源代码,它演示了如何使用JScrollPane ,此例子演示如何将滚动窗格与jtextArea和JFrame一起使用 :

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
/**
 * A complete Java class to demonstrate the use of a JScrollPane.
 * 
 * @author alvin alexander, devdaily.com.
 *
 */
public class JScrollPaneDemo
{
 public static void main(String[] args)
 {
 new JScrollPaneDemo();
 }
 public JScrollPaneDemo()
 {
 SwingUtilities.invokeLater(new Runnable()
 {
 public void run()
 {
 // create a jtextarea
 JTextArea textArea = new JTextArea();
 // add text to it; we want to make it scroll
 textArea.setText("xxnxxnxxnxxnxxnxxnxxnxxnxxnxxnxxnxxnxxnxxn");
 // create a scrollpane, givin it the textarea as a constructor argument
 JScrollPane scrollPane = new JScrollPane(textArea);
 // now add the scrollpane to the jframe's content pane, specifically
 // placing it in the center of the jframe's borderlayout
 JFrame frame = new JFrame("JScrollPane Test");
 frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
 // make it easy to close the application
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 // set the frame size (you'll usually want to call frame.pack())
 frame.setSize(new Dimension(240, 180));
 // center the frame
 frame.setLocationRelativeTo(null);
 // make it visible to the user
 frame.setVisible(true);
 }
 });
 }
}

Xn_warm profile image