我正在为文件浏览器构建GUI,它将显示文件和文件。目录内的文件夹。 File Explorer with TopBar(JPanel) on top, TabPane(JTabbedPane) on lower right, and LeftPanel(JPanel) on lower left
顶部的'+'按钮为jtabbedpane添加了新标签,'<'顶部的按钮返回到父文件夹,leftPanel用于快捷方式。我使用JList来显示文件和文件夹。这个jlist被添加到JScrollPane,然后添加到JTabbedpane。
现在出现问题,当我添加新标签然后切换到其他标签但由'<'所做的更改时button和leftPanel仅在最后一个选项卡上执行。我需要找到一个解决方案,我可以告诉'<'后退按钮仅在选定的选项卡上进行更改。我怎么能实现这个目标? 我正在分享一些课程的代码。 FilesInTheJList.java是用于在jtabbedpane中显示文件的类。
LeftPanel.java
public class LeftPanel extends JPanel
{
public JList<Object> leftList;
public static DefaultListModel<Object> listModel;
private static final long serialVersionUID = 1L;
public LeftPanel(TabPane tabbedPane){
leftList = new JList<Object>();
String leftNamesString[] = {"Downloads","Desktop", "Documents", "Pictures", "Movies", "Users", "Sharing"};
listModel = new DefaultListModel<Object>();
for (String list: leftNamesString) {
listModel.addElement(list);
}
leftList.setModel(listModel);
leftList.setFixedCellHeight(30);
leftList.setFixedCellWidth(150);
leftList.setFont(new Font("SansSerif",Font.PLAIN,16));
leftList.setBorder(BorderFactory.createMatteBorder(10, 5, 5, 0, Color.WHITE));
leftList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
leftList.setCellRenderer(new MyCellRenderer());
addAction(tabbedPane);
listRoots();
}
public void listRoots() {
File[] paths;
paths = File.listRoots();
for(File path:paths)
{
listModel.addElement(path.toString());
}
leftList.invalidate();
leftList.repaint();
}
public void addAction(TabPane tabbedPane){
leftList.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
String home = System.getProperty("user.home");
switch(leftList.getSelectedValue().toString()){
case "Downloads":
tabbedPane.showFilesInTheJList.repaintList(home+"/Downloads/");
break;
case "Desktop":
tabbedPane.showFilesInTheJList.repaintList(home+"/Desktop/");
break;
case "Documents":
tabbedPane.showFilesInTheJList.repaintList(home+"/Documents/");
break;
case "Pictures":
tabbedPane.showFilesInTheJList.repaintList(home+"/Pictures/");
break;
case "Movies":
tabbedPane.showFilesInTheJList.repaintList(home+"/Movies/");
break;
case "Users":
tabbedPane.showFilesInTheJList.repaintList("/Users/");
break;
case "Sharing":
@SuppressWarnings("unused")
SharingGui openSharingGui = new SharingGui();
break;
default:
tabbedPane.showFilesInTheJList.repaintList(leftList.getSelectedValue().toString());
break;
}
}
//other empty mouse pressed/released methods
});
}
private static class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
//not so important stuff
}
}
tabpane.java
public class TabPane extends JTabbedPane{
private static final long serialVersionUID = 1L;
private String pathString;
public FilesInTheJList showFilesInTheJList;
TabPane()
{
setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
pathString = "/Users/jaspreet/Downloads/";
}
public void addTabs()
{
if (getTabCount() == 0) {
showFilesInTheJList = new FilesInTheJList();
showFilesInTheJList.paintList(pathString);
addTab(showFilesInTheJList.parentDirName, new JScrollPane(showFilesInTheJList.list));
}
else {
showFilesInTheJList = new FilesInTheJList();
showFilesInTheJList.paintList(pathString);
insertTab(showFilesInTheJList.parentDirName, null,new JScrollPane(showFilesInTheJList.list), showFilesInTheJList.parentDirName, getTabCount());
initTabComponent(getTabCount()-1);
}
}
private void initTabComponent(int i) {
this.setTabComponentAt(i, new ButtonTabComponent(this));
}
}
从splitpane.java
调用它们splitpane.java
public class SplitPane extends JPanel
{
private static final long serialVersionUID = 1L;
public JSplitPane splitPane;
public TabPane tabbedPane;
SplitPane(){
tabbedPane = new TabPane();
tabbedPane.addTabs();
LeftPanel leftSplit = new LeftPanel(tabbedPane);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(leftSplit.leftList), tabbedPane);
splitPane.setOneTouchExpandable(true);
splitPane.setContinuousLayout(true);
}
}