你怎么还在问这个问题啊,我在你已经结贴的那个贴子上贴了一段代码,是我昨晚调试通过,编绎后直接可以运行的,注意要把包名改掉。
程序已解决了你的所有问题,你认真看一下。功能是点击一个按钮后,画一个JLabel和一个色块,直接运行就有滚动条了,当然,这个滚动条也可以通过动态地做setPreferredSize使panel中的控件超出范围后再显示出来。

解决方案 »

  1.   

    我再贴一次吧,我用的是JDK1.4.2,eclipse 3.0 ,注意把包名改掉:
    /*
     * Created on 2005-4-13
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package nt2000.DrvManage;import java.awt.Graphics;
    import java.awt.Graphics2D;import javax.swing.JPanel;
    import java.awt.geom.Rectangle2D;
    import java.awt.Color;
    /**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class TestDrawPanel extends JPanel {
    private Rectangle2D rect = null;
    /**
     * This is the default constructor
     */
    public TestDrawPanel() {
    super();
    initialize();
    }
    /**
     * This method initializes this
     * 
     * @return void
     */
    private  void initialize() {
    this.setLayout(null);
    this.setSize(300,200);
    }
    public void setRect(Rectangle2D rect){
    this.rect = rect;
    }
    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2D = (Graphics2D)g;
    g2D.setPaint(Color.RED);
    if (rect != null)
    g2D.fill(rect); }
    }
    /*
     * Created on 2005-4-13
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package nt2000.DrvManage;import javax.swing.JFrame;import javax.swing.JScrollPane;
    import javax.swing.JLabel;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JToolBar;
    import javax.swing.JButton;
    import java.awt.Dimension;
    /**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class TestFrame extends JFrame { private javax.swing.JPanel jContentPane = null; private JScrollPane jScrollPane = null;
    private TestDrawPanel jPanel = null;
    private JToolBar jToolBar = null;
    private JButton jButton = null;
    /**
     * This is the default constructor
     */
    public TestFrame() {
    super();
    initialize();
    }
    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
    this.setSize(300,200);
    this.setContentPane(getJContentPane());
    }
    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private javax.swing.JPanel getJContentPane() {
    if(jContentPane == null) {
    jContentPane = new javax.swing.JPanel();
    jContentPane.setLayout(new java.awt.BorderLayout());
    jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
    jContentPane.add(getJToolBar(), java.awt.BorderLayout.NORTH);
    }
    return jContentPane;
    }
    /**
     * This method initializes jScrollPane
     * 
     * @return javax.swing.JScrollPane
     */    
    private JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
    jScrollPane = new JScrollPane();
    jScrollPane.setViewportView(getJPanel());
    }
    return jScrollPane;
    }
    /**
     * This method initializes jPanel
     * 
     * @return javax.swing.JPanel
     */    
    private TestDrawPanel getJPanel() {
    if (jPanel == null) {
    jPanel = new TestDrawPanel();
    jPanel.setPreferredSize(new Dimension(300,300));
    }
    return jPanel;
    }
    /**
     * This method initializes jToolBar
     * 
     * @return javax.swing.JToolBar
     */    
    private JToolBar getJToolBar() {
    if (jToolBar == null) {
    jToolBar = new JToolBar();
    jToolBar.add(getJButton());
    }
    return jToolBar;
    }
    /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */    
    private JButton getJButton() {
    if (jButton == null) {
    jButton = new JButton();
    jButton.setText("draw");
    jButton.addActionListener(new java.awt.event.ActionListener() { 
    public void actionPerformed(java.awt.event.ActionEvent e) {    
    JLabel label = new JLabel("test");
    label.setBounds(20,20,30,40);
    getJPanel().add(label);
    getJPanel().setRect(new Rectangle2D.Double(60,20,30,30));
    getJPanel().repaint();
    }
    });
    }
    return jButton;
    }
        }
      

  2.   

    要在paint()中画图啊,难道是在1.4.2中才有吗?我在这个版块看到所有人都是在paintComponent中画。
    而且paint也不能显示地调用,而是要通过repaint()间接调用。
      

  3.   

    前面我错了,paintComponent中画也行,因为已回复了三次,无法再回所以现在予以更正。但paintComponent不能主动调用,也要通过repaint调用。