所以問題是這樣的。我有一個GUI打開一個JFileChooser,用戶選擇一個文件,當命中時,一個新的類被調用(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
}
thx,那的確很簡單。 – Jay 2011-05-10 11:16:46