在JLabel上显示变量

时间:2015-09-02 00:49:24

标签: java swing user-interface average

只是平均计算器。那么,你可以与程序推进交互 成绩和选择平均值。首先,我无法让它起作用 意思是,所以我改变它。然后,每次我尝试选择a时都会出错 方法,所以不。然后,该方法将无法正常工作。所以我终于明白了 工作,除了一件事......它不能告诉我平均值。

private JTextField textField_2;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JLabel mostrar;
private double combo, nota1, nota2, nota3, nota4, sem1, sem2, fin, aux1, aux2;
private JRadioButton DosSemestres, CuatroParciales, TresParciales, DosParciales;
private JLabel SelecMetod, LBLMax , LBLMin, lblNota1, lblNota2, lblNota3, lblNota4, lblSem1, lblsem2, lblfinal;
private JButton NotaBoton, Calcular, Elegir;
private JLabel AreaSem1, AreaSem2, AreaPromedioFin;
private JLabel Clasi;


public Panel() {
    setLayout(null);
    setSize(850, 556);
    //metodo de creacion de año electivo
    SelecMetod = new JLabel("Seleccione el metodo electivo: ");
    DosSemestres = new JRadioButton("1. Dos semestres de dos parciales cada uno");
    CuatroParciales = new JRadioButton("2. Cuatro parciales repartidos en un año electivo");
    TresParciales = new JRadioButton("3. Tres parciales repartidos en un año electivo");
    DosParciales = new JRadioButton("4. Dos parciales repartidos en un año electivo");
    ButtonGroup group = new ButtonGroup();
    Dimension size1 = DosSemestres.getPreferredSize();
    Dimension size2 = CuatroParciales.getPreferredSize();
    Dimension size3 = TresParciales.getPreferredSize();
    Dimension size4 = DosParciales.getPreferredSize();
    SelecMetod.setBounds(20, 5, size1.width, 20);
    SelecMetod.setForeground(Color.BLUE);
    DosSemestres.setBounds(20, 25, size1.width, 20);
    CuatroParciales.setBounds(20, 55, size2.width, 20);
    TresParciales.setBounds(20, 85, size3.width, 20);
    DosParciales.setBounds(20, 115, size4.width, 20);
    group.add(DosSemestres);
    group.add(CuatroParciales);
    group.add(TresParciales);
    group.add(DosParciales);
    add(SelecMetod);
    add(DosSemestres);
    add(CuatroParciales);
    add(TresParciales);
    add(DosParciales);
    DosSemestres.addActionListener(this);
    CuatroParciales.addActionListener(this);
    TresParciales.addActionListener(this);
    DosParciales.addActionListener(this);

    //Notas minimas y maximas
    LBLMax = new JLabel("Ingrese la nota minima de excelencia academia: ");
    LBLMax.setForeground(Color.GREEN);
    LBLMin = new JLabel("Ingrese la nota minima para aprobar: ");
    LBLMin.setForeground(Color.GREEN);
    Dimension Not1 = LBLMax.getPreferredSize();
    Dimension Not2 = LBLMin.getPreferredSize();
    LBLMax.setBounds(440, 25, Not1.width, 20);
    LBLMin.setBounds(440, 90 , Not2.width, 20);
    add(LBLMax);
    add(LBLMin);

    textField = new JTextField("");
    textField.setBackground(Color.BLUE);
    textField.setForeground(Color.WHITE);
    textField_2 = new JTextField("");
    textField_2.setBackground(Color.BLUE);
    textField_2.setForeground(Color.WHITE);
    textField.setBounds(440, 55 , 60, 20);
    textField_2.setBounds(440, 120 , 60, 20);
    add(textField);
    add(textField_2);
    mostrar = new JLabel("La nota media es:" );
    mostrar.setBounds(440, 140, 180, 30);
    add(mostrar);

    Elegir = new JButton("Aceptar");
    Elegir.setBounds(440, 170, 120, 30);
    add(Elegir);
    Elegir.addActionListener(this);

    //Notas reales
    lblNota1 = new JLabel("Ingrese la primera nota:");
    lblNota2 = new JLabel("Ingrese la segunda nota:");
    lblNota3 = new JLabel("Ingrese la tercera nota:");
    lblNota4 = new JLabel("Ingrese la cuarta nota:");
    Dimension nota1 = lblNota1.getPreferredSize();
    Dimension nota2 = lblNota2.getPreferredSize();
    Dimension nota3 = lblNota4.getPreferredSize();
    Dimension nota4 = lblNota4.getPreferredSize();
    lblNota1.setBounds(30, 220, nota1.width, nota1.height);
    lblNota2.setBounds(30, 250, nota2.width, nota2.height);
    lblNota3.setBounds(30, 280, 280, nota3.height);
    lblNota4.setBounds(30, 310, nota4.width, nota4.height);
    textField_1 = new JTextField();
    textField_1.setBackground(Color.LIGHT_GRAY);
    textField_1.setForeground(Color.BLACK);
    textField_1.setBounds(250, 220, 40, 20);
    add(textField_1);
    textField_3 = new JTextField();
    textField_3.setBackground(Color.LIGHT_GRAY);
    textField_3.setForeground(Color.BLACK);
    textField_3.setBounds(250, 250, 40, 20);
    add(textField_3);
    textField_4 = new JTextField();
    textField_4.setBackground(Color.LIGHT_GRAY);
    textField_4.setForeground(Color.BLACK);
    textField_4.setBounds(250, 280, 40, 20);
    add(textField_4);
    textField_4.setVisible(false);

    textField_5 = new JTextField();
    textField_5.setBackground(Color.LIGHT_GRAY);
    textField_5.setForeground(Color.BLACK);
    textField_5.setBounds(250, 310, 40, 20);
    add(textField_5);
    textField_5.setVisible(false);

    add(lblNota1);
    add(lblNota2);
    add(lblNota3);
    add(lblNota4);
    lblNota3.setVisible(false);
    lblNota4.setVisible(false);


    //Boton que calculara
    Calcular = new JButton("Calcular");
    Calcular.setBounds(130, 350 , 160, 40);
    add(Calcular);
    Calcular.addActionListener(this);

    //Promedios
    lblSem1 = new JLabel("El promedio del primer semestre es: ");
    lblsem2 = new JLabel("El promedio del segundo semestre es: ");
    lblSem1.setBounds(440, 220, 240, 40);
    add(lblSem1);
    lblSem1.setVisible(false);
    lblsem2.setBounds(440, 260, 250, 40);
    add(lblsem2);
    lblsem2.setVisible(false);
    AreaSem1 = new JLabel();
    AreaSem2 = new JLabel();
    AreaSem1.setBounds(520, 220, 80, 50);
    AreaSem1.setVisible(false);
    add(AreaSem1);
    AreaSem2.setBounds(520, 260, 80, 50);
    AreaSem2.setVisible(false);
    add(AreaSem2);

    lblfinal = new JLabel("El promedio final es: ");
    lblfinal.setBounds(440, 300, 240, 40);
    add(lblfinal);
    AreaPromedioFin = new JLabel();
    AreaPromedioFin.setBounds(520, 300, 90, 40);
    add(AreaPromedioFin);
    Clasi = new JLabel();
    Clasi.setBackground(Color.BLUE);
    Clasi.setBounds(520, 350, 250, 50);
    AreaPromedioFin.setBounds(520, 360, 180, 40);
    add(Clasi);

}


@Override
public void actionPerformed(ActionEvent e) {
     if(e.getSource() == Elegir){
            double min = (Double.parseDouble(textField.getText()));
            double aprobado = (Double.parseDouble(textField_2.getText()));
            double R = ((min + aprobado)/2);
            mostrar.setText("La nota media es:  "+String.valueOf(R));}

    if(e.getSource() == DosSemestres){
        lblNota3.setVisible(true);
        lblNota4.setVisible(true);
        textField_5.setVisible(true);
        textField_4.setVisible(true);
        lblSem1.setVisible(true);
        lblsem2.setVisible(true);
        AreaSem1.setVisible(true);
        AreaSem2.setVisible(true);



        double nota1 = (Double.parseDouble(textField_1.getText()));
        double nota2 = (Double.parseDouble(textField_3.getText()));
        double sem1 = (nota1+nota2)/2;
        double nota3 = (Double.parseDouble(textField_4.getText()));
        double nota4 = (Double.parseDouble(textField_5.getText()));
        double sem2 = (nota3+nota4)/2;
        double fin = (nota1+nota2+nota3+nota4)/4;    
        if(e.getSource() == Calcular){
              AreaSem1.setText(String.valueOf(sem1));
              AreaSem2.setText(String.valueOf(sem2));
              AreaPromedioFin.setText(String.valueOf(fin));
        }


    else if(e.getSource() == CuatroParciales){
        lblNota3.setVisible(true);
        lblNota4.setVisible(true);
        textField_5.setVisible(true);
        textField_4.setVisible(true);
        lblSem1.setVisible(false);
        lblsem2.setVisible(false);
        AreaSem1.setVisible(false);
        AreaSem2.setVisible(false);

        double nota1_1 = (Double.parseDouble(textField_1.getText()));
        double nota2_1 = (Double.parseDouble(textField_3.getText()));
        double nota3_1 = (Double.parseDouble(textField_4.getText()));
        double nota4_1 = (Double.parseDouble(textField_5.getText()));
        double fin_1 = (nota1+nota2+nota3+nota4)/4;    
        if(e.getSource() == Calcular){
              AreaPromedioFin.setText(String.valueOf(fin));}



                }
    else if(e.getSource() == TresParciales){
        lblNota3.setVisible(true);
        lblNota4.setVisible(false);
        textField_5.setVisible(false);
        textField_4.setVisible(true);
        lblSem1.setVisible(false);
        lblsem2.setVisible(false);
        AreaSem1.setVisible(false);
        AreaSem2.setVisible(false);

        double nota1_2 = (Double.parseDouble(textField_1.getText()));
        double nota2_2 = (Double.parseDouble(textField_3.getText()));
        double nota3_2 = (Double.parseDouble(textField_4.getText()));
        double fin_2 = (nota1+nota2+nota3)/3;    
        if(e.getSource() == Calcular){
              AreaPromedioFin.setText(String.valueOf(fin));}
                }


    else if(e.getSource() == DosParciales){
        lblNota3.setVisible(false);
        lblNota4.setVisible(false);
        textField_5.setVisible(false);
        textField_4.setVisible(false);
        lblSem1.setVisible(false);
        lblsem2.setVisible(false);
        AreaSem1.setVisible(false);
        AreaSem2.setVisible(false);

        double nota1_3 = (Double.parseDouble(textField_1.getText()));
        double nota2_3 = (Double.parseDouble(textField_3.getText()));

        double fin_3 = (nota1+nota2)/2;    
        if(e.getSource() == Calcular){
              AreaPromedioFin.setText(String.valueOf(fin));}

    }
    }
    }
}

1 个答案:

答案 0 :(得分:3)

如果字段为空,

double nota1 = (Double.parseDouble(textField_1.getText()));会导致潜在java.lang.NumberFormatException。您应该使用textField_1.getText().isEmpty()检查字段是否包含任何文本。如果用户输入无效值,您还应该捕获NumberFormatException

JFormattedTextFieldJSpinner有助于减少一些问题。有关详细信息,请查看How to Use Formatted Text FieldsHow to Use Spinners

当用户选择1. Dos semestres de dos parciales cada uno时,您会显示新字段,但在用户输入任何内容之前,您会尝试处理这些字段,这会进一步导致NumberFormatException

以下......

if (e.getSource() == DosSemestres) {
    //...
    if (e.getSource() == Calcular) {

永远不会奏效。该事件只能有一个来源,因此它可以是DosSemestresCalcular,但不能同时为两个。这似乎是您问题的重要来源,以及之前的评论。

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

您可能还希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码