2012-12-05 34 views
3

我是一名java初学者,我尝试制作一个基本程序,它将删除Windows中临时文件中的某个文件。当我没有实现JPanel & JFrame时,它确实删除了这个文件,但我没有遇到任何问题。当按下“确定删除”按钮时,应该删除文件,按“退出”按钮时退出程序。它现在所做的只是调出GUI而没有别的。甚至没有系统打印。下面是代码:JButton没有做它应该做的事

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.io.File; 
import java.io.IOException; 


    /** 
    * Created with IntelliJ IDEA. 
    * User: Andrew 
    * Date: 12/4/12 
    * Time: 7:09 PM 
    * To change this template use File | Settings | File Templates. 
    */ 

    public class DeleteFile { 

    public static void main (String args[]) throws IOException { 
     frame.setVisible(true); 
     frame.setName(boxname); 
     frame.setSize(100, 150); 
     frame.addWindowListener(new WindowAdapter() { 

      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 

     button1.setText(buttontext); 
     button1.setVisible(true); 
     button1.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

      } 
     }); 
     class Action1 implements ActionListener { 
      public void actionPerformed (ActionEvent e) { 
       deleteFile(); 
       JLabel label = new JLabel("Deletion was successful"); 
       JPanel panel = new JPanel(); 
       panel.add(label); 
      } 
     } 
     class Action2 implements ActionListener { 
      public void actionPerformed (ActionEvent e) { 

      } 
      public void windowEvent (WindowEvent e) { 
       System.exit(0); 
      } 
     } 

     JPanel panel = new JPanel(); 
     frame.add(panel); 
     JButton button = new JButton("Delete for sure?"); 
     panel.add(button); 
     button.addActionListener (new Action1()); 
     panel.setName(boxname); 

     JButton button2 = new JButton("Exit"); 
     panel.add(button2); 
     button2.addActionListener (new Action2()); 

     // JLabel label = new JLabel(filePath); 
     // panel.add(label); 




    } 






    static String buttontext = "Delete file for sure?"; 
    static String boxname = "Trepix Temp File Deleter"; 
    static String filePath = "C:\\Users\\Andrew\\AppData\\Local\\Temp\\CamRec0\\cursor-1.ico"; 
    static JFrame frame = new JFrame(); 
    static JButton button1 = new JButton(); 
    static JPanel panel = new JPanel(); 







    public static boolean fileIsValid() { 
     File file = new File(filePath); 

     if (file.exists()) { 
      return true; 


     } else { 
      return false; 
     } 

    } 

    public static void deleteFile() { 
     if (fileIsValid() == true) { 
      File file = new File(filePath); 
      file.delete(); 

     } 
    } 




} 

回答

4
class Action1 implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      deleteFile(); 
      JLabel label = new JLabel("Deletion was successful"); 
      JPanel panel = new JPanel(); 
      panel.add(label); 
     } 
    } 

面板对象永远不会放置在是导致顶层窗口层次结构的一部分的任何容器。换句话说,它不会被放置在JFrame或JDialog中的任何东西中,因此它将永远不会显示。

class Action2 implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 

     } 
     public void windowEvent (WindowEvent e) { 
      System.exit(0); 
     } 
    } 

这是没有意义的地方在一个ActionListener这个windowEvent方法,因为这是一个WindowListener的的一部分,是完全不同的。为什么不简单地在actionPerformed(...)方法中调用System.exit(0);

此外,您的代码不应该有任何静态字段或方法,因为这是与面向对象编程的对立面。

+0

+1打我吧... – MadProgrammer

+0

@MadProgrammer:对不起 - 你正在忙着编辑OP。但知道你的智慧,你可能会发布更好的东西。 –

+0

你的答案几乎是一字一句我正在打字8P – MadProgrammer

相关问题