Java绘图字符串在JPanel中

时间:2012-08-11 13:28:47

标签: java methods jpanel draw

我是完整的Java noob,这个问题非常简单。我确实尝试在整个互联网上找到答案但是,没有什么特别是我需要的(如果你知道这个主题的任何教程页面请发布链接我将非常感激。)

基本上我试图在DrawPanel中绘制字符串。我知道我需要在某个地方调用方法才能这样做,但我不知道在哪里。我的绘图面板有方法:

public void drawGuessWord(Graphics g){
    WordsList guessWord = new WordsList();
    String word = guessWord.pickWord();
    g.drawString(word, 20, 20);
}

我想调用该方法,以便在DrawPanel中绘制字符串。

以防这是我的整个DrawPanel:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

public class DrawPanel extends JPanel {

    DrawPanel(){
        Border raisedbevel = BorderFactory.createRaisedBevelBorder();
        Border loweredbevel = BorderFactory.createLoweredBevelBorder();
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createLoweredBevelBorder());
        this.setPreferredSize(new Dimension(200,200));
    }

    public void drawGuessWord(Graphics g){
        WordsList guessWord = new WordsList();
        String word = guessWord.pickWord();
        g.drawString(word, 20, 20);
    }

}

2 个答案:

答案 0 :(得分:0)

public void paint(Graphics g)
{
  g.drawString(word, 20, 20);
}

答案 1 :(得分:0)

除了MimiEAM的解决方案之外,您可能还想阅读