难以使JButton工作

时间:2015-11-01 23:45:01

标签: jbutton

我用Java创建一个简单的GPA计算器。它使用JOptionPane工作,但我希望用户能够在一个窗口中回答所有问题。所以,我正在试验JPanel。

当用户点击"提交"按钮,我希望程序计算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); 
}


}

2 个答案:

答案 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 ...
}

您还需要使用提交按钮注册处理程序。如果您使用MyGpaCalc作为ActionListener,则在初始化submit JButton后,您需要添加以下内容:

submit.addActionListener(this);

答案 1 :(得分: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.
        }
    });
}




}

}