Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JLabel上显示变量_Java_Swing_User Interface_Average - Fatal编程技术网

Java 在JLabel上显示变量

Java 在JLabel上显示变量,java,swing,user-interface,average,Java,Swing,User Interface,Average,只是一个普通的计算器。嗯,你可以和程序互动 评分并选择平均值。首先,我无法让它工作,因为我承担了 我的意思是,所以我改变了。然后,每次我尝试选择一个选项时,它都会给出一个错误 方法,所以没有。那么,这个方法就不能正常工作了。所以我终于明白了 工作,除了一件事。。。它不能显示我的平均水平 private JTextField textField_2; private JTextField textField; private JTextField textField_1; private JTex

只是一个普通的计算器。嗯,你可以和程序互动 评分并选择平均值。首先,我无法让它工作,因为我承担了 我的意思是,所以我改变了。然后,每次我尝试选择一个选项时,它都会给出一个错误 方法,所以没有。那么,这个方法就不能正常工作了。所以我终于明白了 工作,除了一件事。。。它不能显示我的平均水平

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));}

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

JFormattedTextField
JSpinner
将有助于减少这些问题。查看和了解更多详细信息

当用户选择
1时。Dos semestres de Dos parciales cada uno
,显示新字段,但在用户输入任何内容之前,您尝试处理这些字段,这将进一步导致
数字格式异常

以下是

if (e.getSource() == DosSemestres) {
    //...
    if (e.getSource() == Calcular) {
这是行不通的。事件只能有一个源,因此它可以是
DosSemestres
Calcular
,但决不能同时是两者。这似乎是您的问题的一个重要来源,以及之前的评论

避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题


您可能还希望进行通读,这将使人们更容易阅读您的代码,您也更容易阅读他人的代码

您应该将其标记为“java”和“swing”。您要求我们做什么?当你问这样的问题时,请只发布引起问题的代码,并明确你想要修复的内容。变量名不应以大写字符开头。您不应该使用setBounds()。相反,你应该使用。考虑提供一个演示你的问题。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应