Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Assignment3 extends JApplet implements ActionListener {
  6. JTextField T1 ;
  7. JTextField T2 ;
  8. JLabel invalid;
  9. JTextArea result;
  10. JButton exit ;
  11. JLabel L1;
  12. JLabel name;
  13. JLabel gpa;
  14. JButton save;
  15. JButton clear;
  16. int max=0;
  17. double [] gArray;
  18. String [] nArray;
  19.  
  20. public void init() {
  21. Container cp = getContentPane();
  22. cp.setLayout(new FlowLayout());
  23. L1 = new JLabel("Enter studens name and GPA: ");
  24. cp.add(L1);
  25. name = new JLabel("Name: ",SwingConstants.LEFT);
  26. T1 = new JTextField(SwingConstants.RIGHT);
  27. T1.setColumns(30);
  28. cp.add(name);
  29. cp.add(T1);
  30. gpa = new JLabel("GPA: ",SwingConstants.LEFT);
  31. cp.add(gpa);
  32. T2 = new JTextField(SwingConstants.RIGHT);
  33. T2.setColumns(20);
  34. cp.add(T2);
  35. result = new JTextArea(5,30);
  36. cp.add(result);
  37. result.setVisible(false);
  38. save = new JButton("Save");
  39. cp.add(save);
  40. clear = new JButton("Clear");
  41. cp.add(clear);
  42. exit = new JButton("Exit");
  43. cp.add(exit);
  44. invalid = new JLabel("Invalid GPA!");
  45. cp.add(invalid);
  46. invalid.setForeground(Color.RED);
  47. invalid.setVisible(false);
  48. save.addActionListener(this);
  49. clear.addActionListener(this);
  50. exit.addActionListener(this);
  51. nArray = new String [100];
  52. gArray = new double[100];
  53. }
  54.  
  55. public void actionPerformed(ActionEvent e) {
  56. String ac = e.getActionCommand();
  57. if(ac.equals("Save")){
  58. Double g = Double.valueOf(T2.getText());
  59. if(g<0||g>4)
  60. invalid.setVisible(true);
  61. else{
  62. nArray[i]=T1.getText();
  63. gArray[i]=g;
  64. i++;
  65. T1.setText("");
  66. T2.setText("");
  67. }
  68. }
  69. if(ac.equals("Clear")){
  70. T1.setText("");
  71. T2.setText("");
  72. invalid.setVisible(false);
  73. }
  74. //Exit button
  75. if(ac.equals("Exit")){
  76. result.setVisible(true);
  77. save.setVisible(false);
  78. clear.setVisible(false);
  79. exit.setVisible(false);
  80. T1.setVisible(false);
  81. T2.setVisible(false);
  82. L1.setVisible(false);
  83. name.setVisible(false);
  84. gpa.setVisible(false);
  85. for(int j=0;j<i;j++){
  86. if(gArray[j]>gArray[max]) {
  87. max=j;
  88. }//if end
  89. }// for loop end
  90. String s=(nArray[max]+" Scored the maximum GPA ("+gArray[max]+") n among total of " +i+" students.");
  91. result.setText(s);
  92. }//first if end
  93. }// end of method
  94. }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement