添加滚动的长篇文字描述
我采用的方法是使用JLabel框中添加html代码,这样就可以;
添加长篇文字
主要语句
String txt="<html>\r\n" +
" <body>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" </body>\r\n" +
"</html>";
JLabel lblNewLabel = new JLabel (txt);
添加可以滚动的滚动条
主要语句
//创建滚动区域
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 426, 253);
//滚动区域添加到面板中
contentPane.add(scrollPane);
//将长篇文字放到滚动面板中
scrollPane.setViewportView(lblNewLabel);
完整代码
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
public class help extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
help frame = new help();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public help() {
setAutoRequestFocus(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 426, 253);
contentPane.add(scrollPane);
String txt="<html>\r\n" +
" <body>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" <h2>加油</h2>\r\n" +
" <p>我们一起加油</p>\r\n" +
" </body>\r\n" +
"</html>";
JLabel lblNewLabel = new JLabel (txt);
scrollPane.setViewportView(lblNewLabel);
}
}