2012-12-05 60 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

相關問題