我在图形方面非常缺乏经验,所以我尝试了一个教程,最终得到了这个:
import java.awt.Canvas;
import java.awt.Graphics;
public class Ide extends Canvas implements Runnable{
private static final long serialVersionUID = 6804608149790007396L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private boolean running = false;
Graphics gr;
public Ide(){
new Window(WIDTH,HEIGHT,"Ide v0.1",this);
}
public synchronized void start(){
running = true;
gr = this.getGraphics();
gr.drawString("Hello", 100, 20);
}
public synchronized void stop(){
running = false;
}
public static void main(String[] args) {
new Ide();
}
}
而这个:
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Window extends Canvas{
private static final long serialVersionUID = -8255319694373975038L;
public Window(int w, int h, String title, Minecraft_Function_Ide ide){
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(w,h));
frame.setMaximumSize(new Dimension(w,h));
frame.setMinimumSize(new Dimension(w,h));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(ide);
frame.setVisible(true);
ide.start();
}
"Hello"之前工作正常,但现在不再工作了,我不确定我是否只是缺少某种更新语句,或者我是否完全做了错误的事情。
有更有经验的人想帮忙吗?
问题可能是您从未在gr
对象上调用 paint()
方法。但这并不能解释为什么你首先让它工作。
尝试在start()
方法的末尾添加以下行:
gr.paint();