0
我有一個問題。我有這個字符的聲明,但我找不到一個功能的作品....(Java)如何繪製單個字符
如何在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")
;
}
}
當你說「好好嘗試一下drawString之似乎工作「,你的意思是它給出了一個錯誤:預期的字符串參數,但發現字符?或者你的意思是'drawString-method'即使你放入一個String-char也不會畫出來?如果是第一種情況,請使用:'g.drawString(String.valueOf(yourChar));'而不是。如果是第二種情況,我不知道。 PS:Ik zou Engelse en Nederlandse viarbelen niet door elkaar mixen(aangezien ik'woord' en'gok' zie,i.p.v.'word' en' guess'。) – 2014-10-29 09:40:38
Tja,iedereen kan Nederlands,toch? – BarrySW19 2014-10-29 10:00:12