Does not work dispose () in Java [closed]

0

This is the main Frame code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;

public class Venta extends JFrame implements ActionListener, ChangeListener, ItemListener {

    private JLabel micro, mother, memoria;
    private JComboBox listaMicro;
    private JCheckBox monitor, disco;
    private JRadioButton asus, gigabyte, msi, dosmb, cuatromb, ochomb;
    private ButtonGroup mothers, memorias;
    private JTextField total;
    private JButton calcular, ver, salir;
    private int precioMicro;
    private int precioMadre;
    private int precioMemoria;
    private int precioDisco;
    private int precioMonitor;
    private int estadoImporte;

    public Venta() {
        setLayout(null);
        micro = new JLabel("Tipo de Micro:");
        micro.setBounds(100, 50, 150, 30);
        add(micro);
        mother = new JLabel("Placa madre: ");
        mother.setBounds(30, 150, 150, 30);
        add(mother);
        memoria = new JLabel("Memoria: ");
        memoria.setBounds(30, 250, 150, 30);
        add(memoria);
        listaMicro = new JComboBox();
        listaMicro.setBounds(300, 50, 150, 30);
        listaMicro.addItem("Intel");
        listaMicro.addItem("Atlon");
        listaMicro.addItem("Turion");
        add(listaMicro);
        listaMicro.addItemListener(this);
        listaMicro.setSelectedIndex(1);
        mothers = new ButtonGroup();
        asus = new JRadioButton("Asus");
        asus.setBounds(240, 150, 150,30);
        asus.addChangeListener(this);
        add(asus);
        mothers.add(asus);
        gigabyte = new JRadioButton("GigaByte");
        gigabyte.setBounds(390, 150, 150, 30);
        gigabyte.addChangeListener(this);
        add(gigabyte);
        mothers.add(gigabyte);
        msi = new JRadioButton("Msi");
        msi.setBounds(540, 150, 150,30);
        msi.addChangeListener(this);
        add(msi);
        mothers.add(msi);
        memorias = new ButtonGroup();
        dosmb = new JRadioButton("2 MB");
        dosmb.setBounds(240, 250, 150,30);
        dosmb.addChangeListener(this);
        add(dosmb);
        memorias.add(dosmb);
        cuatromb = new JRadioButton("4 MB");
        cuatromb.setBounds(390, 250, 150, 30);
        cuatromb.addChangeListener(this);
        add(cuatromb);
        memorias.add(cuatromb);
        ochomb = new JRadioButton("8 MB");
        ochomb.setBounds(540, 250, 150,30);
        ochomb.addChangeListener(this);
        add(ochomb);
        memorias.add(ochomb);
        monitor = new JCheckBox("Monitor");
        monitor.setBounds(90, 300,150,30);
        monitor.addChangeListener(this);
        add(monitor);
        disco = new JCheckBox("Disco");
        disco.setBounds(240, 300, 150, 30);
        disco.addChangeListener(this);
        add(disco);
        calcular = new JButton("Calcular Importe");
        calcular.setBounds(30, 400, 180, 40);
        calcular.addActionListener(this);
        add(calcular);
        ver = new JButton("Ver Dialogo");
        ver.setBounds(30, 500, 180, 40);
        ver.addActionListener(this);
        ver.setEnabled(false);
        add(ver);
        total = new JTextField("0,00");
        total.setBounds(300, 400, 180, 40);
        add(total);
        salir = new JButton("Salir");
        salir.setBounds(300,500,180,40);
        salir.addActionListener(this);
        add(salir);
        estadoImporte=0;
    }

    public void itemStateChanged(ItemEvent f) {

        if(f.getSource()==listaMicro) {

            String tmicro = (String) listaMicro.getSelectedItem();
            if (tmicro.equals("Intel")) {
                precioMicro=150;
            }
            else if(tmicro.equals("Atlon")) {
                precioMicro=80;
            }
            else {
                precioMicro=120;
            }
        }
    }

    public void stateChanged(ChangeEvent g) {

        precioMonitor=0;
        precioDisco=0;

        if(asus.isSelected()==true) {
            precioMadre=75;
            }
        if(gigabyte.isSelected()==true) {
            precioMadre=320;
        }
        if(msi.isSelected()==true) {
            precioMadre=100;
        }

        if(dosmb.isSelected()==true) {
            precioMemoria=50;
        }
        if(cuatromb.isSelected()==true) {
            precioMemoria=80;
        }
        if(ochomb.isSelected()==true) {
            precioMemoria=130;
        }
        if(monitor.isSelected()==true) {
            precioMonitor=250;
        }
        if(disco.isSelected()==true) {
            precioDisco=80;
        }
    }


    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==calcular) {
            // ya que se presiono calcular habilita para ver el dialogo
            estadoImporte = 1;
            // hace la suma de los componentes, formatea y los coloca en el textfield
            float totalgral = precioDisco+precioMonitor+precioMemoria+precioMicro+precioMadre;
            String cadena=String.format("%.02f", totalgral);
            total.setText(cadena);
        }
        if(e.getSource()==salir) {
            System.exit(0);
        }
        if (estadoImporte == 1) {
            // habilita el dialogo porque se presiono calcular
            ver.setEnabled(true);
            if (e.getSource()==ver) {
                String tmicro = (String) listaMicro.getSelectedItem();
                Dialogo dialogo1 = new Dialogo(tmicro, total);
                dialogo1.setBounds(50,50,500,500);
                dialogo1.setVisible(true);
            }
        }   


        }



    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Venta formulario1 = new Venta();
        formulario1.setSize(700, 650);
        formulario1.setVisible(true);
        formulario1.setResizable(false);
        formulario1.setTitle("Venta de Computadoras");


    }

}

// y este es el del JDialog donde no me funciona el dispose()

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

class Dialogo extends JDialog implements ActionListener {

    private JLabel etiqTipoMicro;
    private JLabel etiqImporte;
    private JLabel tipoMicro;
    private JLabel importe;
    private JButton cerrar;

    Dialogo (String tmicro, JTextField total){
        setLayout(null);
        setResizable(false);
        setModal(true);
        JLabel etiqTipoMicro = new JLabel("Tipo de Micro: ");
        etiqTipoMicro.setBounds(30, 30, 150, 30);
        add(etiqTipoMicro);
        // recupera el tipo de micro
        JLabel tipoMicro = new JLabel(tmicro);
        tipoMicro.setBounds(200, 30, 150, 30);
        add(tipoMicro);
        JLabel etiqImporte = new JLabel("Importe Total: ");
        etiqImporte.setBounds(30, 100, 150, 30);
        add(etiqImporte);
        // recupera el texto del textfield
        JLabel importe = new JLabel (total.getText());
        importe.setBounds(200, 100, 150, 30);
        add(importe);
        JButton cerrar = new JButton("Cerrar");
        cerrar.setBounds(300, 300, 150, 30);
        cerrar.addActionListener(this);
        add(cerrar);

    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==cerrar){
         dispose () ; 

            }


    }

}
    
asked by Diego K. 17.11.2017 в 13:36
source

1 answer

1

Be very careful with the following:

The class Dialogo has 2 JButton called cerrar , one global and private and the other at the constructor level of the class. The method actionPerformed makes use of JButton global, which is not instantiated , so if you act on it, nothing will happen.

The solution is to modify JButton cerrar = new JButton("Cerrar"); by cerrar = new JButton("Cerrar"); to instantiate that JButton that was declared private and global.

In fact all the global elements of that class are not instances but you use new components from the constructor, that could bring you many headaches thinking about large scale projects because, among other things, you duplicate code and keep it becomes tediously expensive.

    
answered by 17.11.2017 / 16:04
source