【问题标题】:Debugging Swing App调试 Swing 应用程序
【发布时间】:2016-03-02 03:10:19
【问题描述】:

您好,我想知道是否可以在不进入 JComponent 类内部创建 JPanel、Jbuttons 等的情况下调试 Java Swing 应用程序。我真的只关心调试我的代码,而不是研究如何创建 JPanels、JButtons 等。

我在我创建的变量的代码中设置了断点,并且想看看它们的行为如何,但是当尝试运行调试模式时,它在 main 方法中启动,就好像没有断点一样现在。

我附上了断点在我的代码中的实际位置。 where I have inserted my breakpoint

这是我尝试调试类时调试器开始的地方。 where the debugger starts

编辑 我在下面附上了我的代码。

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.在此处显示您的代码和您的问题,而不是在链接中。 2. 是的,调试 Swing 应用程序绝对不难,特别是如果您使用良好的 M-V-C(模型-视图-控制器)分离关注点,实际上这是这样做的主要原因之一。
  • 哦,我明白了,您发布的代码图像更难提供帮助,因为我们无法复制和粘贴您的代码。同样,请将代码发布为已格式化为代码的文本。你的工作是尽量让其他人更容易帮助你,这将大大有助于实现这一目标。
  • @MadProgrammer 我通常使用“step over”命令,除非我需要深入研究方法的交互工作,但在这种情况下,在调试器启动时,它会将我直接带入 JComponent 类,当我步进时在我的 gui 创建代码的所有代码中,它在 JFrame 中没有显示任何内容。
  • 可能您还设置了旧的断点,您可能想看看是否可以清除所有断点并重试

标签: java eclipse swing debugging


【解决方案1】:

如果您使用的是 Eclipse,请转到 Window/Preferences/Java/Debug/Step Filtering。为 java.awt.* 和 javax.swing.* 添加条目,并打开步骤过滤。现在调试器将在单步执行时将这些类中的方法作为单步执行。其他 IDE 应该有类似的设置。

当调试器在奇怪的地方停止时,通常是因为打开的另一个项目具有不同版本的源代码。 Eclipse 似乎无法判断哪些源代码属于当前正在调试的二进制文件。如果打开了多个引用同一库的不同版本的项目,这尤其成问题。这不太可能是您的问题,但请记住。

但有一个稍微偏离主题的问题:为什么当传递的参数为 not null 时,您有返回 true 的 isNull() 方法?当然应该将它们重命名为 isNotNull(),还是应该否定测试?

这些方法也可以是静态单行:

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

更好的是,只需在代码中包含测试并删除方法即可。

【讨论】:

  • 谢谢,实际上我在之前的代码中将它们作为测试条件,但正在观看 MIT 的开放课件课程,他们说您应该将测试放在允许模块化的方法中,并重新编写代码利用。不过,我对 Java 编程还是很陌生,所以我感谢所有的反馈。
  • 还有您提出的解决方案,调试器仍在将我带入 JComponent 类。
  • @Well 要么 javax.swing.* 不在过滤器列表中,要么过滤器未启用
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多