【发布时间】: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