ActionListener/actionPerformed 类中的扫描仪错误
大家好,我正在开发一个用 Java 制作的十六进制转储实用程序。我在使用十六进制/ascii 逻辑之前使用的扫描仪存在一些问题。
现在,由于调试原因,我已经排除了逻辑和其他一些内容,但如果有人知道发生了什么,我想知道!非常感谢提示。
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(" ");
}
});
}
}
}
Hey everyone, I'm working on a hex dump utility made in Java. I'm having some issues with the Scanner I'd be using before the hex/ascii logic.
Right now I have the logic and a few other things excluded for debugging reasons, but if anyone knows what's going on I'd like to know! Hints greatly appreciated.
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(" ");
}
});
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要编译您的示例,您可以将
new Scanner
行包装在 try catch 中,如下所示:完成此操作后,您就可以按照您想要的方式继续处理文件的行。
To get your example to compile, you can wrap the
new Scanner
line in a try catch like this:Once you do that, then you can proceed to processing the lines of the file the way you intended.
我能看到的唯一问题是 Scanner 构造函数 抛出 FileNotFoundException;因此,您需要将其包含在 try/catch 块中。
它应该看起来像这样(仅适用于初学者):
您可能还会找到 Java 异常教程很有帮助。
Your only problem that I can see is that the Scanner constructor throws FileNotFoundException; therefore, you need to enclose it in a try/catch block.
It should look something like this (just for starters):
You might also find the Java Exceptions Tutorial helpful.