2015-11-01 54 views
0

我正在用Java創建一個簡單的GPA計算器。它使用JOptionPane,但我希望用戶能夠在一個窗口中回答所有問題。所以,我正在試驗JPanel。難以使JButton工作

當用戶點擊「提交」按鈕時,我希望程序計算出GPA。我無法確定如何合併事件處理程序。

下面是代碼:

import java.math.RoundingMode; 
import java.text.DecimalFormat; format. 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.event.*; 


public class MyGpaCalc extends JFrame { 

public static void createGuiWindow() { 


JFrame frame = new JFrame("GPA Calculator"); 
frame.setSize(300,250); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


JPanel panel = new JPanel(); 

JLabel aGradesLabel = new JLabel("# credits earned with A grade: "); 
JTextField aGradesText = new JTextField(3); 
JLabel bGradesLabel = new JLabel("# credits earned with B grade: "); 
JTextField bGradesText = new JTextField(3); 
JLabel cGradesLabel = new JLabel("# credits earned with C grade: "); 
JTextField cGradesText = new JTextField(3); 
JLabel dGradesLabel = new JLabel("# credits earned with D grade: ");  
JTextField dGradesText = new JTextField(3); 
JLabel fGradesLabel = new JLabel("# credits earned with F grade: "); 
JTextField fGradesText = new JTextField(3); 

JButton submit = new JButton("Submit"); 

panel.add(aGradesLabel); 
panel.add(aGradesText); 
panel.add(bGradesLabel); 
panel.add(bGradesText); 
panel.add(cGradesLabel); 
panel.add(cGradesText); 
panel.add(dGradesLabel); 
panel.add(dGradesText); 
panel.add(fGradesLabel); 
panel.add(fGradesText); 

frame.getContentPane().add(panel); 
frame.setVisible(true); 

} 

public static void main(String[] args) { 

createGuiWindow(); 


DecimalFormat df = new DecimalFormat("0.00"); 
df.setRoundingMode(RoundingMode.FLOOR); 


double aGrades = Double.parseDouble(aGradesText.getText()); 
double bGrades = Double.parseDouble(bGradesText.getText()); 
double cGrades = Double.parseDouble(cGradesText.getText()); 
double dGrades = Double.parseDouble(dGradesText.getText()); 
double fGrades = Double.parseDouble(fGradesText.getText()); 


double totalCredits = aGrades + bGrades + cGrades + dGrades + fGrades; 

double gradePoints = (aGrades * 4) + (bGrades * 3) + (cGrades * 2) + (dGrades * 1) + (fGrades * 0); 

double sum = gradePoints/totalCredits; 




System.exit(0); 
} 


} 

回答

0

這是很基本的AWT/Swing編程,所以在這個問題上的任何教程將幫助。

爲了讓你開始:你需要在某個類中實現ava.awt.event.ActionListener,將該接口的actionPerfomed方法添加到類中,並用該按鈕註冊偵聽器。

在您的例子這將是最簡單的,只是有MyGpaCalc實現ActionListener:

public class Test extends JFrame implements ActionListener 

然後添加處理方法:

@Override 
public void actionPerformed(ActionEvent e) { 
    // do the calculation work here and update whatever component displays the result ... 
} 

您還需要與註冊處理您提交按鈕。如果您使用MyGpaCalcActionListener,這是你需要初始化之後添加什麼submitJButton

submit.addActionListener(this); 
+0

謝謝。如果有人知道更多關於我的特定代碼的建議,那麼理解教程就容易多了。當我有機會再次處理這個問題時,我會公佈結果! – JennyJ

+0

如果答案可以解決您的問題,請將其標記爲已接受。 – Monz

+0

我很高興做到這一點,但我不明白...請指教! – JennyJ

0

這是我結束了。我相信這並不完美,但它會以截短的格式返回正確的答案。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.DecimalFormat; 
import java.math.RoundingMode; 


public abstract class GpaCalc extends JFrame implements ActionListener { 


public static void main(String[] args) { 

    JFrame frame = new JFrame("GPA Calculator"); 
    frame.setVisible(true); 
    frame.setSize(300,250); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    frame.add(panel); 

    JLabel aGrades = new JLabel("# Credits earned with A grade:"); 
     JTextField a_Grades = new JTextField(3); 
    JLabel bGrades = new JLabel("# Credits earned with B grade:"); 
     JTextField b_Grades = new JTextField(3);  
    JLabel cGrades = new JLabel("# Credits earned with C grade:"); 
     JTextField c_Grades = new JTextField(3); 
    JLabel dGrades = new JLabel("# Credits earned with D grade:"); 
     JTextField d_Grades = new JTextField(3); 
    JLabel fGrades = new JLabel("# Credits earned with F grade:"); 
     JTextField f_Grades = new JTextField(3); 

    panel.add(aGrades); 
    panel.add(a_Grades); 
    panel.add(bGrades); 
    panel.add(b_Grades); 
    panel.add(cGrades); 
    panel.add(c_Grades); 
    panel.add(dGrades); 
    panel.add(d_Grades); 
    panel.add(fGrades); 
    panel.add(f_Grades); 

    JButton submit = new JButton("Submit"); 
    panel.add(submit); 
    submit.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      String gradesOfA = a_Grades.getText(); 
      double aGrade = Double.parseDouble(gradesOfA); 
      String gradesOfB = b_Grades.getText(); 
      double bGrade = Double.parseDouble(gradesOfB); 
      String gradesOfC = c_Grades.getText(); 
      double cGrade = Double.parseDouble(gradesOfC); 
      String gradesOfD = d_Grades.getText(); 
      double dGrade = Double.parseDouble(gradesOfD); 
      String gradesOfF = f_Grades.getText(); 
      double fGrade = Double.parseDouble(gradesOfF); 

      double totalCredits = aGrade + bGrade + cGrade + dGrade + fGrade; 


      double gradePoints = (aGrade * 4) + (bGrade * 3) + (cGrade * 2) + (dGrade * 1) + (fGrade * 0); 

      double sum = gradePoints/totalCredits; 

      DecimalFormat df = new DecimalFormat("0.00"); 
      df.setRoundingMode(RoundingMode.FLOOR); // this truncates, instead of rounds, the final output 
      //This outputs the GPA, where the double sum is the answer 
      JOptionPane.showMessageDialog(null, "Your GPA is " + df.format(sum)); 
      System.exit(0); //this makes sure values are wiped out after run. 
     } 
    }); 
} 




} 

}