Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Assignment3 extends JApplet implements ActionListener {
- JTextField T1 ;
- JTextField T2 ;
- JLabel invalid;
- JTextArea result;
- JButton exit ;
- JLabel L1;
- JLabel name;
- JLabel gpa;
- JButton save;
- JButton clear;
- int max=0;
- double [] gArray;
- String [] nArray;
- public void init() {
- Container cp = getContentPane();
- cp.setLayout(new FlowLayout());
- L1 = new JLabel("Enter studens name and GPA: ");
- cp.add(L1);
- name = new JLabel("Name: ",SwingConstants.LEFT);
- T1 = new JTextField(SwingConstants.RIGHT);
- T1.setColumns(30);
- cp.add(name);
- cp.add(T1);
- gpa = new JLabel("GPA: ",SwingConstants.LEFT);
- cp.add(gpa);
- T2 = new JTextField(SwingConstants.RIGHT);
- T2.setColumns(20);
- cp.add(T2);
- result = new JTextArea(5,30);
- cp.add(result);
- result.setVisible(false);
- save = new JButton("Save");
- cp.add(save);
- clear = new JButton("Clear");
- cp.add(clear);
- exit = new JButton("Exit");
- cp.add(exit);
- invalid = new JLabel("Invalid GPA!");
- cp.add(invalid);
- invalid.setForeground(Color.RED);
- invalid.setVisible(false);
- save.addActionListener(this);
- clear.addActionListener(this);
- exit.addActionListener(this);
- nArray = new String [100];
- gArray = new double[100];
- }
- public void actionPerformed(ActionEvent e) {
- String ac = e.getActionCommand();
- if(ac.equals("Save")){
- Double g = Double.valueOf(T2.getText());
- if(g<0||g>4)
- invalid.setVisible(true);
- else{
- nArray[i]=T1.getText();
- gArray[i]=g;
- i++;
- T1.setText("");
- T2.setText("");
- }
- }
- if(ac.equals("Clear")){
- T1.setText("");
- T2.setText("");
- invalid.setVisible(false);
- }
- //Exit button
- if(ac.equals("Exit")){
- result.setVisible(true);
- save.setVisible(false);
- clear.setVisible(false);
- exit.setVisible(false);
- T1.setVisible(false);
- T2.setVisible(false);
- L1.setVisible(false);
- name.setVisible(false);
- gpa.setVisible(false);
- for(int j=0;j<i;j++){
- if(gArray[j]>gArray[max]) {
- max=j;
- }//if end
- }// for loop end
- String s=(nArray[max]+" Scored the maximum GPA ("+gArray[max]+") n among total of " +i+" students.");
- result.setText(s);
- }//first if end
- }// end of method
- }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement