简体   繁体   English

Java中的滚动文本

[英]Scrolling text in Java

I'm working on a scrolling text which runs from left to right, it works perfectly fine, but I can't figure it out, how to change the size font and text color, could someone tell me please how to change the text size, font and color.我正在处理从左到右运行的滚动文本,它工作得很好,但我无法弄清楚,如何更改大小字体和文本颜色,有人可以告诉我如何更改文本大小、 字体和颜色。 Should I set the panel's background?我应该设置面板的背景吗? or the frame?还是框架? or the label?还是 label? The question seems simple but I'm still new and learning这个问题看起来很简单,但我还是新手,还在学习

Here is the code:这是代码:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class RunningText extends JPanel {

  int x;
  int y;
  String text;

  public RunningText() {
    JLabel label = new JLabel();
    x = -45;
    y = 150;
    text = "DSTU";
    setSize(400, 300);
  }

  public void paint(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 300);
    g.setColor(Color.BLACK);
    g.drawString(text, x, y);
    System.out.println(x + " " + y);
  }

  public void start() throws InterruptedException {
    while (true) {
      while (x <= getWidth()) {
        x++;
        y = getHeight() / 2;
        repaint();
        Thread.sleep(10);
      }
    }
  }

  public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame();
    RunningText run = new RunningText();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(run);
    frame.setSize(400, 300);
    frame.setVisible(true);
    run.start();

  }
}

for change the color of the text use by the second time the method setColor() in the Graphics object and for change the Font use the method setFont() in the Graphics object更改文本颜色第二次使用图形 object 中的方法 setColor() 和更改字体使用图形 object 中的方法 setFont()

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, 400, 300);
        g.setColor(Color.RED);
        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20)); //changes the font to sans serif, make it bold and set the font size in twenty
        g.drawString(text, x, y);
        System.out.println(x + " " + y);
    }

postscript: the infinite while is useless后记:无限的时间是无用的

    public void start() throws InterruptedException {

        while (x <= getWidth()) {
            x++;
            y = getHeight() / 2;
            repaint();
            Thread.sleep(10);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM