gpt4 book ai didi

Java GUI 搜索目录文件

转载 作者:行者123 更新时间:2023-12-02 03:33:34 25 4
gpt4 key购买 nike

这三个类是我到目前为止所得到的。

package ex;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.event.*;

import java.util.*;

import java.text.*;

class ATable extends AbstractTableModel {
private String title[] = { "Name", "Size", "Type", "Modified Date" };
private String val[][] = new String[1][4];

public void setValueArr(int i) {
val = new String[i][4];
}

public int getRowCount() {
return val.length;
}

public int getColumnCount() {
return val[0].length;
}

public String getColumnName(int column) {
return title[column];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0)
return true;
else
return false;
}

public Object getValueAt(int row, int column) {
return val[row][column];
}

public void setValueAt(String aValue, int rowIndex, int columnIndex) {
val[rowIndex][columnIndex] = aValue;
}
}
<小时/>
            package ex;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.event.*;

import java.util.*;

import java.text.*;

class FileViewer implements TreeWillExpandListener, TreeSelectionListener {
private JFrame frame = new JFrame("Explorer");
private Container con = null;

private JSplitPane pMain = new JSplitPane();
private JScrollPane pLeft = null;
private JPanel pRight = new JPanel(new BorderLayout());

private DefaultMutableTreeNode root = new DefaultMutableTreeNode("My Computer");
private JTree tree;

private JPanel pNorth = new JPanel();
private JPanel northText = new JPanel();
private JLabel northLabel0 = new JLabel("Path");
private JTextField pathText = new JTextField();
private JLabel northLabel1 = new JLabel("Search");
private JTextField searchText = new JTextField();


private Dimension dim, dim1;
private int xpos, ypos;

FileViewer() {
init();
start();
frame.setSize(800, 600);
dim = Toolkit.getDefaultToolkit().getScreenSize();
dim1 = frame.getSize();
xpos = (int) (dim.getWidth() / 2 - dim1.getWidth() / 2);
ypos = (int) (dim.getHeight() / 2 - dim1.getHeight() / 2);
frame.setLocation(xpos, ypos);
frame.setVisible(true);
}


void init() {
pMain.setResizeWeight(1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();
con.setLayout(new BorderLayout());

pathText.setPreferredSize(new Dimension(300, 20));
searchText.setPreferredSize(new Dimension(300, 20));
northText.add(northLabel0);
northText.add(pathText);
northText.add(northLabel1);
northText.add(searchText);
pNorth.add(northText);
con.add(pNorth, "North");
File file = new File("C:/");
File list[] = file.listFiles();
DefaultMutableTreeNode temp;

for (int i = 0; i < list.length; ++i) {
temp = new DefaultMutableTreeNode(list[i].getPath());
temp.add(new DefaultMutableTreeNode("None"));
root.add(temp);
}
tree = new JTree(root);
pLeft = new JScrollPane(tree);

pMain.setDividerLocation(150);
pMain.setLeftComponent(pLeft);
pMain.setRightComponent(pRight);
con.add(pMain);
}

void start() {
tree.addTreeWillExpandListener(this);
tree.addTreeSelectionListener(this);
}

public static void main(String args[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
new FileViewer();
}

String getPath(TreeExpansionEvent e) {
StringBuffer path = new StringBuffer();
TreePath temp = e.getPath();
Object list[] = temp.getPath();
for (int i = 0; i < list.length; ++i) {
if (i > 0) {
path.append(((DefaultMutableTreeNode) list[i]).getUserObject() + "\\");
}
}
return path.toString();
}

String getPath(TreeSelectionEvent e) {
StringBuffer path = new StringBuffer();
TreePath temp = e.getPath();
Object list[] = temp.getPath();
for (int i = 0; i < list.length; ++i) {
if (i > 0) {
path.append(((DefaultMutableTreeNode) list[i]).getUserObject() + "\\");
}
}
return path.toString();
}

public void getSearch(){

}





public void treeWillCollapse(TreeExpansionEvent event) {
}

public void treeWillExpand(TreeExpansionEvent e) {
if (((String) ((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).getUserObject()).equals("My Computer")) {
} else {
try {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
File tempFile = new File(getPath(e));
File list[] = tempFile.listFiles();
DefaultMutableTreeNode tempChild;
for (File temp : list) {
if (temp.isDirectory() && !temp.isHidden()) {
tempChild = new DefaultMutableTreeNode(temp.getName());
if (true) {
File inFile = new File(getPath(e) + temp.getName() + "\\");
File inFileList[] = inFile.listFiles();
for (File inTemp : inFileList) {
if (inTemp.isDirectory() && !inTemp.isHidden()) {
tempChild.add(new DefaultMutableTreeNode("None"));
break;
}
}
}
parent.add(tempChild);
}
}
parent.remove(0);
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Can't Find a File");
}
}
}

public void valueChanged(TreeSelectionEvent e) {
if (((String) ((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).getUserObject()).equals("My Computer")) {
pathText.setText("My Computer");
} else {
try {
pathText.setText(getPath(e));
pRight = new FView(getPath(e), null).getTablePanel();
pMain.setRightComponent(pRight);
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Can't Find a File");
}
}
}

}

            package ex;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.event.*;

import java.util.*;

import java.text.*;

class FView {
private ATable at = new ATable();
private JTable jt = new JTable(at);

private JPanel pMain = new JPanel(new BorderLayout());
private JScrollPane pCenter = new JScrollPane(jt);

private File file;
private File list[];
private long size = 0, time = 0;

FView(String str, String searchKey) {
init();
if (searchKey!=null){
search(str, searchKey);
}
start(str);

}

void init() {
pMain.add(pCenter, "Center");
}

void start(String strPath) {
file = new File(strPath);
list = file.listFiles();
at.setValueArr(list.length);
for (int i = 0; i < list.length; ++i) {
size = list[i].length();
time = list[i].lastModified();
for (int j = 0; j < 4; ++j) {
switch (j) {
case 0:
at.setValueAt(list[i].getName(), i, j);
break;
case 1:
if (list[i].isFile())
at.setValueAt(Long.toString((long) Math.round(size / 1024.0)) + "Kb", i, j);
break;
case 2:
if (list[i].isFile()) {
at.setValueAt(getLastName(list[i].getName()), i, j);
} else
at.setValueAt("파일폴더", i, j);
break;
case 3:
at.setValueAt(getFormatString(time), i, j);
break;
}
}
}
jt.repaint();
pCenter.setVisible(false);
pCenter.setVisible(true);
}

void search(String strPath, String searchKey){
file = new File(strPath);
list = file.listFiles();
at.setValueArr(list.length);
for (int i = 0; i < list.length; ++i) {
size = list[i].length();
time = list[i].lastModified();
for (int j = 0; j < 4; ++j) {
switch (j) {
case 0:
at.setValueAt("zzzzzzzzz", i, j);
break;
case 1:
if (list[i].isFile())
at.setValueAt(Long.toString((long) Math.round(size / 1024.0)) + "Kb", i, j);
break;
case 2:
if (list[i].isFile()) {
at.setValueAt(getLastName(list[i].getName()), i, j);
} else
at.setValueAt("File Folder", i, j);
break;
case 3:
at.setValueAt(getFormatString(time), i, j);
break;
}
}
}
jt.repaint();
pCenter.setVisible(false);
pCenter.setVisible(true);
}

String getLastName(String name) {
int pos = name.lastIndexOf(".");
String result = name.substring(pos + 1, name.length());
return result;
}

String getFormatString(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date d = new Date(time);
String temp = sdf.format(d);
return temp;
}

JPanel getTablePanel() {
return pMain;
}
}

我想要的是路径文本字段右侧的“搜索文件”功能。

搜索文本字段的位置现在很好,但我不知道如何在其上添加功能。

它应该显示一个文件,其中包含用户在搜索文本字段中输入的单词。 (仅当前目录中的文件。不需要显示子目录中的文件)

当搜索文本字段为空时,它应该照常显示当前目录中的所有文件。

请帮我解决这个问题。

最佳答案

con.add(pNorth, "North");

不要使用魔法字符串。该 API 将包含您可以使用的变量:

con.add(pNorth, BorderLayout.NORTH);

It should show a file that contains a word which the user type on search text field

您需要将 DocumentListener 添加到 JTextFieldDocument 中。每当添加或删除文本时都会触发一个事件,然后您就可以进行搜索。

阅读 Swing 教程中关于 How to Write a DocumentListener 的部分了解更多信息和示例。

关于Java GUI 搜索目录文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731529/

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