我正在使用Netbeans开发将在Windows中使用的应用程序。我注意到我无法右键单击进行复制或粘贴。我怎样才能启用它? (我使用的是基本的Swing控件,如JText和JTextArea。)
答案 0 :(得分:13)
Why right click is not working on java application?
我不会创建新的副本,剪切,粘贴,撤消和放大选择所有操作,因为那些已经存在于每个组件的ActionMap中。我只是这样做:
Action copyAction = textField.getActionMap().get("copy");
Action cutAction = textField.getActionMap().get("cut");
Action pasteAction = textField.getActionMap().get("paste");
Action undoAction = textField.getActionMap().get("undo");
Action selectAllAction = textField.getActionMap().get("selectAll");
popup.add (undoAction);
popup.addSeparator();
popup.add (cutAction);
popup.add (copyAction);
popup.add (pasteAction);
popup.addSeparator();
popup.add (selectAllAction);
return popup;
这样您就不会重新创建已编写的更多代码。除此之外,我将遵循这个例子。
答案 1 :(得分:1)
征求意见,
它是否与操作系统交互?我想从我的Java应用程序中复制一些内容并粘贴到记事本上。
为什么要打扰用户手动复制粘贴,为什么不将textarea的内容写入文本文件?
答案 2 :(得分:1)
final int colx;
final int rowy;
final String val1;
colx = CFDTable.getSelectedColumn();
rowy = CFDTable.getSelectedRow();
val1 = (String) CFDTable.getValueAt(rowy, colx);
JPopupMenu jPopupMenu = new javax.swing.JPopupMenu();
jPopupMenu.setName("jPopupMenu");
CFDTable.setComponentPopupMenu(jPopupMenu);
JMenuItem jMenuItem = new javax.swing.JMenuItem();
jMenuItem.setText("Copy"); // NOI18N
jMenuItem.setName("jMenuItem"); // NOI18N
jPopupMenu.add(jMenuItem);
jMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringSelection entry = new StringSelection(val1);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(entry, entry);
}
});
答案 3 :(得分:1)
静态类,可以立即将常规弹出菜单添加到文本字段中。
只需调用JTextFieldRegularPopupMenu.addTo(jTextFieldObj);
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import javax.swing.undo.*;
public class JTextFieldRegularPopupMenu {
public static void addTo(JTextField txtField)
{
JPopupMenu popup = new JPopupMenu();
UndoManager undoManager = new UndoManager();
txtField.getDocument().addUndoableEditListener(undoManager);
Action undoAction = new AbstractAction("Undo") {
@Override
public void actionPerformed(ActionEvent ae) {
if (undoManager.canUndo()) {
undoManager.undo();
}
else {
JOptionPane.showMessageDialog(null,
"Undoable: " + undoManager.canUndo() ,
"Undo Status",
JOptionPane.INFORMATION_MESSAGE);
}
}
};
Action copyAction = new AbstractAction("Copy") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.copy();
}
};
Action cutAction = new AbstractAction("Cut") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.cut();
}
};
Action pasteAction = new AbstractAction("Paste") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.paste();
}
};
Action selectAllAction = new AbstractAction("Select All") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.selectAll();
}
};
popup.add (undoAction);
popup.addSeparator();
popup.add (cutAction);
popup.add (copyAction);
popup.add (pasteAction);
popup.addSeparator();
popup.add (selectAllAction);
txtField.setComponentPopupMenu(popup);
}
}
答案 4 :(得分:0)
任何JTextComponent的完整工作模型,包括非功能性操作以及在文本选择状态下启用/禁用菜单项。
c:\xyz\desktop\practice