2016-03-02 119 views
2

你好,我想知道是否有可能調試一個Java Swing應用程序,而無需進入JComponent類來創建JPanel,Jbuttons等。我真的只關心調試我的代碼,而不關注如何創建JPanel,JButton等。調試Swing應用程序

我已經設置斷點在我的代碼上,我已經創建,並希望變量來看看它們是如何表現,而是試圖運行它開始在main方法調試模式,如果有一個破發點時,一個是當不存在。

我已附上斷點實際位於我的代碼中的位置。 where I have inserted my breakpoint

enter image description here

,這是在調試器啓動時,我嘗試調試類。 where the debugger starts

enter image description here

編輯 我在下面附上我的代碼。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Image; 
import java.awt.event.*; 
import java.io.File; 

import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 
import javax.swing.table.DefaultTableModel; 

public class main_frame extends JFrame { 

    private static final long serialVersionUID = 1L; 
    // Panels 
    private JPanel contentPane = new JPanel(); 
    private JPanel inPanel = new JPanel(); 
    private JPanel outPanel = new JPanel(); 

    // Labels 
    private JLabel lblFile; 
    private final JLabel lblInformation = new JLabel(
      "<html>Please select a Directory or File that you would like to keep an eye on.Once you have selected your directory or file, hit the submit button to start and set your alert time.</html> "); 

    // textFields 
    private JTextField file_path = new JTextField(); 
    private JTextField txtAlerttime; 
    // tables 
    private JTable filesAndTimes = new JTable(); 

    // JScrollPane 
    private JScrollPane scroll; 

    // Buttons 
    private JButton btnCancel = new JButton(); 
    private JButton btnOpen = new JButton(); 
    private JButton btnSubmit = new JButton(); 

    // Other Variables 
    File file = null; 
    String[] fileNames = null; 
    String[] fileTimes = null; 
    String[] filePaths = null; 
    int submit_count = 0; 
    int openEventClickCount = 0; 

    /** 
    * Create the main frame. 
    */ 
    public main_frame() { 
     gui(); 
    } 

    /* 
    * @param The gui that is going to create the main frame for our 
    * application. 
    */ 
    public void gui() { 
     setTitle("File Watcher 2000"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 863, 480); 
     setLocationRelativeTo(null); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     inPanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true)); 
     inPanel.setBounds(217, 12, 634, 163); 
     inPanel.setLayout(null); 
     contentPane.add(inPanel); 

     lblFile = new JLabel("Select File or Directory :"); 
     lblFile.setBounds(12, 56, 177, 17); 
     inPanel.add(lblFile); 

     file_path.setHorizontalAlignment(SwingConstants.LEADING); 
     file_path.setBounds(207, 54, 243, 19); 
     file_path.setColumns(20); 
     file_path.setEditable(false); 
     inPanel.add(file_path); 
     lblInformation.setVerticalAlignment(SwingConstants.TOP); 
     lblInformation.setBounds(12, 12, 610, 30); 

     inPanel.add(lblInformation); 

     JLabel lblAlertMeWhen = new JLabel("Alert me when my File or Directory changes in :"); 
     lblAlertMeWhen.setBounds(12, 85, 339, 15); 
     inPanel.add(lblAlertMeWhen); 

     txtAlerttime = new JTextField(); 
     txtAlerttime.setHorizontalAlignment(SwingConstants.CENTER); 
     txtAlerttime.setText("120"); 
     txtAlerttime.setBounds(357, 85, 44, 15); 
     inPanel.add(txtAlerttime); 
     txtAlerttime.setColumns(5); 

     JLabel lblMins = new JLabel("mins"); 
     lblMins.setBounds(407, 85, 44, 15); 
     inPanel.add(lblMins); 

     // Cancel Event 

     btnCancel = new JButton("Cancel"); 
     btnCancel.setBounds(12, 127, 81, 25); 
     inPanel.add(btnCancel); 

     // Open Event 

     btnOpen = new JButton("Open"); 
     btnOpen.setBounds(451, 127, 72, 25); 
     inPanel.add(btnOpen); 

     // Submit Event 

     btnSubmit = new JButton("Submit"); 
     btnSubmit.setBounds(539, 127, 83, 25); 
     inPanel.add(btnSubmit); 
     btnSubmit.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       mf_submit_Event submitEvent = new mf_submit_Event(file); 

       submitEvent.actionPerformed(e); 

       fileNames = submitEvent.getFileNames(); 
       System.out.println("File Times has " + fileNames.length + " files."); 

       fileTimes = submitEvent.getFileTimes(); 
       System.out.println("File Times has " + fileTimes.length + " files."); 

       filePaths = submitEvent.getFilePaths(); 
       System.out.println("File Paths has " + filePaths.length + " files."); 

       filesAndTimes = submitEvent.getTable(); 
       System.out.println("Table Retrieved from Submit Event Successfully"); 

       scroll = new JScrollPane(filesAndTimes); 
       scroll.setBounds(12, 12, 550, 177); 
       filesAndTimes.setFillsViewportHeight(true); 

       // The mouseHandler Class handles custom events for the JTable 
       // through the submit event. 
       filesAndTimes.addMouseListener(new submitMouseEventHandler(filesAndTimes)); 
       submit_count++; 

       outPanel.add(scroll, BorderLayout.CENTER); 
       outPanel.revalidate(); 
      } 
     }); 

     // ****************** OPEN EVENT ****************************** 

     btnOpen.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       mf_open_Event openEvent = new mf_open_Event(); 
       // call ActionEvent 
       openEvent.actionPerformed(e); 

       // Setting the file path that was chosen by the user. 
       file = openEvent.getFile(); 
       openEventClickCount++; 
       System.out.println("Open event click count is " + openEventClickCount); 
       /* 
       * Setting the text field to be the file chosen by the user, if 
       * no file is chosen, the file path is set to the default root 
       * directory in the users computer. 
       */ 

       if (isNull(file)) { 
        file_path.setText(file.getAbsolutePath()); 
       } else { 
        file_path.setText(File.listRoots()[0].getAbsolutePath()); 
       } 
      } 
     }); 

     // ****************** CANCEL EVENT ****************************** 
     btnCancel.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       int userChoice = JOptionPane.showConfirmDialog(null, 
         "Are you sure that you want to Exit the Program", "Close Program", 
         JOptionPane.YES_NO_OPTION); 

       if (userChoice == 0) { 
        System.exit(0); 
       } else { 

        int userClearTable = JOptionPane.showConfirmDialog(null, 
          "Would you like to clear the data in the Table?", "Clear Table Data", 
          JOptionPane.YES_NO_OPTION); 
        if (userClearTable == 0) { 
         DefaultTableModel dtm = (DefaultTableModel) filesAndTimes.getModel(); 
         dtm.setColumnCount(0); 
         dtm.setRowCount(0); 

         filesAndTimes.setModel(dtm); 

        } 
       } 

      } 
     }); 
     outPanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true)); 
     outPanel.setBounds(12, 187, 839, 281); 
     contentPane.add(outPanel); 
     outPanel.setLayout(new BorderLayout()); 

     JLabel imgLabel = new JLabel(""); 
     imgLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     imgLabel.setVerticalAlignment(SwingConstants.TOP); 
     Image img = new ImageIcon(this.getClass().getResource("/file_icon.png")).getImage(); 
     imgLabel.setIcon(new ImageIcon(img)); 
     imgLabel.setBounds(12, 12, 159, 135); 
     contentPane.add(imgLabel); 

    }// END OF THE GUI 

    /* 
    * ************************************* METHOD HOUSING BELOW 
    * ******************************************* 
    */ 

    /** 
    * @param checking 
    *   that the file parameter that is passed in is not null. 
    * @returns true if the parameter is null. 
    */ 
    public boolean isNull(File file) { 
     boolean answer = false; 
     if (file != null) { 
      answer = true; 
     } 
     return answer; 
    } 

    /** 
    * @param checking 
    *   that the string array parameter that is passed in is not null. 
    * @returns true if the parameter is null. 
    */ 
    public boolean isNull(String[] strArray) { 
     boolean answer = false; 
     if (strArray != null) { 
      answer = true; 
     } 
     return answer; 
    } 

    /** 
    * @param checking 
    *   that the String parameter that is passed in is not null. 
    * @returns true if the parameter is null. 
    */ 
    public boolean isNull(String str) { 
     boolean answer = false; 
     if (str != null) { 
      answer = true; 
     } 
     return answer; 
    } 

    /** 
    * @param getting 
    *   the integer alert time that is passed by the user. 
    * @return alert time in minutes 
    */ 
    public int getAlertTime() { 
     return Integer.parseInt(txtAlerttime.getText()); 
    } 

    /** 
    * @param getting 
    *   the file or directory path that was selected by the user. 
    * @return file path provided by the user. 
    */ 
    public String getFilePath() { 
     return file.getName(); 
    } 

} 
+1

查找「step over」命令 – MadProgrammer

+0

1.在此處顯示您的代碼,而不是鏈接。 2.是的,當然,調試Swing應用程序並不困難,特別是如果您使用良好的M-V-C(模型 - 視圖 - 控制器)分離問題,實際上這是實現此目的的主要原因之一。 –

+1

哦,我明白了,您發佈的代碼圖像更難以提供幫助,因爲我們無法複製和粘貼代碼。再次請將代碼發佈爲已被格式化爲代碼的文本。你的工作是儘可能讓別人儘可能簡單地幫助你,而這將會朝着這個目標邁進很長一段路。 –

回答

0

如果您使用的是Eclipse,請轉到Window/Preferences/Java/Debug/Step Filtering。爲java.awt。*和javax.swing。*添加條目,並打開步驟過濾。現在,當單步執行時,調試器將執行這些類中的方法。其他IDE應該有類似的設置。

當調試器在奇怪的地方停止時,通常是因爲打開了另一個具有不同版本源代碼的項目。 Eclipse似乎無法告訴哪些源代碼屬於當前正在調試的二進制文件。如果有多個項目打開引用同一個庫的不同版本,這是一個特別的問題。這不可能是你的問題,但要牢記在心。

但是,一個稍微偏離主題的問題:當傳遞的參數是而不是 null時,爲什麼有isNull()方法返回true?當然,他們應該改名爲isNotNull(),否則測試應該被否定?

而且這些方法可以是靜態的俏皮話:

public static boolean isNotNull(File file) { 
    return file != null; 
} 

更妙的是,剛剛在您的測試代碼並刪除方法。

+0

謝謝,實際上我已經將它們作爲代碼的測試條件,但是正在觀看麻省理工學院開放課件產品課程他們表示應該將測試放在允許模塊化的方法中,以及代碼重用中。不過,對於使用Java編程,我仍然很新,所以我很欣賞所有的反饋。 – TTB53

+0

也與你提出的調試器的解決方案仍然把我帶入JComponent類。 – TTB53

+0

@或者javax.swing。*不在過濾器列表中,或者未啓用過濾 – kiwiron