gpt4 book ai didi

java - (Java)如何绘制单个字符

转载 作者:行者123 更新时间:2023-12-01 22:37:53 26 4
gpt4 key购买 nike

我有一个问题。我声明了这个字符,但找不到有效的函数......

如何在 GUI 中绘制单个字符? :)drawString 似乎不起作用,drawChars 需要一个数组:S

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener {
private JPanel p;
private JTextField tf;
private JLabel lb;
private JButton b, bstart;
private String woord = "woord";
private int x = 0;
private int y = 0;
private Font f = new Font("Courier", Font.PLAIN,60);
private int count;
private int y = 60;

public MyFrame() {
setLayout(new FlowLayout());

p = new JPanel();
p.setPreferredSize(new Dimension(500, 600));
add(p);
p.setOpaque(true);
p.setBackground(Color.BLUE);

bstart = new JButton("start");
add(bstart);
bstart.addActionListener(this);
lb = new JLabel("Voer uw woord in: ");
add(lb);
tf = new JTextField(8);
add(tf);
b = new JButton("Kraken");
add(b);
b.addActionListener(this);

setSize(150, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent event) {
Graphics g = p.getGraphics();
if (event.getSource() == bstart) {
g.setColor(Color.WHITE);
// g.drawRect(25, 25, 500, 500);
// g.drawLine(500, 425, 25, 425);
// g.drawLine(500, 325, 25, 325);
// g.drawLine(500, 225, 25, 225);
// g.drawLine(500, 125, 25, 125);

for (int i = 0; i < 5; i++) {
for (int z = 0; z < 5; z++) {
g.drawRect(x,y,100,100);
x = x + 100;
}
x = 0; y = y+100;
}
g.setFont(f);
g.drawString("W", 30, 60);

}
if(event.getSource() == b){
count++;
String gok = tf.getText();
g.setColor(Color.RED);
g.fillRect(0,0,100,100);
g.setColor(Color.white);
g.setFont(f);
g.drawString("W", 30, 60);
char secondLetter = gok.charAt(1);
char thirdLetter = gok.charAt(2);
char fourthLetter = gok.charAt(3);
char lastLetter = gok.charAt(4);
g.drawString(secondLetter,130, y);

}

String input = tf.getText();
if (input.toLowerCase() == "woord")
;
}
}

最佳答案

为什么不直接转换为字符串:

char c = '#';
g.drawString(String.valueOf(c), x, y);

关于java - (Java)如何绘制单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26626756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com