Thank you
Can you make the textfields have a default value of zero when it starts up? I got the .class file and when I go to put in on a website i use the following simple site.
All i get is Applet Calculator noninited. This is my applet.Code:<html> <head> <title>Calculator</title> </head> <body> <applet code=calculator height=300 width=300></applet> </body> </html>
Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class calculator extends JApplet implements ActionListener { //Declare three text fields private JTextField jtfgrade1, jtfgrade2, jtfgrade3, jtfgrade4, jtfResults; private JTextField jtfcredit1, jtfcredit2,jtfcredit3,jtfcredit4, jtftot; private JButton jbtCalc, jbtClear; //Declare "Add" button //Main Method //public static void main(String[] args) //{ //calculator frame = new calculator(); //frame.pack(); //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setVisible(true); //} public void init() { //setTitle("Payment Calculator"); //Use panel p1 to group text fields JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(5,1)); p1.setBackground(Color.black); p1.add(new JLabel("<html><font color=white>Class 1 Grade</font></html>")); p1.add(jtfgrade1 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Class 1 Credits</font></html>")); p1.add(jtfcredit1 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Class 2 Grade</font></html>")); p1.add(jtfgrade2 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Class 2 Credits</font></html>")); p1.add(jtfcredit2 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Class 3 Grade</font></html>")); p1.add(jtfgrade3 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Class 3 Credits</font></html>")); p1.add(jtfcredit3 = new JTextField(1)); p1.add(new JLabel("<html><font color=white> Class 4 Grade</font></html>")); p1.add(jtfgrade4 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Class 4 Credits</font></html>")); p1.add(jtfcredit4 = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Total <u>GRADED</u> Credits</font></html>")); p1.add(jtftot = new JTextField(1)); p1.add(new JLabel("<html><font color=white>Your GPA is </font></html>")); p1.add(jtfResults = new JTextField(1)); jtfResults.setEditable(false); JPanel p3 = new JPanel(); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.setBackground(Color.black); p3.add(new JLabel("<html><center><font color=rgb(195,138,45)>When entering grades use these conversions A = 4 - B = 3 - C = 2 - D = 1</font></center></html>")); // p3.add(new JLabel(" A = 4 - B = 3 - C = 2 - D = 1")); //Use panel p2 for the button JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); p2.setBackground(Color.black); p2.add(jbtCalc = new JButton("<html><font color=white>Calculate</font></html>")); p2.add(jbtClear = new JButton("<html><font color=white>Clear</font></html>")); jbtCalc.setBackground(Color.black); jbtCalc.setForeground(Color.red); jbtClear.setBackground(Color.black); jbtClear.setForeground(Color.red); getContentPane().setLayout(new BorderLayout()); getContentPane().add(p1, BorderLayout.CENTER); getContentPane().add(p2, BorderLayout.SOUTH); getContentPane().add(p3, BorderLayout.NORTH); //Register Listener jbtCalc.addActionListener(this); jbtClear.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == jbtCalc) { //Get int values from text fields and use trim() to trim extra space in field double grade1 = (Double.parseDouble(jtfgrade1.getText().trim())); double grade2 = (Double.parseDouble(jtfgrade2.getText().trim())); double grade3 = (Double.parseDouble(jtfgrade3.getText().trim())); double grade4 = (Double.parseDouble(jtfgrade4.getText().trim())); double credit1 = (Double.parseDouble(jtfcredit1.getText().trim())); double credit2 = (Double.parseDouble(jtfcredit2.getText().trim())); double credit3 = (Double.parseDouble(jtfcredit3.getText().trim())); double credit4 = (Double.parseDouble(jtfcredit4.getText().trim())); double total_credits = (Double.parseDouble(jtftot.getText().trim())); double result = ((grade1 * credit1)+(grade2 * credit2)+(grade3 * credit3) +(grade4 * credit4))/total_credits; //Set results in the TextField jtfResults jtfResults.setText(String.valueOf(result)); } if (e.getSource() == jbtClear) { jtfResults.setText(" "); jtfgrade1.setText(" "); jtfgrade2.setText(" "); jtfgrade3.setText(" "); } } }




Reply With Quote