添加滚动的长篇文字描述

本文介绍了如何在Java应用中利用JLabel和HTML代码来创建长篇文字的滚动效果,包括主要语句的阐述,完整的代码示例以及实际展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加滚动的长篇文字描述

我采用的方法是使用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);
	}
}

展示

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值