代码是这样的:
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子句也存在一个不相关的问题,原本应该与当前的相反))。
答案 0 :(得分:1)
mainpanel.add(jaypanel);
mainpanel.setSize(400,400);
mainpanel.setLayout(new ScrollPaneLayout());
无论何时使用布局管理器,都应在将组件添加到面板之前设置布局。
但是,无需设置滚动窗格的布局管理器。滚动窗格将自动设置布局。
不要使用setSize(..)
。布局管理器将确定每个组件的大小和位置。获取所有setSize(...)语句。
阅读Layout Managers上的Swing教程中有关工作示例的部分。