Java ActionListener/actionPerformed类中的扫描程序错误
大家好,我正在开发一个用Java制作的十六进制转储实用程序。我在十六进制/ascii逻辑之前使用的扫描仪有一些问题 现在,出于调试的原因,我已经排除了逻辑和其他一些东西,但是如果有人知道发生了什么,我想知道!非常感谢Java ActionListener/actionPerformed类中的扫描程序错误,java,actionlistener,java.util.scanner,Java,Actionlistener,Java.util.scanner,大家好,我正在开发一个用Java制作的十六进制转储实用程序。我在十六进制/ascii逻辑之前使用的扫描仪有一些问题 现在,出于调试的原因,我已经排除了逻辑和其他一些东西,但是如果有人知道发生了什么,我想知道!非常感谢 package filedumputility; import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.io.*; public cla
package filedumputility;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileDump extends JFrame {
public FileDump() throws Exception{
setTitle("File Dump Utility");
add(new DumpPanel());
}
//Main method
public static void main(String[] args) throws Exception {
FileDump frame = new FileDump();
frame.setSize(500, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
//Class to creaste components
class DumpPanel extends JPanel {
private JFileChooser fileChooser = new JFileChooser();
private JTextArea dumpArea = new JTextArea(11, 20);
private JTextField fileName = new JTextField(16);
private JButton newButton = new JButton("New File");
private JButton clearButton = new JButton("Clear");
private JButton dumpButton = new JButton("Dump File");
private JScrollPane scrollPane = new JScrollPane(dumpArea);
private JPanel p = new JPanel();
private Scanner input;
//Class to add components and read the file
public DumpPanel() throws Exception {
p.setLayout(new FlowLayout());
setLayout(new BorderLayout());
add(p, BorderLayout.NORTH);
p.add(fileName);
p.add(dumpButton);
p.add(newButton);
p.add(clearButton);
add(scrollPane, BorderLayout.SOUTH);
dumpArea.setEditable(false);
fileName.setEditable(false);
//Create event for the new file button
newButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(fileChooser.showOpenDialog(null)
== JFileChooser.APPROVE_OPTION) {
//Get the file
java.io.File file = fileChooser.getSelectedFile();
//Set file name bar to the selected file
fileName.setText(file.getName());
}
}
});
dumpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(fileChooser.showOpenDialog(null)
== JFileChooser.APPROVE_OPTION) {
//Get the file
java.io.File file = fileChooser.getSelectedFile();
//Create scanner
Scanner input = new Scanner(file);
}
}
});
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dumpArea.setText(" ");
fileName.setText(" ");
}
});
}
}
}
要编译您的示例,您可以将
新扫描仪
行包装成如下的try-catch:
Scanner input = null;
try {
input = new Scanner(file);
} catch(FileNotFoundException ex) {
// handle this condition appropriately
}
一旦你这样做了,你就可以按照你想要的方式继续处理文件的行了。要编译你的例子,你可以将
新的扫描器
行包装在一个像这样的尝试捕捉中:
Scanner input = null;
try {
input = new Scanner(file);
} catch(FileNotFoundException ex) {
// handle this condition appropriately
}
一旦你这样做了,你就可以继续按照你想要的方式处理文件的行了。我能看到的唯一问题是扫描器构造器;因此,您需要将其包含在try/catch块中 它应该是这样的(仅用于初学者):
你可能也会发现这很有帮助。我能看到的唯一问题是扫描器构造函数;因此,您需要将其包含在try/catch块中 它应该是这样的(仅用于初学者): 你可能也会发现这个方法很有用