gpt4 book ai didi

java - 根据数组创建对象

转载 作者:搜寻专家 更新时间:2023-11-01 03:19:43 25 4
gpt4 key购买 nike

我有一个数组,其中包含位于另一个目录中的目录数量。但是我的问题是我必须为每个文件夹创建一个 JPanel

例如:

File folder = new File("...");
File[] listOfFiles = folder.listFiles(); <- this contains the folders ({"folder1","folder2","folder3",...}).

现在我不知道如何解决这个问题。

我写的一些代码:

SystemAPI sapi = new SystemAPI(); <- custom written API  
File folder = new File(sapi.getHomeDir+"\\Documents\\*programname*\\default");
File[] listOfFiles = folder.listFiles();

try{
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println(listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println(listOfFiles[i].getName());
}
}
}catch(Exception a){
JOptionPane.showMessageDialog(null, "Error Occurred", "Error Dialog", JOptionPane.ERROR_MESSAGE);
}

最佳答案

你可以做这样的事情(我使用 MigLayout 提供的布局制作它,但可以更改)

import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
import java.io.*;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JTabbedPane;

public class SimpleGUI
{
public JFrame myMainWindow = new JFrame("Multtiple directories");

JPanel[] guiPanelS = new JPanel[1000];
JScrollPane[] guiJSP = new JScrollPane[1000];
public JTabbedPane tabbedPane = new JTabbedPane();
int pI = 0;
String mainDirectory = "C:/Users/Daniel/Dropbox/Programming/Code/Notepad++/Java/Stack Overflow";
String mainDirectoryName = new File(mainDirectory).getName();
int indentForMainPanel = 30;
String parseIndent = Integer.toString(indentForMainPanel);
String lastDirOpen = mainDirectory;
String lastDirOpenBut = mainDirectory;
ArrayList<File> directoriesList = new ArrayList<File>();

public void runGUI()
{
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myMainWindow.setLayout(new GridLayout(1,1));

listFilesHD(new File(mainDirectory),mainDirectoryName);

final boolean showTabsHeader = false;
tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI()
{
@Override
protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight)
{
if(showTabsHeader)
{
return super.calculateTabAreaHeight(tabPlacement, horizRunCount, maxTabHeight);
}
else
{
return 0;
}
}
protected void paintTabArea(Graphics g,int tabPlacement,int selectedIndex){}
});

myMainWindow.getContentPane().add(tabbedPane);
tabbedPane.addChangeListener(new CustomChangeListener());

myMainWindow.setVisible(true);
myMainWindow.setBounds(10,10,500,500);
}

public void listFilesHD(File f,String dName)
{
MigLayout layout = new MigLayout("wrap", "[grow]");
guiPanelS[pI] = new JPanel(layout);
guiJSP[pI] = new JScrollPane(guiPanelS[pI]);
JLabel currentDir = new JLabel("Current Directory: "+dName);
guiPanelS[pI].add(currentDir,"align center");
JLabel DirectoryName = new JLabel(dName);
DirectoryName.setForeground(Color.BLUE);
guiPanelS[pI].add(DirectoryName);
File[] listOfFiles = f.listFiles();

JLabel[] listsValues = new JLabel[listOfFiles.length];
JButton[] tabSwitch = new JButton[listOfFiles.length];

int i=0;
int i2=0;
for(File fileTemp:listOfFiles)
{
listsValues[i] = new JLabel(listOfFiles[i].getName());
if(listOfFiles[i].isDirectory())
{
directoriesList.add(listOfFiles[i]);
tabSwitch[i2] = new JButton(listOfFiles[i].getName());
tabSwitch[i2].setOpaque(false); //These remove the button filling and border
tabSwitch[i2].setContentAreaFilled(false);
tabSwitch[i2].setBorder(null);
tabSwitch[i2].setFocusable(false);
tabSwitch[i2].setForeground(Color.RED);
tabSwitch[i2].addActionListener(new openNewPaneActionListener());
guiPanelS[pI].add(tabSwitch[i2],"gapLeft 30");
i2++;
}
else if(listOfFiles[i].isFile())
{
guiPanelS[pI].add(listsValues[i],"gapLeft 30");
}

i++;
}

tabbedPane.addTab(f.getAbsolutePath(),guiJSP[pI]);
guiJSP[pI].getVerticalScrollBar().setUnitIncrement(16);
pI++;
}

public void listFilesSubD(File f,String dName,String dPath, String homeDirectory)
{
MigLayout layout = new MigLayout("wrap", "[grow]");
guiPanelS[pI] = new JPanel(layout);
guiJSP[pI] = new JScrollPane(guiPanelS[pI]);
JLabel currentDir = new JLabel("Current Directory: "+dName);
guiPanelS[pI].add(currentDir,"align center");
JButton homeDirectoryName = new JButton(homeDirectory);
homeDirectoryName.setForeground(Color.BLUE);
homeDirectoryName.addActionListener(new backListener());
homeDirectoryName.setOpaque(false); //These remove the button filling and border
homeDirectoryName.setContentAreaFilled(false);
homeDirectoryName.setBorder(null);
homeDirectoryName.setFocusable(false);
guiPanelS[pI].add(homeDirectoryName);

if(!new File(lastDirOpenBut).getName().equals(mainDirectoryName))
{
JButton lastDirectoryName = new JButton(new File(lastDirOpenBut).getName());
lastDirectoryName.setForeground(Color.GREEN);
lastDirectoryName.addActionListener(new upOneListener());
lastDirectoryName.setOpaque(false);
lastDirectoryName.setContentAreaFilled(false);
lastDirectoryName.setBorder(null);
lastDirectoryName.setFocusable(false);
guiPanelS[pI].add(lastDirectoryName,"gapLeft 30");

File[] listOfFiles = f.listFiles();

JLabel[] listsValues = new JLabel[listOfFiles.length];
JButton[] tabSwitch = new JButton[listOfFiles.length];

int i=0;
int i2=0;
for(File fileTemp:listOfFiles)
{
listsValues[i] = new JLabel(listOfFiles[i].getName());
if(listOfFiles[i].isDirectory())
{
directoriesList.add(listOfFiles[i]);
tabSwitch[i2] = new JButton(listOfFiles[i].getName());
tabSwitch[i2].setOpaque(false);
tabSwitch[i2].setContentAreaFilled(false);
tabSwitch[i2].setBorder(null);
tabSwitch[i2].setFocusable(false);
tabSwitch[i2].setForeground(Color.RED);
tabSwitch[i2].addActionListener(new openNewPaneActionListener());
guiPanelS[pI].add(tabSwitch[i2],"gapLeft 60");
i2++;
}
else if(listOfFiles[i].isFile())
{
guiPanelS[pI].add(listsValues[i],"gapLeft 60");
}
i++;
}
}
else
{
File[] listOfFiles = f.listFiles();

JLabel[] listsValues = new JLabel[listOfFiles.length];
JButton[] tabSwitch = new JButton[listOfFiles.length];

int i=0;
int i2=0;
for(File fileTemp:listOfFiles)
{
listsValues[i] = new JLabel(listOfFiles[i].getName());
if(listOfFiles[i].isDirectory())
{
directoriesList.add(listOfFiles[i]);
tabSwitch[i2] = new JButton(listOfFiles[i].getName());
tabSwitch[i2].setOpaque(false); //These remove the button filling and border
tabSwitch[i2].setContentAreaFilled(false);
tabSwitch[i2].setBorder(null);
tabSwitch[i2].setFocusable(false);
tabSwitch[i2].setForeground(Color.RED);
tabSwitch[i2].addActionListener(new openNewPaneActionListener());
guiPanelS[pI].add(tabSwitch[i2],"gapLeft 30");
i2++;
}
else if(listOfFiles[i].isFile())
{
guiPanelS[pI].add(listsValues[i],"gapLeft 30");
}
i++;
}
}

if(tabbedPane.indexOfTab(dPath)==-1)
{
tabbedPane.addTab(dPath,guiJSP[pI]);
}

guiJSP[pI].getVerticalScrollBar().setUnitIncrement(16);
pI++;
}

public static void main(String[] args)
{
SimpleGUI sG = new SimpleGUI();
sG.runGUI();
}

class openNewPaneActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
lastDirOpenBut = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
String butSrcTxt = "";
Object source = e.getSource();
if (source instanceof JButton)
{
JButton btn = (JButton)source;
butSrcTxt = btn.getText();
}

boolean tF = false;
File fMain = new File("");
for(File f:directoriesList)
{
if(f.getName().equals(butSrcTxt))
{
fMain = f;
tF = true;
}
}

if(tF)
{
if(tabbedPane.indexOfTab(fMain.getAbsolutePath())==-1)
{
listFilesSubD(fMain, butSrcTxt, fMain.getAbsolutePath(),mainDirectoryName);
}

tabbedPane.setSelectedIndex(tabbedPane.indexOfTab(fMain.getAbsolutePath()));
}

}
}

class upOneListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
tabbedPane.setSelectedIndex(tabbedPane.indexOfTab(lastDirOpen));
}
}

class CustomChangeListener implements ChangeListener
{
@Override
public void stateChanged(ChangeEvent e)
{
lastDirOpen = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
int endIndex = lastDirOpen.lastIndexOf(Character.toString((char) 92));
if (endIndex != -1)
{
lastDirOpen = lastDirOpen.substring(0, endIndex);
}
}
}

class backListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
tabbedPane.setSelectedIndex(0);
}
}
}

代码生成一个简单的 GUI(如下图所示)。图为当前打开的目录。蓝色按钮是单击时打开的主目录。打开目录中的文件是黑色的。子目录是红色按钮,单击它会打开一个包含该目录内容的新面板,最后绿色按钮是当前打开目录上方的目录。例如,如果您在 C:\\D\B 中,当单击绿色按钮时,您将输入 C:\\D

所有面板都在 JScrollPane 上,因此您无需担心目录中的文件数量。您现在可以根据自己的情况调整此代码。如果您还没有 MigLayout,我还建议您获取它,这样您就可以了解这个小程序是如何工作的。

Pic of compiled code

关于java - 根据数组创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com