2012-06-12 62 views
0

我一直在嘗試爲此JButton編寫if else語句時遇到了麻煩。我試圖讓這個JButton的調用的計算方法
我使用的Eclipse,這是Java的無法計算如何使用Jbutton調用方法

import java.awt.EventQueue; 

import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import javax.swing.text.JTextComponent; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.io.File; 

public class DirectorySize { 

protected static final JTextComponent filelist = null; 
private JFrame frame; 
private JTextField textField; 
private JTextField sizeOutput; 
protected File file; 
protected Object path; 
protected long size; 
protected Object getContenPane; 
protected Object directory; 
private int totalFolder; 
private int totalFile; 



/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       DirectorySize window = new DirectorySize(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
*/ 
public DirectorySize() { 
    initialize(); 
} 

/** 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 553, 144); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    textField = new JTextField(); 
    textField.setBounds(10, 11, 402, 35); 
    frame.getContentPane().add(textField); 
    textField.setColumns(10); 

    //JFileChooser to open directory 
    JButton directoryButton = new JButton("Directory"); 
    directoryButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      JFileChooser chooser = new JFileChooser(); 
      chooser.setFileHidingEnabled(true); 
      chooser.setMultiSelectionEnabled(false); 
      chooser.setFileSelectionMode  
          (JFileChooser.DIRECTORIES_ONLY); 
      chooser.setDialogType(JFileChooser.OPEN_DIALOG); 
      int state = chooser.showOpenDialog(frame); 
      if (state == JFileChooser.APPROVE_OPTION) 

    textField.setText(chooser.getSelectedFile().getAbsolutePath()); 

     } 
    }); 
    directoryButton.setBounds(422, 17, 105, 23); 
    frame.getContentPane().add(directoryButton); 

    //Jbutton to calculate directory size when pressed 
    JButton sizeReader = new JButton("Calculate Size"); 
    sizeReader.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 

      String sizeReader = sizeOutput.getText(); 
      String fs = "foldersize"; 
       if(fs == sizeReader){ 

       }else{ 


      //sizeOutput.setText(sizeOutput.getText() + totalFolder); 
       } 
     } 
    }); 
    sizeReader.setBounds(10, 57, 131, 38); 
    frame.getContentPane().add(sizeReader); 

    sizeOutput = new JTextField(); 
    sizeOutput.setEditable(false); 
    sizeOutput.setBounds(151, 57, 261, 38); 
    frame.getContentPane().add(sizeOutput); 
    sizeOutput.setColumns(10); 

    // exit button for frame 
    JButton btnNewButton_2 = new JButton("Exit"); 
    btnNewButton_2.addActionListener(new ActionListener() { 
     private Object exitButton; 
     public void actionPerformed(ActionEvent arg0) { 

      System.exit(0); 

      this.add(exitButton); 
     } 

     private void add(Object exitButton2) { 

     } 
    }); 
    btnNewButton_2.setBounds(422, 65, 105, 23); 
    frame.getContentPane().add(btnNewButton_2); 

} 

    //Calculation method for the directories 
    public long getFileSize(File folder) { 
    totalFolder++; 
    System.out.println("Folder: " + folder.getName()); 
    long foldersize = 0; 

    File[] filelist = folder.listFiles(); 
    for (int i = 0; i < filelist.length; i++) { 
     if (filelist[i].isDirectory()) { 
      foldersize += getFileSize(filelist[i]); 
     } else { 
      totalFile++; 
      foldersize += filelist[i].length(); 
     } 
    } 
    return foldersize; 
} 
public int getTotalFolder() { 
    return totalFolder; 
} 
public int getTotalFile() { 
    return totalFile; 
} 
    } 

可以請人幫助我,告訴我,我需要做的 什麼我做錯了還是什麼感謝您幫助

+0

SizeOutput也是一個按鈕??? –

+0

我試過如果/ else語句並嘗試catch,但我似乎無法得到它 – PurpleVans

+0

SizeOutput是Textfield – PurpleVans

回答

1

嘗試以下操作:

String SizeReader = SizeOutput.getText().toLowerCase(); 
if(fs.toLowerCase().equals(SizeReader)) 
{ 

} 
else 
{ 

} 
+0

我得到一個異常錯誤,下面的代碼 – PurpleVans

+0

*「我得到一個異常」*我猜你有一些異常輸出。這樣我們就不必再進行猜測了,將它編輯成你的問題。 –

+0

什麼?我的代碼很好,它沒有任何編譯錯誤,只是當我添加@tunmise fasipe給我的if/else語句時,我會得到一個異常錯誤。 – PurpleVans