你好,我想知道是否有可能調試一個Java Swing應用程序,而無需進入JComponent類來創建JPanel,Jbuttons等。我真的只關心調試我的代碼,而不關注如何創建JPanel,JButton等。調試Swing應用程序
我已經設置斷點在我的代碼上,我已經創建,並希望變量來看看它們是如何表現,而是試圖運行它開始在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();
}
}
查找「step over」命令 – MadProgrammer
1.在此處顯示您的代碼,而不是鏈接。 2.是的,當然,調試Swing應用程序並不困難,特別是如果您使用良好的M-V-C(模型 - 視圖 - 控制器)分離問題,實際上這是實現此目的的主要原因之一。 –
哦,我明白了,您發佈的代碼圖像更難以提供幫助,因爲我們無法複製和粘貼代碼。再次請將代碼發佈爲已被格式化爲代碼的文本。你的工作是儘可能讓別人儘可能簡單地幫助你,而這將會朝着這個目標邁進很長一段路。 –