所以我不想讓textfeild打開一個文本文件,閱讀它,並將其內容複製到它。我不要那個。如何從文本字段打開任何文件?
我想知道的是如何使用這個文本框打開任何類型的文件。我曾嘗試此代碼,這是行不通的:
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("serial")
public class Console extends JFrame implements KeyListener{
TextArea c = new TextArea("", 30,30, TextArea.SCROLLBARS_VERTICAL_ONLY);
JTextField command = new JTextField(2);
public Console(String title) {
super(title);
c.setEditable(false);
c.setBackground(Color.BLACK);
c.setForeground(Color.GREEN);
c.setFont(new Font("Courier", 12, 16));
c.setText("Booted up the console" + "\n" + "Enter a command...");
command.setBackground(Color.BLACK);
command.setForeground(Color.GREEN);
command.setFont(new Font("Courier", 12, 16));
add(c, BorderLayout.PAGE_START);
add(command, BorderLayout.PAGE_END);
command.addKeyListener(this);
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
String COMMAND = command.getText();
String otherCommand = command.getText();
Desktop desktop = Desktop.getDesktop();
File file = new File(otherCommand);
String f = file.toString();
if (COMMAND.equals("open")) {
c.append("\n" + "Enter file to open:");
if(otherCommand.equals(f)) {
try {
desktop.open(file);
} catch (IOException e1) {
c.append("Invalid file location");
}
}
}
if (COMMAND.equals("exit")) {
c.append("\n" + "Exiting...");
System.exit(0);
}
}
}
public void windowActivated(WindowEvent e){
}
public void windowDeactivated(WindowEvent e){
}
public void windowIconified(WindowEvent e){
}
public void windowDeiconified(WindowEvent e){
}
public void windowOpened(WindowEvent e){
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowClosed(WindowEvent e){
System.exit(0);
}
}
我已經遇到問題最主要的是在此位的代碼:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
String COMMAND = command.getText();
String otherCommand = command.getText();
Desktop desktop = Desktop.getDesktop();
File file = new File(otherCommand);
String f = file.toString();
if (COMMAND.equals("open")) {
c.append("\n" + "Enter file to open:");
if(otherCommand.equals(f)) {
try {
desktop.open(file);
} catch (IOException e1) {
c.append("Invalid file location");
}
}
}
if (COMMAND.equals("exit")) {
c.append("\n" + "Exiting...");
System.exit(0);
}
}
}
我得到這個錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: open doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at com.parth.Console.keyPressed(Console.java:52)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
千萬不要永遠不要將KeyListener添加到JTextField。永遠。改用ActionListener,因爲這樣當用戶輸入數據時,監聽器可以響應,然後按''。但是,就像@AndyTurner提到的那樣,你試圖打開一個不存在的文件。 –
呃,你試圖打開的文件是否存在?該文件的名稱是什麼? –
也可以在**用戶有機會輸入任何內容之前從JTextField **中獲取文本。 –