我一直在努力为这个JButton编写if else语句。我试图让这个Jbutton调用计算方法
我正在使用Eclipse,这是Java
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 :(得分:1)
尝试以下方法:
String SizeReader = SizeOutput.getText().toLowerCase();
if(fs.toLowerCase().equals(SizeReader))
{
}
else
{
}