都来看啊,一个简单的问题,为什么不能显示文字啊?

来源:百度知道 编辑:UC知道 时间:2024/05/07 07:20:18
今天按照书上的代码写了如下程序:
import java.awt.*;
import javax.swing.*;

public class Bushiyigezi{
public static void main(String arg []){

Bushiyigeziframe frame = new Bushiyigeziframe();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
}

class Bushiyigeziframe extends JFrame{
public void Bushiyigeziframe(){
setTitle("你好啊");
setSize(X, Y );
BushiyigeziPanel panel = new BushiyigeziPanel();
Container contentPane = getContentPane();
contentPane.add(panel);

}
public static final int X=300;
public static final int Y=200; }

class BushiyigeziPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString( " 你好啊", B, C);

public void Bushiyigeziframe(){
setTitle("你好啊");
setSize(X, Y );
BushiyigeziPanel panel = new BushiyigeziPanel();
Container contentPane = getContentPane();
contentPane.add(panel);

}

这个方法是构造方法,不要加void
这样你再将窗口拉大的话,相当于画布重新布置了一次.画的那几个字自然也就不见了.而你这个就是根本没有用到下面的方法.相当于没有画
将其删除掉就可以了~

public Bushiyigeziframe(){
setTitle("你好啊");
setSize(X, Y );
BushiyigeziPanel panel = new BushiyigeziPanel();
Container contentPane = getContentPane();
contentPane.add(panel);

}

太乱了