Java paint方法为什么不能画出图像? 5
packagetest;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.event.WindowA...
package test;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Frame01 extends Frame{
public void launchFrame(){
setSize(300, 300);
setLocation(100,100);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.drawLine(10,10,20 ,20 );
}
public static void main(String[] args) {
new Frame01().launchFrame();
}
}
为什么不能画出图像? 展开
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Frame01 extends Frame{
public void launchFrame(){
setSize(300, 300);
setLocation(100,100);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.drawLine(10,10,20 ,20 );
}
public static void main(String[] args) {
new Frame01().launchFrame();
}
}
为什么不能画出图像? 展开
展开全部
Frame是窗体,需要在其中添加 Jpanel 等组件。(标识符中用数字时最好用下划线连接,如 Frame_01)
public void launchFrame(){
setSize(300, 300);
setLocation(100,100);
this.add(new Panel_01());
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class Panel_01 extends JPanel{
@Override
public void paint(Graphics g) {
g.drawLine(10,10,20 ,20 );
}
}
public void launchFrame(){
setSize(300, 300);
setLocation(100,100);
this.add(new Panel_01());
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class Panel_01 extends JPanel{
@Override
public void paint(Graphics g) {
g.drawLine(10,10,20 ,20 );
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询