public class checkGpa extends JFrame implements ActionListener { static JButton backButton = new JButton("back"); JFrame frame = new JFrame("GPA"); Label averageGPALabel = new Label("average GPA"); JTextField averageGPAField = new JTextField(); Label weightedGPALabel = new Label("weighted GPA"); JTextField weightedGPAField = new JTextField(); /** * Constructor creates a JFrame and sets its properties. * Calls the PersonalReader method to read and display student GPA information. * Adds a JPanel to the JFrame and creates and positions various GUI elements. * Sets an ActionListener for the back button to return to the previous window. * * @throws IOException if an error occurs while reading from the JSON file */ public checkGpa() throws IOException { frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(400, 300); // Create and set properties for JPanel JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // Create and position GUI elements averageGPALabel.setBounds(10, 10, 20, 10); averageGPALabel.setBounds(20, 10, 100, 10); averageGPAField.setBounds(120, 10, 100, 10); weightedGPALabel.setBounds(10, 40, 100, 10); weightedGPAField.setBounds(120, 40, 100, 10); backButton.setAlignmentX(Component.CENTER_ALIGNMENT); // Display GPA information by calling PersonalReader method GPA gpa = PersonalReader(); averageGPAField.setText(gpa.getAverageGPA()); weightedGPAField.setText(gpa.getWeightedGPA()); panel.add(averageGPALabel); panel.add(averageGPAField); panel.add(weightedGPALabel); panel.add(weightedGPAField); panel.add(backButton); backButton.addActionListener(this); frame.add(panel); frame.setVisible(true); }使用tdd改写
时间: 2024-01-06 11:03:14 浏览: 135
首先,我们需要确定该类的功能和输入输出:
- 功能:显示学生的平均绩点和加权绩点,并提供返回上一窗口的按钮。
- 输入:无。
- 输出:学生的平均绩点和加权绩点,以及返回上一窗口的按钮。
接下来,我们可以使用 TDD 的思路来分步完成该类的改写:
1. 编写测试用例,测试 checkGpa 类的构造函数是否能够正确创建 JFrame 和显示学生绩点信息。
```java
@Test
public void testConstructor() throws IOException {
checkGpa gpa = new checkGpa();
JFrame frame = gpa.getFrame();
assertTrue(frame.isVisible()); // JFrame should be visible
assertEquals("GPA", frame.getTitle()); // JFrame title should be "GPA"
// Check if average GPA and weighted GPA fields are displayed correctly
JTextField averageGPAField = gpa.getAverageGPAField();
JTextField weightedGPAField = gpa.getWeightedGPAField();
assertEquals("", averageGPAField.getText()); // Average GPA field should be empty
assertEquals("", weightedGPAField.getText()); // Weighted GPA field should be empty
}
```
2. 根据测试用例编写 checkGpa 类的构造函数。
```java
public checkGpa() throws IOException {
// Create and set properties for JFrame
JFrame frame = new JFrame("GPA");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400, 300);
// Create and set properties for JPanel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Create and position GUI elements
JLabel averageGPALabel = new JLabel("Average GPA");
JTextField averageGPAField = new JTextField();
JLabel weightedGPALabel = new JLabel("Weighted GPA");
JTextField weightedGPAField = new JTextField();
JButton backButton = new JButton("Back");
backButton.setAlignmentX(Component.CENTER_ALIGNMENT);
// Display GPA information by calling PersonalReader method
GPA gpa = PersonalReader();
averageGPAField.setText(gpa.getAverageGPA());
weightedGPAField.setText(gpa.getWeightedGPA());
// Add GUI elements to JPanel and add ActionListener to back button
panel.add(averageGPALabel);
panel.add(averageGPAField);
panel.add(weightedGPALabel);
panel.add(weightedGPAField);
panel.add(backButton);
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose(); // Close JFrame when back button is clicked
}
});
// Add JPanel to JFrame and make it visible
frame.add(panel);
frame.setVisible(true);
// Store JFrame and GUI elements for testing
this.frame = frame;
this.averageGPAField = averageGPAField;
this.weightedGPAField = weightedGPAField;
}
```
3. 编写测试用例,测试 checkGpa 类的 ActionListener 是否能够正确响应返回上一窗口的按钮。
```java
@Test
public void testBackButton() throws IOException {
checkGpa gpa = new checkGpa();
JFrame frame = gpa.getFrame();
JButton backButton = gpa.getBackButton();
assertTrue(backButton.getActionListeners().length > 0); // ActionListener should be added to back button
backButton.doClick(); // Simulate clicking back button
assertFalse(frame.isVisible()); // JFrame should be closed
}
```
4. 根据测试用例编写 checkGpa 类的 ActionListener。
```java
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose(); // Close JFrame when back button is clicked
}
});
```
最后,我们需要将 checkGpa 类中的 GUI 元素(例如 JTextField 和 JButton)声明为类的私有成员变量,并提供对应的 getter 方法,以便测试用例可以访问它们。
```java
public class checkGpa extends JFrame implements ActionListener {
private JFrame frame;
private JTextField averageGPAField;
private JTextField weightedGPAField;
private JButton backButton;
public checkGpa() throws IOException {
// Constructor code
}
public JFrame getFrame() {
return frame;
}
public JTextField getAverageGPAField() {
return averageGPAField;
}
public JTextField getWeightedGPAField() {
return weightedGPAField;
}
public JButton getBackButton() {
return backButton;
}
}
```
阅读全文