1、色块可以用背景色,不要自己用Graphics画,容易出毛病
2、JScrollPane关心的是内部容器的PreferredSize,跟你的size没关系,所以要出现滚动条,你的PreferredSize要大于JScrollPane本身的size
3、这个跟FileFilter相关吧
4、这个谁知道你要的是什么啊?

解决方案 »

  1.   

    第三个问题:
    chooser = new JFileChooser( "." );
    FileFilter type = new ShareFileFilter( "html files",new String[]{ ".htm",".html","xhtml" } );
    chooser.addChoosableFileFilter( type );
    chooser.setFileFilter( type );
    FileView view = new ShareFileView();
    chooser.setFileView( view );
    public class ShareFileFilter extends FileFilter {
    private String extensions[];
    private String description; public ShareFileFilter( String description,String extension ) {
    this( description, new String[] { extension } );
    } public ShareFileFilter( String description,String extensions[] ) {
    this.description = description;
    this.extensions = (String[])extensions.clone();
    } public boolean accept( File file ) {
    if( file.isDirectory() ){
    return true;
    }
    int count = extensions.length;
    String path = file.getAbsolutePath();
    for( int i = 0; i < count ; i++ ) {
    String ext = extensions[i];
    if( path.endsWith( ext ) && 
    ( path.charAt( path.length()-ext.length() ) == '.' ) ) {
    return true;
    }
    }
    return false;
    } public String getDescription() {
    return ( description == null? extensions[0]:description );
    }
    }
    /*
     * Created on 2004-12-21
     */
    package com.yuch.ui;import java.awt.Color;
    import java.io.File;
    import java.util.HashMap;import javax.swing.Icon;
    import javax.swing.filechooser.FileView;
    /**
     * @author yuch
     */
    /**
     * Customize the FileView which can link specifid files to icons accordingly
     *
     */
    public class ShareFileView extends FileView {
    private HashMap hash = new HashMap();

    public ShareFileView() {
    hash.put( "html",new AnOvalIcon( Color.red ) );
    hash.put( "htm",new AnOvalIcon( Color.red ) );
    hash.put( "xhtml",new AnOvalIcon( Color.red ) );
    }

    public String getName( File f ) {
    String s = f.getName();
    if( s.length() == 0 ) {
    s = f.getAbsolutePath();
    }
    return s;
    }

    public String getDescription( File f ) {
    return f.getName();
    }

    public String getTypeDescription( File f ) {
    return f.getAbsolutePath();
    }

    public Icon getIcon( File f ) {
    String path = f.getAbsolutePath();
    int pos = path.lastIndexOf('.');
    if( ( pos >= 0 ) && ( pos < ( path.length()-1 ) ) ) {
    String ext = path.substring( pos+1 ).toLowerCase();
    return (Icon)hash.get( ext );
    }
    return null;
    }

    public Boolean isTraversable( File file ) {
    return ( new Boolean( file.isDirectory() ) );
    }
    }
      

  2.   

    to:relive
    第一个问题能不能就我的程序给出具体的解决办法,最好是代码
      

  3.   

    this.getContentPane().add(scrollpane, BorderLayout.CENTER);
    scrollpane.getViewport().add(panel, null);
    问题在于:你认为当你用Graphics在边界外面画图的时候,scrollpane应该自动滚动到你画笔所在位置。你是不是这么想的?
    这个想法是错误的,正如同你在一张白纸上画画,你永远无法把画画在纸外面一样,ShowColor就是你的白纸,Graphics是你的笔,ShowColor的大小限定了你所能使用的空间,所以,你要在边界外画的话,你首先要改变纸的大小,ShowColor.setPreferredSize(),如果你的纸有画框的话,那么你要同时改变画框的大小panel.setPreferredSize(),不然你画是画上了,不过你只能看见画框里的东西,跟没画一个样。所以
    public class ShowColor extends JPanel {
        public void paintComponent(Graphics g, int i, int j, int k, int leftX,
                                   int topY, int width, int height) {
          //这里要加判断,当leftX+width>this.getSize().getWidth()或topY+height>this.getSize().getHeight()的时候,你要扩大你的画板和边框的大小
          //ShowColor.setPreferredSize();panel.setPreferredSize();
          super.paintComponents(g);
          Graphics2D g2 = (Graphics2D) g;
          Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
          g2.setPaint(new Color(i, j, k));
          g2.fill(rect);    }
    }
      

  4.   

    showcolor只是我的一个类,没有大小可言啊,我现在把显示色块的函数照你的意思换成了:
     
      public class ShowColor
          extends JPanel {    public void paintComponent(Graphics g, int i, int j, int k, int leftX,
                                   int topY, int width, int height) {      super.paintComponents(g);
          Graphics2D g2 = (Graphics2D) g;
          Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
          if ( (leftX + width) > this.getSize().getWidth()) {
            panel.setPreferredSize(new Dimension( (panel.WIDTH) + leftX + width,
                                                 panel.HEIGHT));      }
          g2.setPaint(new Color(i, j, k));
          g2.fill(rect);    }
      }
    同时我在程序的最后一部分添加了:
     show1.paintComponent(this.getGraphics(), (int) Red[i] * 17,
                                 (int) Green[i] * 17, (int) Blue[i] * 17,
                                 100 * (j + 1),
                                 100 * (i + 1), 50, 50);
    //新添加的
    panel.add(show1);
    panel.repaint();
    .......现在的情况是不要panel.repaint()时,出来的色块只要改变一下JFrame的大小就消失,然后JLabel才出来。
    而两个都要时,色块就直接一闪而过,JLabel出现。