因为我的JScrollPane里有很多的JButton在拉滚动条时,有很明显的刷新跟不上,就是被拉出来的新画面会出现白的部分。
怎么能解决呀!!

解决方案 »

  1.   

    把代碼貼出來看看~    如果沒有錯誤  swing不會出現這樣的狀況
      

  2.   

    主页面代码this.setSize(width, height);
    getContentPane().setLayout(null);
    this.setBackground(Color.BLACK);
    this.setLocale(new java.util.Locale("sq"));
    getContentPane().setForeground(new java.awt.Color(0,128,255));
    this.setVisible(false);
    {
    devicePanel = new JPanel();
    getContentPane().add(devicePanel);
    devicePanel.setBackground(Color.white);
    devicePanel.setLayout(null);
    devicePanel.setName("devicePanel");
    devicePanel.setBounds(0, 0, deviceWidth, deviceHeight);
    {
    jLabel1 = new JLabel();
    devicePanel.add(jLabel1);

    jLabel1.setText(devicetitleString);
    jLabel1.setBounds(7, 7, deviceWidth-10, deviceHeight-10);
    jLabel1.setFont(new java.awt.Font("MS Gothic",0,14));
    }
    }
    {
    headerPane = new GanttHeader();//JScrollPane
    headerPane.setBackground(Color.white);
    headerPane.setBounds(deviceWidth, 0, width-deviceWidth-17, height);
    }
    {
    itemPane = new GanttItem();//JScrollPane

    itemPane.setBounds(0, deviceHeight, width, height-deviceHeight-17);
    }

    {
    ganttChart = new GanttChart();//JScrollPane

    getContentPane().add(ganttChart);
    getContentPane().add(itemPane);
    getContentPane().add(headerPane);
    ganttChart.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
    ganttChart.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    ganttChart.setBounds(deviceWidth-1,deviceHeight,width-deviceWidth,height-deviceHeight);
    JScrollBar   bar   =   ganttChart.getHorizontalScrollBar();  
    JScrollBar   bar1   =   ganttChart.getVerticalScrollBar();


    bar.addAdjustmentListener(new   AdjustmentListener()   
                  {   
                      public   void   adjustmentValueChanged(AdjustmentEvent   e)   
                      {   JScrollBar   bar3   =   headerPane.getHorizontalScrollBar(); 
                        int a=e.getValue();
                        bar3.setValue(a);
                        bar3.addAdjustmentListener(new   AdjustmentListener(){
                         public   void   adjustmentValueChanged(AdjustmentEvent   e)   {
                        headerPane.repaint();}
                        });
                        repaint();
                      }   
                  });
    bar1.addAdjustmentListener(new   AdjustmentListener()   
                  {   
                      public   void   adjustmentValueChanged(AdjustmentEvent   e)   
                      {   
                     JScrollBar   bar2   =   itemPane.getVerticalScrollBar(); 
                     int a=e.getValue();
                     bar2.setValue(a);
                     bar2.addAdjustmentListener(new   AdjustmentListener(){
                             public   void   adjustmentValueChanged(AdjustmentEvent   e)   {
                            itemPane.repaint();}
                            });
                     repaint();
                      }   
                  });

    }

    GanttChart代码protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
    g.fillRect(0, 1,GanttChart.height,GanttChart.chartCount*GanttChart.chartheight);
    g.setColor(Color.YELLOW);
    g.setFont(new Font("", Font.BOLD, 12));
    int xx;
    JButton jButton=null;
    for(int i=1;i<=GanttChart.chartCount;i++)
    {
    xx = i * GanttChart.chartheight;
    GanttChart.chartheight,GanttHeader.titlePanelWidth,xx);
    jButton=new JButton();
    jButton.setSize(this.getSize().width/2,10);
    jButton.setBounds(50,xx-35,GanttHeader.titlePanelWidth/2,15);

    this.add(jButton);
    g.drawLine(0,xx,GanttHeader.titlePanelWidth,xx);
    }
    }
      

  3.   

    就是我用Button去显示工作进度,所以会有很多的Button放到Panel里,
      

  4.   

    怎么能把组件构造的方法放在paintComponent中,这个方法是一直在调用的。
    你应该把所有Button的创建放在这个面板的构造函数中。让paintComponent只是去填充颜色而已。
      

  5.   


    嗯  有道理·     你就是想產生多個jbutton   這段代碼 完全可以放到外面~   和paint沒啥關系·