Advertisement
julibar

Ej2 - Completo

Apr 25th, 2025 (edited)
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.06 KB | None | 0 0
  1. package Desarrollo;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JButton;
  6. import javax.swing.JComboBox;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11. import javax.swing.border.TitledBorder;
  12. import javax.swing.JPanel;
  13. import java.awt.Font;
  14.  
  15. public class VentanaEjercicio2 extends JFrame{
  16.  
  17.     private static final long serialVersionUID = 1L;
  18.    
  19.     private JTextField nota1, nota2, nota3;
  20.     private JComboBox<String> comboTP;
  21.     private JButton btnCalcular;
  22.     private JTextField txtPromedio;
  23.     private JTextField txtCondicion;
  24.  
  25.     public VentanaEjercicio2() {
  26.         setTitle("Promedio");
  27.         setBounds(500, 250, 501, 412);
  28.         getContentPane().setLayout(null);
  29.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  30.        
  31.         JPanel panel = new JPanel();
  32.         panel.setBounds(42, 31, 266, 184);
  33.         panel.setBorder(new TitledBorder(null, "Notas del estudiante", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  34.  
  35.         JLabel lbl1 = new JLabel("Nota 1:");
  36.         lbl1.setFont(new Font("Tahoma", Font.PLAIN, 12));
  37.         lbl1.setBounds(35, 32, 46, 25);
  38.         panel.add(lbl1);
  39.         panel.setLayout(null);
  40.         nota1 = new JTextField();
  41.         nota1.setBounds(91, 32, 136, 25);
  42.         panel.add(nota1);
  43.  
  44.         JLabel lbl2 = new JLabel("Nota 2:");
  45.         lbl2.setFont(new Font("Tahoma", Font.PLAIN, 12));
  46.         lbl2.setBounds(35, 66, 46, 25);
  47.         panel.add(lbl2);
  48.         nota2 = new JTextField();
  49.         nota2.setBounds(91, 102, 136, 25);
  50.         panel.add(nota2);
  51.  
  52.         JLabel lbl3 = new JLabel("Nota 3:");
  53.         lbl3.setFont(new Font("Tahoma", Font.PLAIN, 12));
  54.         lbl3.setBounds(35, 101, 46, 25);
  55.         panel.add(lbl3);
  56.         nota3 = new JTextField();
  57.         nota3.setBounds(91, 67, 136, 25);
  58.         panel.add(nota3);
  59.  
  60.         JLabel lblTP = new JLabel("TP:");
  61.         lblTP.setFont(new Font("Tahoma", Font.PLAIN, 12));
  62.         lblTP.setBounds(45, 136, 34, 25);
  63.         panel.add(lblTP);
  64.         comboTP = new JComboBox<>(new String[]{"Aprobado", "Desaprobado"});
  65.         comboTP.setBounds(91, 137, 136, 25);
  66.         panel.add(comboTP);
  67.        
  68.         getContentPane().add(panel);
  69.        
  70.         JPanel panel2 = new JPanel();
  71.         panel2.setBorder(new TitledBorder(null, "Notas del estudiante", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  72.         panel2.setBounds(42, 253, 266, 99);
  73.         panel2.setLayout(null);
  74.        
  75.         JLabel lblPromedio = new JLabel("Promedio:");
  76.         lblPromedio.setBounds(23, 28, 68, 15);
  77.         lblPromedio.setFont(new Font("Tahoma", Font.PLAIN, 13));
  78.         panel2.add(lblPromedio);
  79.        
  80.         JLabel lblCondicion = new JLabel("Condicion:");
  81.         lblCondicion.setFont(new Font("Tahoma", Font.PLAIN, 13));
  82.         lblCondicion.setBounds(23, 61, 68, 15);
  83.         panel2.add(lblCondicion);
  84.        
  85.         txtPromedio = new JTextField();
  86.         txtPromedio.setEditable(false);
  87.         txtPromedio.setBounds(98, 24, 120, 25);
  88.         panel2.add(txtPromedio);
  89.        
  90.         txtCondicion = new JTextField();
  91.         txtCondicion.setEditable(false);
  92.         txtCondicion.setBounds(98, 57, 120, 25);
  93.         panel2.add(txtCondicion);
  94.        
  95.         getContentPane().add(panel2);
  96.        
  97.         btnCalcular = new JButton("Calcular");
  98.         btnCalcular.setFont(new Font("Tahoma", Font.PLAIN, 12));
  99.         btnCalcular.setBounds(345, 112, 120, 30);
  100.         getContentPane().add(btnCalcular);
  101.        
  102.         btnCalcular.addActionListener(new ActionListener() {
  103.             public void actionPerformed(ActionEvent e) {
  104.                 calcularCondicion();
  105.             }
  106.         });
  107.        
  108.         JButton btnNuevo = new JButton("Nuevo");
  109.         btnNuevo.setFont(new Font("Tahoma", Font.PLAIN, 12));
  110.         btnNuevo.setBounds(345, 162, 120, 30);
  111.         getContentPane().add(btnNuevo);
  112.        
  113.         btnNuevo.addActionListener(new ActionListener() {
  114.             public void actionPerformed(ActionEvent e) {
  115.                 nota1.setText("");
  116.                 nota2.setText("");
  117.                 nota3.setText("");
  118.                 txtPromedio.setText("");
  119.                 txtCondicion.setText("");
  120.                 comboTP.setSelectedIndex(0);
  121.             }
  122.         });
  123.        
  124.         JButton btnSalir = new JButton("Salir");
  125.         btnSalir.setFont(new Font("Tahoma", Font.PLAIN, 12));
  126.         btnSalir.setBounds(345, 213, 120, 30);
  127.         getContentPane().add(btnSalir);
  128.  
  129.         btnSalir.addActionListener(new ActionListener() {
  130.             public void actionPerformed(ActionEvent e) {
  131.                 dispose();
  132.             }
  133.         });
  134.        
  135.        
  136.     }
  137.  
  138.     private void calcularCondicion() {
  139.         try {
  140.             double n1 = Double.parseDouble(nota1.getText());
  141.             double n2 = Double.parseDouble(nota2.getText());
  142.             double n3 = Double.parseDouble(nota3.getText());
  143.             String estadoTP = (String) comboTP.getSelectedItem();
  144.            
  145.             // Validar que las notas estén en el rango de 1 a 10
  146.             if (n1 < 1 || n1 > 10 || n2 < 0 || n2 > 10 || n3 < 0 || n3 > 10) {
  147.                 JOptionPane.showMessageDialog(this, "Las notas deben estar entre 1 y 10.", "Error", JOptionPane.ERROR_MESSAGE);
  148.                 return;
  149.             }
  150.  
  151.             double prom = (n1 + n2 + n3) / 3.0;
  152.             String cond;
  153.  
  154.             if (estadoTP.equals("Desaprobado") || n1 < 6 || n2 < 6 || n3 < 6) {
  155.                 cond = "Libre";
  156.             } else if (n1 >= 8 && n2 >= 8 && n3 >= 8) {
  157.                 cond = "Promocionado";
  158.             } else {
  159.                 cond = "Regular";
  160.             }
  161.            
  162.             txtPromedio.setText(String.format("%.2f", prom));
  163.             txtCondicion.setText(cond);
  164.  
  165.         } catch (NumberFormatException ex) {
  166.             JOptionPane.showMessageDialog(this, "Por favor ingresá todas las notas correctamente (números entre 1 y 10)", "Error", JOptionPane.ERROR_MESSAGE);
  167.         }
  168.     }
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement