2016-09-11 5 views
-1

私はGUI Javaプログラムを作成する方法を学びました。私はプロセスを学ぶために本を守ってきましたが、手紙で本をコピーしていても(私の知る限り)、私はいくつかの問題に取り組んできました。ここで私のフルコードを開始します。私のJavaプログラムから多くのエラーを受け取りました

package healthprofilegui; 

//packages to be imported for the GUI 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 


public class HealthProfileGUI extends JFrame implements ActionListener { 

    // text field variables used to get user input. 
    JTextField txtName = new JTextField(15); 
    JTextField txtAge = new JTextField(15); 
    JTextField txtWeight = new JTextField(15); 
    JTextField txtHeight_feet = new JTextField(15); 
    JTextField txtHeight_inches = new JTextField(15); 

    //text field variables used to display calculations. 
    JTextField txtBMI = new JTextField(15); 
    JTextField txtCategory = new JTextField(15); 
    JTextField txtMaxHeartRate = new JTextField(15); 

    //buttons to be used for the program. 
    JButton btnDisplay = new JButton("Display"); 
    JButton btnClear = new JButton("Clear"); 

    Integer heightTotal = null; 

    private HealthProfile myProfile; 

    public profileGUI() 
    { 
     super("HealthProfile"); 
     myProfile = new HealthProfile(); 
     setLayout(new GridLayout(0,2)); 

     //adds the text field labels to the program. 
     add(new JLabel("txtName")); 
     add(txtName); 
     add(new JLabel("txtAge")); 
     add(txtAge); 
     add(new JLabel("txtWeight")); 
     add(txtWeight); 
     add(new JLabel("txtHeight_feet")); 
     add(txtHeight_feet); 
     add(new JLabel("txtHeight_inches")); 
     add(txtHeight_inches); 


     //adds the buttons to the program. 
     add(btnDisplay); 
     add(btnClear); 

     //adds labels for the output fields 
     add(new JLabel("txtBMI")); 
     add(txtBMI); 
     add(new JLabel("txtCategory")); 
     add(txtCategory); 
     add(new JLabel("txtMaxHeartRate")); 
     add(txtMaxHeartRate); 

     setVisible(true); 
     btnDisplay.addActionListener(this); 
     btnClear.addActionListener(this); 
    } 



    @Override 
    public void action(ActionEvent e) 
    { 
     //clear text if the button is pressed. 
     if(e.getSource() == btnClear) 
     { 
      System.out.println("Clear Pressed"); 
      txtName.setText(""); 
      txtAge.setText(""); 
      txtWeight.setText(""); 
      txtHeight_feet.setText(""); 
      txtHeight_inches.setText(""); 
      txtBMI.setText(""); 
      txtCategory.setText(""); 
      txtMaxHeartRate.setText(""); 

     } 

     //process data if pressed. 
     if(e.getSource() == btnDisplay) 
     { 
      //checks for missing input 
      if (txtName.getText().isEmpty() || txtAge.getText().isEmpty() || txtWeight.getText().isEmpty() || txtHeight_feet.getText().isEmpty() || 
        txtHeight_inches.getText().isEmpty()) 
      { 
       JOptionPane.showMessageDialog(null, "Please provide all input."); 
       return; 
      } 

      myProfile.setName(txtName.getText()); 



      try 
      { 
       myProfile.setAge(Integer.parseInt(txtAge.getText())); 
       myProfile.setWeight(Integer.parseInt(txtWeight.getText())); 

      } 
      catch (NumberFormatException ex) 
      { 
       JOptionPane.showMessageDialog(null, "Please enter a numeric value"); 
       return; 
      } 




     } 
    } 

    public static void main(String[] args) { 

    } 

} 

私は

public class HealthProfileGUI extends JFrame implements ActionListener 

HealthProfileGUIあるクラスに受けています最初のエラーは、抽象的ではなく、ActionListenerに抽象メソッドactionPerformed(ActionEvent)を上書きしません。

このエラーが表示される理由は完全にはわかりません。

二/第三は、GUI

public profileGUI() 

のレイアウトを設定するために私が使用しようとしている方法のためである私にエラー

invalid method declaration; return type required 

を与えている私は考えていますなぜこれが命令として出現したとしても、その中に「リターン」を使用したことはありません。

は私も

super("HealthProfile"); 

のために、「コンストラクタの最初のステートメントでなければなりませんスーパーに呼んで」このエラーを取得しています、私は「可能な限り、このエラーにできるだけ多くの情報を見つけることを試みたが、couldnています私の問題を同様の程度に複製したものは全く見つかりません。

私は問題を抱えている一番最後は

@Override 
public void action(ActionEvent e) 

方法は、スーパータイプ

からメソッドをオーバーライドまたは実装していない私はあまりがあることは申し訳ないですが、私は次のように保持私がすべてのコードを配置し終えた後、エラーが自分自身を並べ替えるという前提に基づいてできる限り最善の方法を説明します。しかしそれほど大したことではありません。

私は大いに助けていただきありがとうございます。特にこれは私のために非常に少数であったので。

+0

HealthProfileクラスがありません。 – blackpen

+0

JavaScriptは* Java *ではない* Java –

+0

私はJavaScriptについて何も言わなかった。私はJavaスクリプトを言った。これは2つの別々の単語です。しかし、それが言葉にされる方法は、分かりやすく混乱させることができます。 –

答えて

1

いくつかの注意事項あなたが始めるために:

//HealthProfile is missing 
private HealthProfile myProfile; 

//the name of the constructor needs to be the same as the class name 
public HealthProfileGUI() 

@Override 
//you should override actionPerformed not action 
//public void action(ActionEvent e) 
public void actionPerformed(ActionEvent e) 

一般的なアドバイス:
あなたは多くのエラーや、「大きな問題」を持っている場合は小さなものにそれを破ります。たとえば、コードを減らして1つで、解決し、次のコードに移動します。より良い練習は、すべてのステップにエラーがないことを確認しながら、ステップバイステップで構築することです。
今後の質問については、MCVEをお送りください。

2

最初のエラーと4番目のエラーでは、actionPerformedメソッドは実装しているActionListenerインターフェイスの一部です。あなたのメソッドはactionと呼ばれ、そのインターフェイスにはありません。

2番目と3番目のエラーでは、コンストラクターの名前がクラスと同じである必要があります。それは他のものであることに注意してください。

関連する問題