Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Desarrollo;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.border.TitledBorder;
- import javax.swing.JPanel;
- import java.awt.Font;
- public class VentanaEjercicio2 extends JFrame{
- private static final long serialVersionUID = 1L;
- private JTextField nota1, nota2, nota3;
- private JComboBox<String> comboTP;
- private JButton btnCalcular;
- private JTextField txtPromedio;
- private JTextField txtCondicion;
- public VentanaEjercicio2() {
- setTitle("Promedio");
- setBounds(500, 250, 501, 412);
- getContentPane().setLayout(null);
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- JPanel panel = new JPanel();
- panel.setBounds(42, 31, 266, 184);
- panel.setBorder(new TitledBorder(null, "Notas del estudiante", TitledBorder.LEADING, TitledBorder.TOP, null, null));
- JLabel lbl1 = new JLabel("Nota 1:");
- lbl1.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lbl1.setBounds(35, 32, 46, 25);
- panel.add(lbl1);
- panel.setLayout(null);
- nota1 = new JTextField();
- nota1.setBounds(91, 32, 136, 25);
- panel.add(nota1);
- JLabel lbl2 = new JLabel("Nota 2:");
- lbl2.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lbl2.setBounds(35, 66, 46, 25);
- panel.add(lbl2);
- nota2 = new JTextField();
- nota2.setBounds(91, 102, 136, 25);
- panel.add(nota2);
- JLabel lbl3 = new JLabel("Nota 3:");
- lbl3.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lbl3.setBounds(35, 101, 46, 25);
- panel.add(lbl3);
- nota3 = new JTextField();
- nota3.setBounds(91, 67, 136, 25);
- panel.add(nota3);
- JLabel lblTP = new JLabel("TP:");
- lblTP.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblTP.setBounds(45, 136, 34, 25);
- panel.add(lblTP);
- comboTP = new JComboBox<>(new String[]{"Aprobado", "Desaprobado"});
- comboTP.setBounds(91, 137, 136, 25);
- panel.add(comboTP);
- getContentPane().add(panel);
- JPanel panel2 = new JPanel();
- panel2.setBorder(new TitledBorder(null, "Notas del estudiante", TitledBorder.LEADING, TitledBorder.TOP, null, null));
- panel2.setBounds(42, 253, 266, 99);
- panel2.setLayout(null);
- JLabel lblPromedio = new JLabel("Promedio:");
- lblPromedio.setBounds(23, 28, 68, 15);
- lblPromedio.setFont(new Font("Tahoma", Font.PLAIN, 13));
- panel2.add(lblPromedio);
- JLabel lblCondicion = new JLabel("Condicion:");
- lblCondicion.setFont(new Font("Tahoma", Font.PLAIN, 13));
- lblCondicion.setBounds(23, 61, 68, 15);
- panel2.add(lblCondicion);
- txtPromedio = new JTextField();
- txtPromedio.setEditable(false);
- txtPromedio.setBounds(98, 24, 120, 25);
- panel2.add(txtPromedio);
- txtCondicion = new JTextField();
- txtCondicion.setEditable(false);
- txtCondicion.setBounds(98, 57, 120, 25);
- panel2.add(txtCondicion);
- getContentPane().add(panel2);
- btnCalcular = new JButton("Calcular");
- btnCalcular.setFont(new Font("Tahoma", Font.PLAIN, 12));
- btnCalcular.setBounds(345, 112, 120, 30);
- getContentPane().add(btnCalcular);
- btnCalcular.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- calcularCondicion();
- }
- });
- JButton btnNuevo = new JButton("Nuevo");
- btnNuevo.setFont(new Font("Tahoma", Font.PLAIN, 12));
- btnNuevo.setBounds(345, 162, 120, 30);
- getContentPane().add(btnNuevo);
- btnNuevo.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- nota1.setText("");
- nota2.setText("");
- nota3.setText("");
- txtPromedio.setText("");
- txtCondicion.setText("");
- comboTP.setSelectedIndex(0);
- }
- });
- JButton btnSalir = new JButton("Salir");
- btnSalir.setFont(new Font("Tahoma", Font.PLAIN, 12));
- btnSalir.setBounds(345, 213, 120, 30);
- getContentPane().add(btnSalir);
- btnSalir.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- dispose();
- }
- });
- }
- private void calcularCondicion() {
- try {
- double n1 = Double.parseDouble(nota1.getText());
- double n2 = Double.parseDouble(nota2.getText());
- double n3 = Double.parseDouble(nota3.getText());
- String estadoTP = (String) comboTP.getSelectedItem();
- // Validar que las notas estén en el rango de 1 a 10
- if (n1 < 1 || n1 > 10 || n2 < 0 || n2 > 10 || n3 < 0 || n3 > 10) {
- JOptionPane.showMessageDialog(this, "Las notas deben estar entre 1 y 10.", "Error", JOptionPane.ERROR_MESSAGE);
- return;
- }
- double prom = (n1 + n2 + n3) / 3.0;
- String cond;
- if (estadoTP.equals("Desaprobado") || n1 < 6 || n2 < 6 || n3 < 6) {
- cond = "Libre";
- } else if (n1 >= 8 && n2 >= 8 && n3 >= 8) {
- cond = "Promocionado";
- } else {
- cond = "Regular";
- }
- txtPromedio.setText(String.format("%.2f", prom));
- txtCondicion.setText(cond);
- } catch (NumberFormatException ex) {
- JOptionPane.showMessageDialog(this, "Por favor ingresá todas las notas correctamente (números entre 1 y 10)", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement