2013-10-17 35 views
0

我的作業包括編寫一個GUI。我不是在找人爲我做功課,但我只是想幫助我更好地理解它。我們將編寫以下類:由於我們需要單獨的類和單獨的驅動程序來測試這些類,因此我將劃分這些工作。根據需要使用MATH類。一定要使用float或double基本數據類型。其他學生需要有一個默認構造函數(爲double設置默認值爲2.0f或2.0),以及重載的方法來接受浮點數據或雙精度數據。重載Java中的構造函數和方法

此外,學生將使用圖形用戶界面編程技術作爲類的接口。學生可以使用標籤,按鈕,單選按鈕,菜單以及滑塊。

廣場類(周長和麪積)

主要是我需要什麼幫助:

應該在哪裏構造走?我將如何實現它們在此代碼中被重載?我如何在這段代碼中重載方法?

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); 

     } 
    } 
} 
+0

你在問什麼是超載? –

回答

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重載。

+0

看了這9個月後,這是一個基本的問題,你解釋得很好。現在我再次閱讀它是非常有意義的。 – FooBar