为什么java程序打包为jar后窗口的jtree不显示
在myeclipse里面运行的时候是可以的
但是打包成jar后就不行了
设置jtree的
代码是这样的
package service;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import service.Dto.MyType;
public class MyTree{
private JPanel panel;
private Dto dto;
private Play play;
private FileInput fileinput = null;
private JScrollPane jscrollTree;
// 设置树
private JTree jtree;
// 设置节点(此节点为跟节点)
private DefaultMutableTreeNode dmtnRoot = new DefaultMutableTreeNode();
private DefaultMutableTreeNode dmtnLeaf;
private LinkedList<DefaultMutableTreeNode> treeList;
private TreePath path;
private static int num = 0;
JPopupMenu popMenu;
JMenuItem addItem;
JMenuItem delItem;
public MyTree(JPanel panel, Dto dto, Play play) {
this.panel = panel;
this.dto = dto;
this.play = play;
treeList = new LinkedList<DefaultMutableTreeNode>();
treeList.add(new DefaultMutableTreeNode("默认列表"));
treeList.add(new DefaultMutableTreeNode("最近播放"));
initView();
setOnlineList();
popMenu = new JPopupMenu();
addItem = new JMenuItem("添加");
delItem = new JMenuItem("移除");
popMenu.add(addItem);
popMenu.add(delItem);
actionListener();
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (fileinput == null)
fileinput = new FileInput(MusicPlayerFrame.musicplayer);
fileinput.open();
File[] files = fileinput.getFiles();
saveOpenFiles(files);
setOnlineList();
}
});
delItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtree.setSelectionPath(path);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jtree
.getLastSelectedPathComponent();
if (node != null) {
String name = node.toString();
if (name.equals("默认列表")) {
removeAll();
} else {
removeFile(name);
}
}
}
});
}
private void removeAll() {
Vector<MyType> playlist = dto.getPlayList();
playlist.clear();
dto.setCurrentIndex(-1);
dto.setStop();
treeList.getLast().removeAllChildren();
setOnlineList();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jtree.updateUI();
}
});
}
private boolean checkSame(String name, Vector<MyType> list) {
for (Iterator<MyType> iterator = list.iterator(); iterator.hasNext();) {
MyType myType = (MyType) iterator.next();
if (myType.getFilename().equals(name)) {
return true;
}
}
return false;
}
public void removeFile(String name) {
Vector<MyType> list = dto.getPlayList();
int k = findFileLoation(name);
if (k < 0 || k >= list.size()) {
return;
}
int cur = dto.getCurrentIndex();
if (cur > k) {
cur--;
} else if (cur == k) {
play.Next();
dto.setCurrentIndex(cur);
} else {}
list.remove(k);
setOnlineList();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jtree.updateUI();
}
});
}
private void saveOpenFiles(File[] files) {
Vector<MyType> playlist = dto.getPlayList();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
getDirectoryFiles(file.getAbsolutePath(), playlist);
}
else if (!checkSame(file.getName().toLowerCase(), playlist)) {
MyType temp = dto.newMyType(file.getName().toLowerCase(),
file.getAbsolutePath().toLowerCase());
playlist.add(temp);
} else {}
}
}
dto.setPlayList(playlist);
}
private void getDirectoryFiles(String filepath, Vector<MyType> playlist) {
num = 0;
getFiles(filepath, playlist);
System.out.println(num);
}
private void getFiles(String filePath, Vector<MyType> playlist) {
File root = new File(filePath);
File[] files = root.listFiles();
if (files == null) return;
for (File file : files) {
if (file.isDirectory()) {
String spath = file.getAbsolutePath();
int index = spath.indexOf("$RECYCLE.BIN");
if (index >= 0) {
System.out.println("do not find $RECYCLE.BIN");
continue;
}
getFiles(file.getAbsolutePath(), playlist);
}
if (file.isFile()) {
if (file.getName().endsWith(".mp3") || file.getName().endsWith(".wav")) {
if (!checkSame(file.getName().toLowerCase(), playlist)) {
MyType temp = dto.newMyType(file.getName().toLowerCase(),
file.getAbsolutePath().toLowerCase());
playlist.add(temp);
System.out.println("import " + file.getAbsolutePath());
num++;
}
}
}
}
}
// 视图
public void initView() {
new DefaultListModel();
panel.setLayout(null);
jtree = new JTree(dmtnRoot);
// 设置根节点是否显示
jtree.setRootVisible(false);
jtree.putClientProperty("JTree.lineStyle", "None");// 清除线
//jtree.setShowsRootHandles(true);// 设置图标
//DefaultTreeCellRenderer cellRenderer=(DefaultTreeCellRenderer)jtree.getCellRenderer();
////获取该树的Renderer
//cellRenderer.setClosedIcon(Img.CLOSE);
////关闭打开图标
//cellRenderer.setOpenIcon(Img.MIN);
////设置展开图标
//设置单击打开
jtree.setToggleClickCount(1);
for (Iterator<DefaultMutableTreeNode> iterator = treeList.iterator();
iterator.hasNext();) {
dmtnRoot.add(iterator.next());
}
jscrollTree = new JScrollPane(jtree);
jscrollTree.setBounds(0, 110, 250, 320);
panel.add(jscrollTree);
}
public void actionListener() {
jtree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int count = jtree.getRowForLocation(e.getX(), e.getY());
if (e.getButton() != 3 && count != -1) {
String strFriendUsername = jtree
.getLastSelectedPathComponent().toString();
if (e.getClickCount() == 2 && count != 0
&& check(strFriendUsername)) {
if (jtree.isRowSelected(jtree.getRowForLocation(
e.getX(), e.getY()))) {
System.out.println("你双击了:" + strFriendUsername);
int k = findFileLoation(strFriendUsername);
if (k >= 0 && k < dto.getPlayList().size()) {
dto.setCurrentIndex(k);
}
play.playSelect();
}
}
}
if (e.getButton() == 3) {
path = jtree.getPathForLocation(e.getX(), e.getY()); // 关键是这个方法的使用
if (path == null) {
return;
}
jtree.setSelectionPath(path);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jtree
.getLastSelectedPathComponent();
if (node != null) {
if (node.equals(treeList.getLast())) {
return;
} else if (node.getParent().equals(treeList.getLast())) {
return;
}
popMenu.show(jtree, e.getX(), e.getY());
}
}
}
});
}
private int findFileLoation(String name) {
Vector<MyType> playList = dto.getPlayList();
for (int i = 0; i < playList.size(); i++) {
if (playList.elementAt(i).getFilename().equals(name)) {
return i;
}
}
return -1;
}
private boolean check(String name) {
for (Iterator<DefaultMutableTreeNode> iterator = treeList.iterator(); iterator
.hasNext();) {
DefaultMutableTreeNode type = iterator.next();
if (type.toString().equals(name)) {
return false;
}
}
return true;
}
// 设置在线列表
public void setOnlineList() {
// TODO
treeList.get(0).removeAllChildren();
for (int i = 0; i < dto.getPlayList().size(); i++) {
add(dto.getPlayList().elementAt(i), treeList.get(0));
}
//jtree.updateUI();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jtree.updateUI();
}
});
}
public void add(MyType elem, DefaultMutableTreeNode dmtList) {
dmtnLeaf = new DefaultMutableTreeNode(elem.getFilename());
dmtList.add(dmtnLeaf);
}
public void addPlayHistory(String name) {
LinkedList<String> list = dto.getPlayHistory();
while (list.contains(name)) {
list.remove(name);
}
list.add(name);
treeList.get(1).removeAllChildren();
for (int i = list.size() - 1; i >= 0; i--) {
treeList.get(1).add(new DefaultMutableTreeNode(list.get(i)));
}
dto.setPlayHistory(list);
//jtree.updateUI();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jtree.updateUI();
}
});
}
}
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception e)
{
e.printStackTrace();
}