所以问题是这个。我有一个GUI打开一个JFileChooser并且用户选择一个文件,当命中OK时,一个新的类被调用(Amostra Sample = new Amostra(namefile),其中namefile是绝对路径)它将从文件中预处理一些数据在GUI上显示它。然后,用户将插入一些选项以对文件执行其余操作并保存。问题是,我无法访问JButton1中创建的对象“Sample”,并且由于处理过程非常耗时,因此访问已完成的工作将是理想的选择。任何提示?
public class FormatFiles extends javax.swing.JFrame {
/** Creates new form FormatFiles */
public FormatFiles() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();//Inicia Caixa Dialog
FileNameExtensionFilter filter = new FileNameExtensionFilter("SNLT logs", "csv");//Restringe tipo de ficheiros
chooser.addChoosableFileFilter(filter);//Adiciona filtro á Caixa
chooser.setAcceptAllFileFilterUsed(false);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
try {
File ficheiro = chooser.getSelectedFile();
jLabel3.setText(ficheiro.getAbsolutePath());
String namefile = ficheiro.getAbsolutePath();
File openAs = new File(namefile);
FileReader in = null;
//Opens File
try {
in = new FileReader(openAs);
} catch (FileNotFoundException ex) {
Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
}
//Load file into jTextArea1
try {
jTextArea1.read(in, openAs.toString());
} catch (IOException ex) {
Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
}
//Calls Amostra
Amostra Sample = new Amostra(namefile);
String segundos = Double.toString(Sample.getsec());
jLabel7.setText(segundos);
} catch (FileNotFoundException ex) {
Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String Accao = jTextField1.getText();
JOptionPane.showMessageDialog(null,Accao);
//Can't Access Sample from here...
}
/**
* @param args the command line arguments
*/
public static void FormatFiles(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new FormatFiles().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
答案 0 :(得分:1)
将sample
的声明移到方法之外,所以整个班级都可以访问它,同时将其改为以小写字母开头:
public class FormatFiles extends javax.swing.JFrame {
....
Amostra sample;
....
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
....
sample = new Amostra(namefile);
....
}
......
}
答案 1 :(得分:0)
你不能只把它声明为实例变量吗?
为符合最佳实践,实例变量应为private
,并且其名称应符合标准Java命名约定,以小写字母开头; e.g。
private Amostra sample;