【问题标题】:Show variable on JLabel在 JLabel 上显示变量
【发布时间】:2015-09-02 00:49:23
【问题描述】:

只是和平均计算器。好吧,你可以与程序进行交互 等级和选择平均值。首先,我无法让它工作 意思是,所以我改变它。然后,每次我尝试选择一个时它都会出错 方法,所以不行。然后,该方法将无法正常工作。所以我终于明白了 工作,除了一件事......它不能显示我的平均水平。

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

    }
    }
    }
}

【问题讨论】:

  • 你应该把它标记为“java”和“swing”。
  • 你要我们做什么?当您在 SO 上提出问题时,请仅发布导致问题的代码,并明确您要修复的内容。
  • 变量名不应以大写字符开头。你不应该使用 setBounds()。相反,您应该使用Layout Managers
  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应

标签: java swing user-interface average


【解决方案1】:

如果该字段为空,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) {

永远不会工作。该事件只能有一个来源,因此它要么是DosSemestres 要么是Calcular,但不能同时是两者。与之前的 cmets 一样,这似乎是您的问题的一个重要根源。

避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

您可能还想通读Code Conventions for the Java TM Programming Language,它将使人们更容易阅读您的代码并让您更轻松地阅读其他人

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2013-03-17
    • 2013-10-12
    相关资源
    最近更新 更多