- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在为使用自定义 TreeModel
的 JTree
实现自定义 TransferHandler
时遇到问题。问题来自于我用来管理数据的特定 TreeModel
。
据我了解,在 swing 中拖放的工作原理如下:
这对我来说是一个大问题,因为我的模型不能两次包含任何数据。我需要的是:
我用谷歌搜索了这个,我发现的唯一的东西是一个小黑客,基本上是滥用 importData 方法来首先删除数据并忽略 exportDone 方法。
这适用于拖放,但会破坏 CCP 功能。CCP 已损坏,因为在 exportDone
方法中我无法确定导出是拖放还是剪切。如果是剪切,我需要从模型中删除数据,但如果是掉落,则不需要。
此外,当涉及带有复制和剪切的 importData
方法时,我还有另一个问题。如果是副本,我需要克隆我的数据,但是当它是剪切时,我不需要克隆,实际上我更愿意不这样做来保留旧引用。但在 importData
方法中给出的唯一参数是一个 TransferSupport
对象。TransferSupport
无法告诉您该操作是复制还是剪切操作。
如果有帮助的话,这是代码:(它已经很大了,抱歉)
package pkg;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JComponent;
public class TransferActionListener implements ActionListener, PropertyChangeListener {
private JComponent focusOwner = null;
/*
* This class is taken from the oracle tutorial website for Copy-Cut-Paste support.
* http://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html
*/
public static final TransferActionListener INSTANCE = new TransferActionListener();
private TransferActionListener() {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addPropertyChangeListener("permanentFocusOwner", this);
}
public void propertyChange(PropertyChangeEvent e) {
Object obj = e.getNewValue();
if (obj instanceof JComponent) {
focusOwner = (JComponent)obj;
} else {
focusOwner = null;
}
}
public void actionPerformed(ActionEvent e) {
if (focusOwner == null) {
return;
}
String action = (String) e.getActionCommand();
Action a = focusOwner.getActionMap().get(action);
if (a != null) {
a.actionPerformed(new ActionEvent(focusOwner, ActionEvent.ACTION_PERFORMED, null));
}
}
}
package pkg;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class ObjectTransferable<E> implements Transferable {
/*
* This class can be used to transfer any kind of java class.
* Can only be used within the same JVM.
*/
private final DataFlavor[] flavors;
private final E obj;
public ObjectTransferable(E object) throws ClassNotFoundException {
obj = object;
flavors = new DataFlavor[] {
new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class="+object.getClass().getName())
};
}
public ObjectTransferable(E object, DataFlavor flavor) {
obj = object;
flavors = new DataFlavor[] {
flavor
};
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return obj;
}
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavors[0].equals(flavor);
}
}
package pkg;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Node {
private final String name;
private final List<Node> children;
private MyModel model;
private Node parent;
public Node(String name) {
this.name = name;
children = new ArrayList<>();
parent = null;
}
protected void setModel(MyModel model) {
this.model = model;
for (Node child : getChildren()) {
child.setModel(model);
}
}
protected MyModel getModel() {
return model;
}
protected void setParent(Node node) {
parent = node;
}
public Node getParent() {
return parent;
}
public void addChild(Node child) {
addChild(child, getChildren().size());
}
public void addChild(Node child, int index) {
if (child.getParent() == this) {
throw new IllegalArgumentException("Node '"+child+"' is already a child of '"+this+"'.");
}
if (child.getParent() != null) {
throw new IllegalArgumentException("Node '"+child+"' already has a parent.");
}
child.setParent(this);
child.setModel(getModel());
children.add(index, child);
fireInsertEvent(child, index);
}
public void removeChild(Node child) {
if (child.getParent() != this) {
throw new IllegalArgumentException("Node '"+child+"' is not a child of '"+this+"'.");
}
int index = children.indexOf(child);
fireRemoveEvent(child, index);
child.setParent(null);
child.setModel(null);
children.remove(index);
}
public List<Node> getChildren() {
return Collections.unmodifiableList(children);
}
protected void fireInsertEvent(Node node, int index) {
if (getModel() != null) {
getModel().fireInsertEvent(node, index);
}
}
protected void fireRemoveEvent(Node node, int index) {
if (getModel() != null) {
getModel().fireRemoveEvent(node, index);
}
}
public String toString() {
return name;
}
}
package pkg;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class MyModel implements TreeModel {
private final List<TreeModelListener> listeners;
private Node root;
public MyModel(Node rootNode) {
listeners = new ArrayList<>();
root = rootNode;
root.setModel(this);
}
public Object getRoot() {
return root;
}
/**
* Returns the parent node for the given child.
* Assumes that the child is an object of type Node.
* @param child
* @return
*/
public Object getParent(Object child) {
Node childNode = (Node) child;
return childNode.getParent();
}
/**
* Returns the child node at index for the given parent.
* Assumes that the parent is an object of type Node.
* @param child
* @return
*/
public Object getChild(Object parent, int index) {
Node parentNode = (Node) parent;
return parentNode.getChildren().get(index);
}
/**
* Returns the number of children the parent has.
* Assumes that the parent is an object of type Node.
* @param child
* @return
*/
public int getChildCount(Object parent) {
Node parentNode = (Node) parent;
return parentNode.getChildren().size();
}
/**
* Returns the index of child within the given parent.
* Returns -1 if child is not a child of parent.
* Assumes that the parent is an object of type Node.
* @param child
* @return
*/
public int getIndexOfChild(Object parent, Object child) {
Node parentNode = (Node) parent;
return parentNode.getChildren().indexOf(child);
}
/**
* Returns true if the given node does not have any children.
* Assumes that node is an object of type Node.
* @param child
* @return
*/
public boolean isLeaf(Object node) {
Node someNode = (Node) node;
return someNode.getChildren().isEmpty();
}
/**
* Removes all nodes, within the iterable, from this model.
* If an object from the iterable is not a Node this method will throw an exception.
* @param nodes
*/
public void removeNodes(Iterable<Object> nodes) {
for (Object obj : nodes) {
Node node = (Node) obj;
Node parent = node.getParent();
parent.removeChild(node);
}
}
/**
* Adds all nodes, within the iterable, as children to the given parent.
* Starts the insertion at startIndex and counts up by one for each insertion.
* If an object from the iterable is not a Node this method will throw an exception.
* @param parent
* @param startIndex
* @param nodes
*/
public void insertNodes(Object parent, int startIndex, Iterable<Object> nodes) {
Node parentNode = (Node) parent;
if (startIndex > parentNode.getChildren().size()) {
startIndex = parentNode.getChildren().size();
}
for (Object obj : nodes) {
Node child = (Node) obj;
parentNode.addChild(child, startIndex++);
}
}
/**
* Not used and not implement.
* Will throw an {@link UnsupportedOperationException} if called.
*/
public void valueForPathChanged(TreePath path, Object newValue) {
// Never being used.
throw new UnsupportedOperationException("Not implemented.");
}
public void addTreeModelListener(TreeModelListener l) {
listeners.add(l);
}
public void removeTreeModelListener(TreeModelListener l) {
listeners.remove(l);
}
/**
* Constructs a TreeModelEvent for the given node and index
* and calls treeNodesInserted on all registered listeners.
* The node must never be null.
* @param node
* @param index
*/
protected void fireInsertEvent(Node node, int index) {
TreeModelEvent e = makeEvent(node, index);
for (TreeModelListener l : listeners) {
l.treeNodesInserted(e);
}
}
/**
* Constructs a TreeModelEvent for the given node and index
* and calls treeNodesRemoved on all registered listeners.
* The node must never be null.
* @param node
* @param index
*/
protected void fireRemoveEvent(Node node, int index) {
TreeModelEvent e = makeEvent(node, index);
for (TreeModelListener l : listeners) {
l.treeNodesRemoved(e);
}
}
/**
* Creates a TreeModelEvent for the given node and index.
* The node must never be null.
* @param node
* @param index
* @return
*/
protected TreeModelEvent makeEvent(Node node, int index) {
return new TreeModelEvent(this, makePath(node), asArray(index), asArray(node));
}
/**
* Creates a {@link TreePath} for the given node.
* The last component in the path will be the given node.
* The root of the tree will not be a part of the path.
* @param node
* @return
*/
protected TreePath makePath(Object node) {
if (node == null) {
throw new NullPointerException();
}
Deque<Object> pathAsStack = new LinkedList<>();
Object current = node;
while (current != null) {
pathAsStack.add(current);
current = getParent(current);
}
Object[] pathAsArray = new Object[pathAsStack.size() - 1];
int index = 0;
while (pathAsStack.size() > 1) {
pathAsArray[index++] = pathAsStack.pollLast();
}
return new TreePath(pathAsArray);
}
/**
* Simple wrapper.
* @param index
* @return
*/
protected int[] asArray(int index) {
return new int[] {index};
}
/**
* Simple wrapper.
* @param index
* @return
*/
protected Object[] asArray(Object obj) {
return new Object[] {obj};
}
}
package pkg;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;
public class JTreeTransferHandler extends TransferHandler {
private static final long serialVersionUID = 1L;
// This flavor will be used for the transfers.
private final DataFlavor nodeFlavor;
public JTreeTransferHandler() {
// We always transfer a List of objects.
nodeFlavor = new DataFlavor(List.class, List.class.getSimpleName());
}
/*
* Next three methods will handle the canImport functionality.
* canImport determines whether an import can take place or is rejected.
* We will treat this differently for Drag & Drop and Copy-Cut-Paste.
*/
public boolean canImport(TransferHandler.TransferSupport support) {
try {
// First, check for the right flavor.
if (!support.isDataFlavorSupported(nodeFlavor)) {
return false;
}
// Then, handle the special cases.
if (support.isDrop()) {
return canImportDrop(support);
} else {
return canImportPaste(support);
}
} catch (Exception e) {
/*
* We do this because otherwise the exception would be swallowed by swing
* and we wont know what happened.
*/
e.printStackTrace();
throw e;
}
}
private boolean canImportDrop(TransferHandler.TransferSupport support) {
support.setShowDropLocation(true);
/*
* Can not drop a path on itself or on a descendant of itself.
* We know, that the component is a JTree.
*/
JTree tree = (JTree) support.getComponent();
JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
TreePath dropPath = dl.getPath();
/*
* If one of the selected paths is supposed to be dropped on
* itself or a descendant of itself, return false.
*/
TreePath[] selectedPaths = tree.getSelectionPaths();
for (TreePath selectedPath : selectedPaths) {
if (selectedPath.isDescendant(dropPath)) {
return false;
}
}
// Otherwise, return true.
return true;
}
private boolean canImportPaste(TransferHandler.TransferSupport support) {
/*
* Can only paste nodes if tree has exactly one path selected.
* Otherwise the paste location is not known...
*/
JTree tree = (JTree) support.getComponent();
TreePath[] selectedPaths = tree.getSelectionPaths();
return selectedPaths.length == 1 && selectedPaths[0] != null;
}
/*
* Next three methods will handle the importData functionality.
* importData will insert the data into our model.
* We will treat this differently for Drag & Drop and Copy-Cut-Paste.
*/
public boolean importData(TransferHandler.TransferSupport support) {
try {
// Check if we can import.
if(!canImport(support)) {
return false;
}
// Handle the different situations.
if (support.isDrop()) {
return importDataDrop(support);
} else {
return importDataPaste(support);
}
} catch (Exception e) {
/*
* We do this because otherwise the exception would be swallowed by swing
* and we wont know what happened.
*/
e.printStackTrace();
throw e;
}
}
private boolean importDataDrop(TransferHandler.TransferSupport support) {
/*
* When dropped the action is a MOVE command.
* We must first remove the old data, and then insert the new data.
*/
List<Object> data = extractImportData(support);
/*
* We know, that the component is always a JTree and the model is always a MyModel.
*/
JTree tree = (JTree) support.getComponent();
MyModel model = (MyModel) tree.getModel();
// Extract drop location and drop index
JTree.DropLocation dl = (JTree.DropLocation)support.getDropLocation();
TreePath destPath = dl.getPath();
Object parent = destPath.getLastPathComponent();
int index = dl.getChildIndex();
if (index == -1) {
// Drop location is on top of a node
index = model.getChildCount(parent);
}
// First remove data
model.removeNodes(data);
// Then insert data
model.insertNodes(parent, index, data);
return true;
}
private boolean importDataPaste(TransferHandler.TransferSupport support) {
/*
* This is either a copy & paste or a cut & paste.
* If this was a copy & paste we need to clone the data!
* If this was a cut & paste we can simply insert it.
*
* Unfortunately, there is no good way to know...
*/
List<Object> data = extractImportData(support);
// no way to know... what a bummer.
int action = MOVE;
if ((action & COPY) == COPY) {
// When we copy, then clone the list data!
// somehow clone the data...
}
/*
* We know, that the component is always a JTree and the model is always a MyModel.
*/
JTree tree = (JTree) support.getComponent();
MyModel model = (MyModel) tree.getModel();
// Extract drop location and drop index
// Drop location depends on selection
TreePath destPath = tree.getSelectionPath();
Object parent;
// Path can be null if nothing is selected.
if (destPath == null) {
parent = model.getRoot();
} else {
parent = destPath.getLastPathComponent();
}
int index = model.getChildCount(parent);
/*
* Inserts the new nodes into the model.
* Nodes must NOT be contained in the model at this point!
*/
model.insertNodes(parent, index, data);
return true;
}
/*
* This method handles the removal of data if the action was a Cut.
*/
protected void exportDone(JComponent c, Transferable data, int action) {
// Only a move action needs to remove the old data.
if (action != MOVE) {
return;
}
/*
* When this is a drag & drop, do nothing.
* When this was a cut, then remove the old data.
*/
// no way to know... what a bummer.
boolean isDragAndDrop = true;
if (!isDragAndDrop) {
// Extract nodes from data
List<Object> nodes = extractImportData(data);
// The component is always a JTree and always has a TreeModel2 as its model
JTree tree = (JTree) c;
MyModel model = (MyModel) tree.getModel();
// Remove the nodes from the model
// This will throw an exception if the nodes are not contained in the model!
model.removeNodes(nodes);
}
}
/*
* Creates our Transferable as a list of all selected paths in the tree.
*/
protected Transferable createTransferable(JComponent c) {
try {
// Component is always a JTree
JTree tree = (JTree) c;
// Extract nodes to be transfered => Always the selected nodes
TreePath[] paths = tree.getSelectionPaths();
if(paths != null) {
List<Object> nodeList = new ArrayList<>();
for (TreePath path : paths) {
nodeList.add(path.getLastPathComponent());
}
return new ObjectTransferable<List<Object>>(nodeList, nodeFlavor);
}
return null;
} catch (Exception e) {
/*
* We do this because otherwise the exception would be swallowed by swing
* and we wont know what happened.
*/
e.printStackTrace();
throw e;
}
}
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
/*
* Utility methods for extracting data from a transfer.
*/
private List<Object> extractImportData(TransferHandler.TransferSupport support) {
return extractImportData(support.getTransferable());
}
@SuppressWarnings("unchecked")
private List<Object> extractImportData(Transferable trans) {
try {
return (List<Object>) trans.getTransferData(nodeFlavor);
} catch (Exception e) {
// We dont need a checked exception because we wont do anything with it anyways.
throw new RuntimeException(e);
}
}
}
package pkg;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.Action;
import javax.swing.DropMode;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.TransferHandler;
import javax.swing.tree.TreeSelectionModel;
public class App {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new App();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* The number of nodes that will be randomly constructed. Must be smaller then NODE_NAMES.length.
*/
private static final int NODE_COUNT = 10;
/**
* An array containing random names that will be used for constructing the tree.
*/
private static final String[] NODE_NAMES = new String[] {
"Albert", "Annabell", "Benjamin", "Bella", "Cedric", "Cecile",
"David", "Danielle", "Emanuel", "Elisabeth", "Frederick", "Felicita",
"Georg", "Giselle", "Hans", "Henriette", "Ismael", "Irene",
"Joshua", "Joceline", "Kyle", "Kaithlin", "Lyod", "Lisa",
"Michael", "Michelle", "Norbert", "Nele", "Olaf", "Ophelia",
"Robert", "Renate", "Stuart", "Sabrina", "Theo", "Tania",
"Ulric", "Ursula", "Victor", "Veronica", "William", "Wilma"
};
/*
* If the static final variables have illegal values we will throw an exception at class initialization.
*/
static {
if (NODE_NAMES.length < NODE_COUNT) {
throw new RuntimeException("Node count must be no bigger then: "+NODE_NAMES.length);
}
}
public App() {
// Setup the frame
JFrame frmTreeModelTest = new JFrame();
frmTreeModelTest.setTitle("JTree Transfer Handler Test");
frmTreeModelTest.setSize(600, 480);
frmTreeModelTest.setLocationRelativeTo(null);
frmTreeModelTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Scroll panel for the tree
JScrollPane scrollPane = new JScrollPane();
frmTreeModelTest.getContentPane().add(scrollPane, BorderLayout.CENTER);
/*
* Construct our initial nodes.
* This will create a random tree which contains all kinds of names.
*/
Node rootNode = new Node("Root");
List<String> possibleNames = new ArrayList<>(Arrays.asList(NODE_NAMES));
List<Node> existingNodes = new ArrayList<>();
existingNodes.add(rootNode);
Random random = new Random();
for (int i = 0; i < NODE_COUNT; i++) {
int nameID = random.nextInt(possibleNames.size());
Node node = new Node(possibleNames.remove(nameID));
int parentID = random.nextInt(existingNodes.size());
Node parent = existingNodes.get(parentID);
parent.addChild(node);
existingNodes.add(node);
}
// The JTree that will be used for this test
JTree tree = new JTree();
tree.setModel(new MyModel(rootNode));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
tree.setTransferHandler(new JTreeTransferHandler());
tree.setDragEnabled(true);
tree.setDropMode(DropMode.ON_OR_INSERT);
/*
* This code was taken from the oracle tutorial website for Copy-Cut-Paste support.
* http://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html
*/
tree.getActionMap().put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
tree.getActionMap().put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
tree.getActionMap().put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
scrollPane.setViewportView(tree);
// Construct the menu bar with CCP functionality.
JMenuBar menuBar = new JMenuBar();
frmTreeModelTest.setJMenuBar(menuBar);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenuItem mntmCopy = new JMenuItem("Copy");
mntmCopy.addActionListener(TransferActionListener.INSTANCE);
mntmCopy.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
mntmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
mnEdit.add(mntmCopy);
JMenuItem mntmCut = new JMenuItem("Cut");
mntmCut.addActionListener(TransferActionListener.INSTANCE);
mntmCut.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
mntmCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
mnEdit.add(mntmCut);
JMenuItem mntmPaste = new JMenuItem("Paste");
mntmPaste.addActionListener(TransferActionListener.INSTANCE);
mntmPaste.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
mntmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
mnEdit.add(mntmPaste);
// Show the frame
frmTreeModelTest.setVisible(true);
}
}
拖放功能有效,但复制-剪切-粘贴不太有效,因为缺少信息......
最佳答案
在此之前,我假设您知道 exportDone 在用于拖放的 importData 之后调用,在用于 ccp(剪切复制)的 importData 之前调用
另外我假设您知道拖动将调用剪切操作,而 ctrl+拖动将调用复制操作..
因此,为了知道操作是 dnd 还是 ccp,您必须在 importData
处设置一个标志这里我使用 isDrag 标志来设置它..
public class JTreeTransferHandler extends TransferHandler {
private final DataFlavor nodeFlavor;
Boolean isCut;
Boolean isdrag = false;
@Override
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
@Override
protected Transferable createTransferable(JComponent source) {
}
@Override
protected void exportDone(JComponent source, Transferable data, int action) {
isCut = action == MOVE; //to check whether the operation is cut or copy
if (isdrag) {
if (isCut) {
//Implement you drag code (normal drag)
} else {
//Implement you ctrl+drag code
}
}
isdrag = false; //resetting the dnd flag
}
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDataFlavorSupported(DataFlavors.nodeFlavor)) {
return false;
}
if (support.isDrop()) {
return canImportDnd();
} else {
return canImportccp();
}
return false;
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
if (support.isDrop()) {
isdrag = true;//To know whether it is a drag and drop in exportdone
if (support.getDropAction() == MOVE) {
//Implement you drag code (normal drag)
} else if (support.getDropAction() == COPY) {
//Implement you ctrl+drag code
}
} else if (isCut) {
//Implement you cut ctrl+x code
}
return true;
}
}
关于java - 自定义 TransferHandler 的 D&D 和 CCP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590606/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!