Java swing 访问数据库

Java swing 访问数据库,第1张

打成JAR后,你的驱动jar 用eclipse是打包不进去的你可以把你的驱动包解压缩,然后双击你自己打的jar,点添加,把解压缩出来的一个文件添加到你的jar包中去 另外一个方法,把你的jar包和驱动包放在同一个目录下应该可以跑

程序写好了,而且实现了延时加载。以免启动速度过慢。

import java.io.File

import javax.swing.JFrame

import javax.swing.JScrollPane

import javax.swing.JTree

import javax.swing.event.TreeExpansionEvent

import javax.swing.event.TreeExpansionListener

import javax.swing.tree.DefaultMutableTreeNode

import javax.swing.tree.DefaultTreeModel

import javax.swing.tree.MutableTreeNode

public class TreeFrame extends JFrame {

private JTree tree

public static void main(String[] args) {

new TreeFrame()

}

public TreeFrame() {

this.setSize(800, 600)

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

tree = new JTree()

tree.addTreeExpansionListener(new TreeExpansionAction())

setRootDir(new File("c:/windows"))

this.add(new JScrollPane(tree))

this.setVisible(true)

}

private void setRootDir(File dir) {

tree.setModel(new DefaultTreeModel(createNode(dir)))

MutableTreeNode rootNode = (MutableTreeNode) tree.getModel().getRoot()

rootNode.setUserObject(dir)

updateNode(rootNode)

tree.updateUI()

}

private void updateNode(Object object) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) object

for (int i = 0i <node.getChildCount()i++) {

DefaultMutableTreeNode node2 = (DefaultMutableTreeNode) node.getChildAt(i)

FileObject fileObject = (FileObject) node2.getUserObject()

if (!fileObject.isUpdated()) {

fileObject.setUpdated(true)

File file = fileObject.getFile()

if (file.isDirectory()) {

addSubFile(node2, file)

}

}

}

}

public MutableTreeNode createNode(File dir) {

DefaultMutableTreeNode node = new DefaultMutableTreeNode(new FileObject(dir))

if (dir.isDirectory()) {

addSubFile(node, dir)

}

return node

}

private void addSubFile(DefaultMutableTreeNode node, File dir) {

File[] files = dir.listFiles()

if (files != null) {

for (File file : files) {

node.add(new DefaultMutableTreeNode(new FileObject(file)))

}

}

}

public class TreeExpansionAction implements TreeExpansionListener {

public void treeExpanded(TreeExpansionEvent event) {

updateNode(event.getPath().getLastPathComponent())

}

public void treeCollapsed(TreeExpansionEvent event) {

}

}

public class FileObject {

private File file

private boolean updated

public FileObject(File file) {

this.file = file

}

public File getFile() {

return file

}

public boolean isUpdated() {

return updated

}

public void setUpdated(boolean updated) {

this.updated = updated

}

public String toString() {

return file.getName()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10709090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-10
下一篇 2023-05-10

发表评论

登录后才能评论

评论列表(0条)

保存