java - 在 JPanel 或 JTextField 中水平滚动文本(如电视中的新闻栏)
问题描述
谁能给我一些建议,我该怎么做?我在互联网上找到了一些代码,但我不知道如何使用它如何将它添加到我的代码中并使用它,因为该代码自己创建了一个框架。
我怎样才能分配JLabel
给这个代码?
这是代码(我想将它用于我创建的框架上的标签):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
public class JScrollingText extends JLabel {
private int speed;
private int period;
private int offset;
private int x;
public JScrollingText(String text) {
this(text, 1);
}
public JScrollingText(String text, int speed) {
this(text, speed, 100);
}
public JScrollingText(String text, int speed, int period) {
this(text, speed, period, 0);
}
public JScrollingText(String text, int speed, int period, int offset) {
super(text);
this.speed = speed;
this.period = period;
this.offset = offset;
}
public void paintComponent(Graphics g) {
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
g.setColor(getForeground());
FontMetrics fm = g.getFontMetrics();
Insets insets = getInsets();
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
int textWidth = fm.stringWidth(getText());
if (width < textWidth) {
width = textWidth + offset;
}
x %= width;
int textX = insets.left + x;
int textY = insets.top + (height - fm.getHeight()) / 2 + fm.getAscent();
g.drawString(getText(), textX, textY);
g.drawString(getText(), textX + (speed > 0 ? -width : width), textY);
}
public void start() {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
x += speed;
repaint();
}
};
timer.scheduleAtFixedRate(task, 0, period);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
JButton quit = new JButton("Quitter");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(quit);
JScrollingText scrollingText1 = new JScrollingText("Barre des tâches... Pressez le bouton Quitter", -3);
scrollingText1.setBorder(BorderFactory.createEtchedBorder());
scrollingText1.start();
frame.getContentPane().add(scrollingText1, BorderLayout.NORTH);
JScrollingText scrollingText2 = new JScrollingText("Barre des tâches... Pressez le bouton Quitter");
scrollingText2.setBorder(BorderFactory.createEtchedBorder());
scrollingText2.start();
scrollingText2.setBackground(Color.YELLOW);
scrollingText2.setOpaque(true);
frame.getContentPane().add(scrollingText2, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
解决方案
Bienvenu aux les Stacks。
关键线是
frame.getContentPane().add(scrollingText1, BorderLayout.NORTH);
frame.getContentPane().add(scrollingText2, BorderLayout.SOUTH);
您必须.add
在代码中对适当的容器或组件执行类似操作。
您可能必须创建一个JScrollPane
或某种窗格的实例,将其添加到您的 GUI,然后将其添加ScrollingText(s)
到其中。
另外,我认为滚动文本类最好不要使用 J 首字母。这将帮助您(和其他编码人员?)避免认为它是 Java 库的一部分。调用类 ScrollingText 或 RKScrollingText 或类似的。
推荐阅读
- python - 以下代码的时间复杂度是多少-在数组中找到最小的缺失数
- r - 检查A列的值是否存在于B列的同一行或前几行中
- powershell - 从PowerShell中的文件中删除属性
- excel - Excel = "'{"aaa": " & A2 &", "bbb": " & B2 &"}'" CONTACT 字符串到 json
- javascript - 格式化 JavaScript 对象占位符和替换对象
- vba - 按顺序对表单字段进行排序
- javascript - 在 discord.js 中制作带有前缀的命令处理程序时遇到问题
- python - 用 json 文件分割带注释的图像
- unit-testing - 运行 Vite 开发服务器时模拟 ES 模块
- javascript - 在出现 csrf 错误时向用户显示不同的页面