أخطاء الماسح الضوئي في ActionListener/ActionPerformed
-
23-09-2019 - |
سؤال
مرحبًا بالجميع ، أنا أعمل على أداة تفريغ سداسية مصنوعة في جافا. أواجه بعض المشكلات مع الماسح الضوئي الذي سأستخدمه قبل منطق Hex/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(" ");
}
});
}
}
}
المحلول
مشكلتك الوحيدة التي يمكنني رؤيتها هي أن مُلاحق الماسح الضوئي يلقي FileNotfoundException; ؛ لذلك ، تحتاج إلى إرفاقه في كتلة المحاولة/التقاط.
يجب أن تبدو مثل هذا (فقط للمبتدئين):
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
try {
Scanner input = new Scanner(file);
} catch (FileNotFoundException ex) {
System.err.println("Error opening " + file.getName());
}
}
}
});
قد تجد أيضًا جافا استثناءات البرنامج التعليمي معاون، مساعد، مفيد، فاعل خير.
نصائح أخرى
للحصول على مثالك للتجميع ، يمكنك لف new Scanner
خط في محاولة مثل هذا:
Scanner input = null;
try {
input = new Scanner(file);
} catch(FileNotFoundException ex) {
// handle this condition appropriately
}
بمجرد القيام بذلك ، يمكنك المتابعة إلى معالجة خطوط الملف بالطريقة التي تقصدها.
لا تنتمي إلى StackOverflow