请不要责怪swing.
给出你的代码.
让我们来help you.

解决方案 »

  1.   

    你要更新一下
    SwingUtilities.updateComponentTreeUI(this);
      

  2.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class scrollPosTester extends JFrame {
       private JButton jButton1 = new JButton();
       private JScrollPane jScrollPane1 = new JScrollPane();   public scrollPosTester() {
          try {
             jButton1.setText("Hide");
             jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   jButton1_actionPerformed(e);
                }
             });
             jScrollPane1.getViewport().setBackground(Color.red);
             this.getContentPane().add(jButton1, BorderLayout.NORTH);
             this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
          }
          catch(Exception e) {
             e.printStackTrace();
          }
       }   void jButton1_actionPerformed(ActionEvent e) {
          jScrollPane1.setVisible(!jScrollPane1.isVisible());
          jButton1.setText(jScrollPane1.isVisible()?"Hide":"Show");   }   public static void main(String[] args){
          scrollPosTester s = new scrollPosTester();
          s.setSize(300,300);
          s.setVisible(true);
       }
    }
      

  3.   

    在初始化该JButton的父容器时先不要添加JScrollPane(或JPanel)。在按下JButton后,重新初始化JButton的父容器的Layout风格,把JScrollPane(或JPanel)加到其中,然后重画(repaint或validate)该父容器。
      

  4.   

    问题解决,又加了两个jPanel,这样有三个 jPanel:一个放 jButton,一个放 jScrollPane,第三个放前两个 jPanel。当然离不开 validate()、repaint()、addNotify()这些东东。这样处理效果比较理想。谢谢各位关注。