JButton不工作

时间:2012-08-06 07:12:09

标签: java linux swing jbutton jfilechooser

我最近为我的应用程序开发了一个GUI,它在Linux Ubuntu 10.04上运行shell脚本。它一直工作到今天。我设计了一个按钮,单击该按钮将打开选择文件的提示。 JFileChooser用于操作。但是从今天早上开始,没有任何错误,点击按钮不会做任何事情。它不仅仅是突然工作。这个错误是什么? 有人可以帮忙吗?这是否与安装的OS / Java有关?

提前致谢。

int yorn = JOptionPane.YES_OPTION;
String user = System.getProperty("user.home");
File file = new File(user+"/.afile");
if (file.exists()){
    yorn = JOptionPane.showConfirmDialog(this,"There seem to have a Previous project , Do you want to back up??");

    if (yorn == JOptionPane.YES_OPTION){ // yesoption clicked!

        //choose folder to open!
        JFileChooser jf = new JFileChooser();
        jf.setDialogTitle("Back up location?");
        String pathname = null;
        jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jf.showOpenDialog(null);
        File f = jf.getSelectedFile();
        pathname = f.getAbsolutePath();
        // JOptionPane.showMessageDialog(this,pathname);
        try{
            Process copy = Runtime.getRuntime().exec("sh "+user+"/myprojects/.backup.sh "+pathname);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(this, e);
        }

        //open file for analysing..
        JOptionPane.showMessageDialog(this,"Backing up complete.. ");

        JFileChooser jf2 = new JFileChooser();
        jf2.setDialogTitle("Open the Codebase");
        String pathname2 = null;
        jf2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jf2.showOpenDialog(null);
        File f2 = jf2.getSelectedFile();
        pathname2 = f2.getAbsolutePath();
        String username = System.getProperty("user.home");
        File writefile = new File(username+"/.afile");              

        Writer output = null;
        try {
            output = new BufferedWriter(new FileWriter(writefile));
            output.write(pathname2);
            output.close();

            //    JOptionPane.showMessageDialog(this,username);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else if (yorn == JOptionPane.NO_OPTION){       

        JFileChooser jf = new JFileChooser();
        jf.setDialogTitle("Open the Codebase");
        String pathname = null;
        jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jf.showOpenDialog(null);
        File f = jf.getSelectedFile();          
        pathname = f.getAbsolutePath();
        String username = System.getProperty("user.home");
        File writefile = new File(username+"/.afile");              
        Writer output = null;
        try {
            output = new BufferedWriter(new FileWriter(writefile));
            output.write(pathname);
            output.close();

            //    JOptionPane.showMessageDialog(this,username);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
} 

这是代码..这是什么,是,它检查是否存在文件,如果存在,则提示备份。如果备份为是,则备份并打开另一个选择的新窗口,如果备份为否,则打开文件选择。正如我所提到的,这一直在我问这个问题的时候。它突然停止了工作。感谢所有的答复。

1 个答案:

答案 0 :(得分:2)

你应该按照之前的要求提供一个例子,但这里有一段适合我的代码,在linux / MAC OS / Windows下测试:

/* chooser is of type JFileChooser of course */
chooser.setDialogTitle("title of your dialog");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY /* change it to fit your needs */);
chooser.setAcceptAllFileFilterUsed(false);
chooser.showOpenDialog(null);