AWT FileDialog 文件名 copy/paste 键击?
AWT FileDialog filename copy/paste keystrokes?
我通常不编写 java GUI 应用程序,但我需要一个简单的实用程序,我设法使用 Swing 和 AWT 编写了它。该实用程序需要打开和保存文件,主要用于 MacOS。 Apple recommends 使用 AWT 的 FileDialog
而不是 Swing 文件选择器,因为 FileDialog
更像本地 Macos 文件对话框。这就是我所做的。
完成的实用程序运行良好,除了一件事我无法解决。保存文件的对话框包括一个文本框,用于输入文件名。右键单击文本框会显示一个带有复制和粘贴选项的菜单。但是相关的击键(Cmd-C、Cmd-V)没有做任何事情。
下面的程序演示了这个问题:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Scratch extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
JButton saveButton;
FileDialog fd;
public Scratch(Frame aFrame) {
super(new BorderLayout());
fd = new FileDialog(aFrame, "Save", FileDialog.SAVE);
saveButton = new JButton("Save a File...");
saveButton.addActionListener(this);
this.add(saveButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton) {
fd.setVisible(true);
String file = fd.getFile();
System.out.println(file);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Scratch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Scratch(frame));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
当您 运行 它会打开一个带有保存按钮的 window。单击该按钮将打开一个带有 "Save as" 字段的 FileDialog。您可以在该字段中键入内容,然后右键单击该字段,然后从弹出菜单中选择 select "copy" 或 "paste"。但是您不能使用 Cmd-V 粘贴到字段中——似乎没有任何键击绑定到复制或粘贴操作。
是否有直接的方法将击键绑定到 FileDialog
中的文件名框?
AWT FileDialog(至少在 MacOS 上 java 8 下)最终调用 CFileDialog. nativeRunFileDialog() 以显示实际对话框。传递给此方法的参数似乎不包含任何可用于将击键附加到保存对话框的文件名字段的内容。
我的结论是,没有合理的方法来更改 FileDialog 以将击键添加到保存选项。我已经恢复到应用程序中的 Swing JFileChooser。
我通常不编写 java GUI 应用程序,但我需要一个简单的实用程序,我设法使用 Swing 和 AWT 编写了它。该实用程序需要打开和保存文件,主要用于 MacOS。 Apple recommends 使用 AWT 的 FileDialog
而不是 Swing 文件选择器,因为 FileDialog
更像本地 Macos 文件对话框。这就是我所做的。
完成的实用程序运行良好,除了一件事我无法解决。保存文件的对话框包括一个文本框,用于输入文件名。右键单击文本框会显示一个带有复制和粘贴选项的菜单。但是相关的击键(Cmd-C、Cmd-V)没有做任何事情。
下面的程序演示了这个问题:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Scratch extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
JButton saveButton;
FileDialog fd;
public Scratch(Frame aFrame) {
super(new BorderLayout());
fd = new FileDialog(aFrame, "Save", FileDialog.SAVE);
saveButton = new JButton("Save a File...");
saveButton.addActionListener(this);
this.add(saveButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton) {
fd.setVisible(true);
String file = fd.getFile();
System.out.println(file);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Scratch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Scratch(frame));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
当您 运行 它会打开一个带有保存按钮的 window。单击该按钮将打开一个带有 "Save as" 字段的 FileDialog。您可以在该字段中键入内容,然后右键单击该字段,然后从弹出菜单中选择 select "copy" 或 "paste"。但是您不能使用 Cmd-V 粘贴到字段中——似乎没有任何键击绑定到复制或粘贴操作。
是否有直接的方法将击键绑定到 FileDialog
中的文件名框?
AWT FileDialog(至少在 MacOS 上 java 8 下)最终调用 CFileDialog. nativeRunFileDialog() 以显示实际对话框。传递给此方法的参数似乎不包含任何可用于将击键附加到保存对话框的文件名字段的内容。
我的结论是,没有合理的方法来更改 FileDialog 以将击键添加到保存选项。我已经恢复到应用程序中的 Swing JFileChooser。