-2
如何解決此代碼的運行時異常?
當點擊按鈕,文件選擇,並從中添加文件的面板顏色消失(錯誤的事情在面板發生)如何解決這個運行時異常?
這是我的代碼:
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.border.BevelBorder;
import javax.swing.JFileChooser;
import javax.swing.plaf.FileChooserUI;
public class pan extends JPanel implements DropTargetListener {
private DefaultListModel listModel = new DefaultListModel();
private DropTarget dropTarget;
private JScrollPane droparea;
private JList list;
private JButton addbutton;
// Create the panel.
//and add to it component
public pan() {
setLayout(null);
addbutton = new JButton("New button");
addbutton.setBounds(10, 10, 90, 100);
addbutton.addActionListener(new Action());
add(addbutton);
list = new JList();
dropTarget = new DropTarget(list, this);
list.setModel(listModel);
list.setDragEnabled(true);
FileListCellRenderer renderer = new FileListCellRenderer();
list.setCellRenderer(renderer);
list.addMouseListener(new mouselistner());
list.clearSelection();
list.setFixedCellHeight(40);
droparea = new JScrollPane();
droparea.setViewportView(list);
droparea.setBounds(10, 150, 635, 330);
add(droparea);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public void dragEnter(DropTargetDragEvent arg0) {
// nothing
}
public void dragOver(DropTargetDragEvent arg0) {
// nothing
}
public void dropActionChanged(DropTargetDragEvent arg0) {
// nothing
}
public void dragExit(DropTargetEvent arg0) {
// nothing
}
public void drop(DropTargetDropEvent evt) {
int action = evt.getDropAction();
evt.acceptDrop(action);
try {
Transferable data = evt.getTransferable();
if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
List<File> files = (List<File>) data
.getTransferData(DataFlavor.javaFileListFlavor);
for (File file : files) {
listModel.addElement(file);
}
}
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
evt.dropComplete(true);
}
}
/** A FileListCellRenderer for a File. */
class FileListCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = -7799441088157759804L;
private FileSystemView fileSystemView;
private JLabel label;
private Color textSelectionColor = Color.BLACK;
private Color backgroundSelectionColor = Color.CYAN;
private Color textNonSelectionColor = Color.BLACK;
private Color backgroundNonSelectionColor = Color.WHITE;
FileListCellRenderer() {
label = new JLabel();
label.setOpaque(true);
fileSystemView = FileSystemView.getFileSystemView();
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean selected, boolean expanded) {
label.setBorder(new BevelBorder(BevelBorder.RAISED,
Color.LIGHT_GRAY, Color.GRAY, null, null));
File file = (File) value;
label.setIcon(fileSystemView.getSystemIcon(file));
label.setText(fileSystemView.getSystemDisplayName(file));
label.setToolTipText(file.getPath());
if (selected) {
label.setBackground(Color.blue);
label.setForeground(textSelectionColor);
} else {
label.setBackground(backgroundNonSelectionColor);
label.setForeground(textNonSelectionColor);
}
return label;
}
}
class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addbutton){
// FileSystemView fsv=FileSystemView.getFileSystemView();
JFileChooser filechooser=new JFileChooser();
filechooser.setMultiSelectionEnabled(true);
filechooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
File files=filechooser.getSelectedFile();
filechooser.showDialog(null, "add"); //here panel problem i think
listModel.addElement(files); //and here run time error genrate
}
}
}
}
這兩個問題在最後2行
你得到了什麼異常? –
你想發佈stacktrace嗎? –
我們很難從這麼多的代碼中找出它。嘗試儘可能多地剝離...閱讀此指南:http://sscce.org/ – durron597