初学Java,在书上看到一个例子
import java.awt.*;import javax.swing.*;public class ch {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Room");
frame.setSize(400, 400);
Container w=frame.getContentPane();
Graphics g=w.getGraphics();
g.drawRect(50,50, 100, 100);
}
}
请问,为什么没有图形输出,是输出后马上就消失了还是根本就没有输出图形?希望大家帮忙,谢谢?
import java.awt.*;import javax.swing.*;public class ch {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Room");
frame.setSize(400, 400);
Container w=frame.getContentPane();
Graphics g=w.getGraphics();
g.drawRect(50,50, 100, 100);
}
}
请问,为什么没有图形输出,是输出后马上就消失了还是根本就没有输出图形?希望大家帮忙,谢谢?
@Override
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.drawRect(50,50, 100, 100);
}
public static void main(String[] args){
JFrame frame = new Ch();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Room");
frame.setSize(400, 400);
}
}