java中drawstring的用法
时间: 2023-06-05 16:47:58 浏览: 408
drawString是Java中Graphics类的一个方法,用于在图形界面上绘制字符串。它的语法是:
public void drawString(String str, int x, int y)
其中,str是要绘制的字符串,x和y是字符串的左上角坐标。此外,还可以使用其他重载方法来设置字体、颜色、对齐方式等。
例如,以下代码将在(100,100)处绘制字符串"Hello, world!":
Graphics g = getGraphics();
g.drawString("Hello, world!", 100, 100);
相关问题
java中drawstring设置字体
在Java中,我们可以使用Graphics类的drawString方法来绘制字符串,同时可以使用Font类设置字体。以下是一个简单的例子:
```
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawStringExample extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Font font = new Font("Arial", Font.BOLD, 20);
g.setFont(font);
g.drawString("Hello, World!", 50, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame("DrawString Example");
frame.add(new DrawStringExample());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
```
在上面的例子中,我们使用Font类创建了一个名为Arial、粗体、大小为20的字体,并使用Graphics类的setFont方法将这个字体设置为绘制字符串时使用的字体。接着,我们使用Graphics类的drawString方法在(50,50)位置绘制了一段字符串"Hello, World!"。
java的drawstring的用法
Java中的drawString()方法用于在指定的位置绘制一个字符串。它的语法如下:
public void drawString(String str, int x, int y)
其中,str是要绘制的字符串,x和y是字符串的左上角的坐标。
除此之外,还有几个参数可以用来指定字体、颜色等:
public void drawString(String str, int x, int y)
public void drawString(String str, int x, int y, Font font)
public void drawString(String str, int x, int y, Color color)
public void drawString(String str, int x, int y, Font font, Color color)
其中,font表示要使用的字体,color表示要使用的颜色。
例如:
```
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawStringExample extends JPanel {
public void paint(Graphics g) {
g.setFont(new Font("TimesRoman", Font.PLAIN, 20)); // 设置字体
g.setColor(Color.RED); // 设置颜色
g.drawString("Hello, World!", 50, 50); // 绘制字符串
}
public static void main(String[] args) {
JFrame frame = new JFrame("DrawString Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawStringExample());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
```
这段代码会在一个窗口中绘制一个红色的“Hello, World!”字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)