0

我的作业涉及编写 GUI。我不是在找人为我做作业,但我只是想帮助更好地理解它。我们将编写以下类: 因为我们需要单独的类和单独的驱动程序来测试这些类,所以我将分工。根据需要使用 MATH 类。一定要使用 float 或 double 原始数据类型。额外的学生将有一个默认构造函数(将默认值设置为 2.0f 或 2.0 对于双精度数),以及接受浮点数或双精度数据的重载方法。

此外,学生将使用图形用户界面编程技术作为课堂界面。学生可以使用标签、按钮、单选按钮、菜单以及滑块。

正方形类(周长和面积)

主要是我需要帮助的:

构造函数应该去哪里?我将如何实现它们以在此代码中重载?如何在这段代码中重载方法?

    import javax.swing.*;
    import java.awt.event.*; // Needed for ActionListener Interface

    /**
     * The BugayTestSquare 3 class displays a JFrame that lets the user enter in the
     * sides of the of a square. When the calculate button is pressed, a dialog box
     * will be displayed with the area and perimeter shown. 
     */

    public class FirstGUI extends JFrame
    {
        /*
         * Acording to good class design princples, the fields are private.
         */
        private JPanel panel;
        private JLabel messageLabel, messageLabel2;
        private JTextField lengthTextField;
        private JButton calcButton;
        private final int WINDOW_WIDTH = 310;
        private final int WINDOW_HEIGHT = 150;

        /**
         * Constructor
     */

    public FirstGUI()
    {
        // Set the window title
        setTitle("Area and Perimiter Calculator");

        // Set the size of the window.
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        // Specify what happens when the close button is clicked.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Build the panel and add it to the frame.
        buildPanel();

        // Add the panel to the frames content pane.
        add(panel);

        //diplay the window.
        setVisible(true);
    }

    /** The buildPanel method adds a label, 
     * text field, and a button to a panel.
     **/

    private void buildPanel()
    {
        // Create a label to display instructions.
        messageLabel = new JLabel("Please enter in the length " +
                                  "of the square.");

        messageLabel2 = new JLabel("Please enter in the width " +
                                 "of the square.");

        // Create a text field 10 characters wide.
        lengthTextField = new JTextField(10);

        //Create a button with the caption "Calculate."
        calcButton = new JButton("Calculate");

        // Add an action listener to the button.

        calcButton.addActionListener(new FirstGUI.CalcButtonListener());

        //Create a JPanel object and let the
        // panel field reference it.
        panel =new JPanel();

        // Add the label, text field, and button.
        // components to the panel.

        panel.add(messageLabel);
        panel.add(messageLabel2);
        panel.add(lengthTextField);
        panel.add(calcButton);
    }

    /**
     * CalcButtonListener is an action listener 
     * class for the Calculate button.
     */

    private class CalcButtonListener implements ActionListener
    {
        /**
         * The actionPerformed method executes when the user
         * clicks on the Calculate Button.
         * @param e The Event Object.
         */

        public void actionPerformed(ActionEvent e)
        {
            String input; // To hold the user's input
            double area; // The area
            double perimeter; // the perimter

            // Get the text entered by the user into the
            // text field

            input = lengthTextField.getText();

            //Perform Calculations
            area = Double.parseDouble(input)*2; 
            perimeter = Double.parseDouble(input)*4;

            //Display the result.

            JOptionPane.showMessageDialog(null, "Your area is " +area +
                "\nYour perimter is " + perimeter);

        }
    }
}
4

1 回答 1

0

这应该有助于:

public class FirstGUI extends JFrame
    {

    public FirstGUI {

        // Constructers go here

     }

}

至于超载。像这样想象,你有一张借记卡,它可以用作借记卡,也可以用作信用卡。它有两个用途。您可以创建两个具有相同名称但含义不同的方法。

例子:

public void functionOne(int num1, int num1) {

// do some stuff

}

public void functionOne(int num1, int num1, String str) {

// do some stuff but has different input

}

public void functionOne(int num1) {

// do some stuff but has different input

}

您可以通过以下方式调用该方法:

functionOne(1, 2);

functionOne(1, 2, "hello world");

public void functionOne(59);

并且所有调用都将针对具有相同名称但输入不同的方法。所有的电话都是正确的。您现在可以重载调用 functionOne。

于 2013-10-18T00:45:58.443 回答