jpanel中的graphics中所画得图像不能显示出来
public class TestGui extends BaseJFrame {
private JPanel jPanelGraphics;// 底板显示线性表的当前元素。
public TestGui() {
this.setLayout(new BorderLayout());
this.setVisible(true);
this.setSize(600, 400);
this.setTitle("线性表");
jPanelGraphics = new JPanel();
this.add(jPanelGraphics);
infoMiddleJpanel(jPanelGraphics);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 注入图像
public void infoMiddleJpanel(JPanel jPanel){
MyList ml = new MyList();
ml.add("a");
ml.add("b");
ml.add("c");
ml.add("d");
paintClear(jPanel);
paintList(ml,jPanel);
}
// 功能是将底板jpanel清空。
public void paintClear(JPanel jPanel){
Graphics g = jPanel.getGraphics();
Color c = jPanel.getBackground();
g.setColor(c); // 用按钮色将绘图底板清空。
g.fillRect(0, 0, jPanel.getWidth(), jPanel.getHeight());
}
// 功能是将线性表中的元素在底板jpanel中显示。
public void paintList(MyList ml,JPanel jPanel){
int ix,iy;
String s;
Graphics g = jPanel.getGraphics();
// paintClear(jPanel);
g.setColor(Color.BLACK);
ix = 50;
iy = 50;
if(ml.length()>0){
for(int i=0;i<ml.length();i++){
// 1:取出第 i个元素存入s
s = ml.getElement(i).toString();
System.out.println(s);
// 2:画矩形表示数据元素
g.drawRect(ix, iy, 30, 40);
// 3:在矩形中标记文字
g.drawString(s, ix+10, iy+25);
// 4:准备画一个数据元素。
ix = ix +40;
}
}
}
public static void main(String[] args) {
new TestGui();
}
}
------解决方案--------------------
this.add(jPanelGraphics);
this.getContentPane.add();