java - JScrollPane 的下半部分不出现

标签 java swing jscrollpane

代码如下:

public static class Fileslist extends JFrame
    {
        List<Container> folderbuttons= new ArrayList<Container>(); 
        List<JButton> folderbuttons2= new ArrayList<JButton>(); 
        List<DbxEntry> filesandfolders=new ArrayList<DbxEntry>();
        ArrayList<Item> kal = new ArrayList<Item>();
        public Fileslist(String path) throws NetworkIOException, DbxException, IOException
        {
            addFolders(path);
            int count=0;
            JScrollPane panel= new JScrollPane();
            JScrollPane mainpanel= new JScrollPane();
            DefaultListModel<String> model= new DefaultListModel<String>();
            JPanel jaypanel = new JPanel();
            for(Container c : folderbuttons)
            {
                jaypanel.add(c);
            }
            for(DbxEntry c: filesandfolders)
            {
                if(c.isFile())
                {
                    if(model.contains(c.name+" "+" File"+" "+c.asFile().lastModified))
                    {
                        model.addElement(c.name+" "+" File"+" "+c.asFile().lastModified);
                    }
                }
            }
            JList<String> filesfolders= new JList<String>(model);
            filesfolders.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
            Container c = new Container();
            c.add(filesfolders);
            panel.add(c);
            panel.setSize(200,200);
            JTextField text = new JTextField("",2);
            text.addKeyListener(new KeyAdapter() 
            {
                public void keyTyped(KeyEvent e) 
                {
                    if (e.getKeyChar() == KeyEvent.VK_ENTER) 
                    {
                        try {
                            ArrayList<Item> black=getFilesStartingWith(path, text.getText());
                            DbxClientV1 client = new DbxClientV1(config, ACCESS_TOKEN);
                            client.createFolder(path+"/"+text.getText());
                             System.out.println("Entered:  "+ text.getText());
                            for(int i=0; i<black.size();i++)
                            {
                                client.move("/"+black.get(i).mainpath, "/"+black.get(i).mainpath+"/"+text.getText()+"/"+black.get(i).name);
                            }
                            System.out.println("Entered: "+ text.getText());
                        } 
                        catch (DbxException e1) 
                        {
                            e1.printStackTrace();
                        }
                        System.out.println("Entered:"+ text.getText());
                    }
                }
            });
            jaypanel.setSize(400, 400);
            mainpanel.add(jaypanel);
            mainpanel.setSize(400,400);
            mainpanel.setLayout(new ScrollPaneLayout());
            getContentPane().add(mainpanel);
            getContentPane().add(panel);
            getContentPane().add(text);
            if(filesandfolders.size()==0)
            {
                System.out.println("No files");
            }
            setSize(600, 600);
            repaint();
            setLayout(new GridLayout(3,3));
            setVisible(true);
        }
        public void addFolders(String path) throws NetworkIOException, DbxException, IOException
        {
            ArrayList<String> folders= new ArrayList<String>();
            folders.add("/");
            int count=0;
            DbxClientV1 client = new DbxClientV1(config, ACCESS_TOKEN);
            List<Metadata> newmeta= new ArrayList<Metadata>(2);
            folders=getAllFolders2(newmeta, client);
            WithChildren result = client.getMetadataWithChildren(path, true);
            filesandfolders = result.children;
            for(String kal:folders)
            {
                JButton addtoMain= new JButton(kal);
                addtoMain.setSize(150,150);
                addtoMain.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    try {
                        Fileslist filesframe=new Fileslist(kal);
                    } catch (NetworkIOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (DbxException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            });
            Container contain= new Container();
            contain.setSize(150, 150);
            contain.setLayout(new FlowLayout());
            contain.add(addtoMain);
            folderbuttons.add(contain);
            folderbuttons2.add(addtoMain);
        }

    }
    public ArrayList<Item> getFilesStartingWith(String path, String key) throws DbxException
    {
        ArrayList<Item> files = new ArrayList();
        DbxClientV1 client = new DbxClientV1(config, ACCESS_TOKEN);
        WithChildren result = client.getMetadataWithChildren(path, true);
        List<DbxEntry> cursor3 = result.children;
        for (DbxEntry entry : cursor3) 
        {
            if(entry.isFile())
            {
                Item newItem = new  Item(entry.path, path, entry.name);
                kal.add(newItem);
            }
        }
        for(int i=0; i<kal.size();i++)
        {
                if(kal.get(i).name.startsWith(key))
                {
                    System.out.println(path+" has under:*** "+kal.get(i).path+" also called "+kal.get(i).name);
                    files.add(kal.get(i));
                }
        }
        return files;
    }
}

这给出了以下结果: http://i558.photobucket.com/albums/ss30/magpiejay/Balksz_zpsuxdv6fvl.jpg~original

滚动按钮的末尾丢失,并且 TextArea 过大。 那么,这是怎么回事?

编辑:好吧,我似乎解决了这个问题。这是因为我直接向 JScrollPane 添加元素,而不是使用 .setViewportView()。

((还有一个与 if 子句无关的问题,它最初应该与当前的相反)。

最佳答案

    mainpanel.add(jaypanel);
    mainpanel.setSize(400,400);
    mainpanel.setLayout(new ScrollPaneLayout());
  1. 每当您使用布局管理器时,都应该在将组件添加到面板之前设置布局。

  2. 但是,不需要设置滚动 Pane 的布局管理器。滚动 Pane 会自动设置布局。

  3. 不要使用setSize(..)。布局管理器将确定每个组件的大小和位置。摆脱所有 setSize(...) 语句。

阅读 Swing 教程中关于 Layout Managers 的部分用于工作示例。

关于java - JScrollPane 的下半部分不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37515330/

相关文章:

java - Hibernate SQLGrammarException : for a table having 3 different association mapping

java - 可靠地区分有状态 EJB 2.0 bean 和无状态 EJB 2.0 bean

java - 前 1000 个素数之和不正确?怎么了?

java - JTextArea 滚动功能

java - VLCJ/libvlc 跨平台库路径

java - 如果用户忘记在我的代码中选择 jRadioButton 如何停止执行?

java - 建议 Java Swing 组件库

java - 将 JPanel 替换为具有更新内容的相同内容

java - JScrollPane 无法在 GUI 中工作

java - 如何检测 JScrollPane 滚动条是否改变可见性?