java 尝试使用 drawString 方法打印名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13825582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:06:07  来源:igfitidea点击:

Trying to use drawString method to print a name

javagraphicsdrawstring

提问by John Dawson

I'm just going through some basic tutorials at the moment. The current one wants a graphics program that draws your name in red. I've tried to make a NameComponent Class which extends JComponent, and uses the drawString() method to do this:

我现在只是在学习一些基本的教程。当前的人想要一个图形程序,用红色画出你的名字。我试图创建一个扩展 JComponent 的 NameComponent 类,并使用 drawString() 方法来做到这一点:

import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JComponent;

public class NameComponent extends JComponent {

    public void paintMessage(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(Color.RED);
    g2.drawString("John", 5, 175);

    }
}

and use a NameViewer Class which makes use of JFrame to display the name:

并使用 NameViewer 类,它利用 JFrame 显示名称:

import javax.swing.JFrame;

public class NameViewer {

public static void main (String[] args) {

    JFrame myFrame = new JFrame();
    myFrame.setSize(400, 200);
    myFrame.setTitle("Name Viewer");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    NameComponent myName = new NameComponent();
    myFrame.add(myName);

    myFrame.setVisible(true);
    }
} 

...but when I run it, the frame comes up blank! Could anyone let me know what I'm doing wrong here?

...但是当我运行它时,框架变成了空白!谁能让我知道我在这里做错了什么?

Thanks a lot!

非常感谢!

采纳答案by Reimeus

You need to override the method paintComponentrather than paintMessage. Adding the @Overrideannotation over the method will show that paintMessageis not a standard method of JComponent. Also you may want to reduce the y-coordinate in your drawStringas the text is currently not visible due to the additional decoration dimensions of the JFrame. Finally remember to call super.paintComponentto repaint the background of the component.

您需要覆盖该方法paintComponent而不是paintMessage. @Override在方法上添加注释将表明它paintMessage不是 的标准方法JComponent。此外,您可能希望减少您的 y 坐标,drawString因为文本当前不可见,因为JFrame. 最后记得调用super.paintComponent重新绘制组件的背景。

@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2 = (Graphics2D) g;
   g2.setColor(Color.RED);
   g2.drawString("John", 5, 100);
}

See: Painting in AWT and Swing

请参阅:在 AWT 和 Swing 中绘画

回答by imulsion

You need to add this line after public void paintMessage(Graphics g){:

您需要在以下内容之后添加此行public void paintMessage(Graphics g){

super.paint(g);

super.paint(g);

This tells Java to use the superclass (JComponent) to paint the message.

这告诉 Java 使用超类 (JComponent) 来绘制消息。

You will also need to call your method paintComponents()rather than paintMessage()

您还需要调用您的方法paintComponents()而不是paintMessage()