想要在文本框输入数字作为柱的高度, 另外一个jContentPane立即更新这个柱文本框部分:
jTextField.addKeyListener(new java.awt.event.KeyAdapter() 
{
public void keyReleased(java.awt.event.KeyEvent e) 
{
System.out.println("keyReleased()"); 
height = Integer.parseInt(jTextField.getText());
System.out.println(height); 
jContentPane1.add(new Bar(height));    //这一句话无法执行, 为什么?
}
});

解决方案 »

  1.   

    语法没错, 就是运行的时候jContentPane1没有反应
      

  2.   

    在jContentPane1部分写
    jContentPane1.add(new Bar(height));就可以执行
      

  3.   

    只是调用bar.setHeight(height)不好吗 
    updateUI
    repaint
    这两个方法都试试
      

  4.   

    swing不精,但是觉得你这个代码不对。
    如果是重新绘制这个柱型的话,为什么用jContentPane1.add(new Bar(height));这个语句呢?
    如果是这样写的话,下次再填一个高度,是不是又要new一个Bar添加到jContentPane1中?
    应该是改变jContentPane1里的柱型的高度,然后重新绘制图像才对吧?
      

  5.   

    jContentPane1.add(new Bar(height));    //这一句话无法执行, 为什么? 
    执行了,但可能看不出效果界面并没有重画