简体   繁体   中英

How to open new JFrame instead of using this

Everytime i wanted to open a new window when a button is clicked i have used this:

String password = new String(TextoContraseña.getPassword());
String nombre = (String)BotonNombreOperario.getSelectedItem();
if(!nombre.equals("Seleccione")) {
   if (Trabajo.validarOperario(nombre,password)) {
             Inicio.setNombreLogin(nombre);
             OpcionesOperario rec = new OpcionesOperario();
   if (JOptionPane.showConfirmDialog(this,rec,(String)BotonNombreOperario.getSelectedItem(), JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE)==JOptionPane.OK_OPTION){
           } // this open the new window
        } else {
            JOptionPane.showMessageDialog(null, "Contraseña incorrecta");
        }
    } else {
        JOptionPane.showMessageDialog(null, "Seleccione a un operario");
    }

The thing is that i know that is not the correct way of doing this, because i'm using a JOptionPane which is not used for select any option but to open a new window. The thing is that i want to do it right using a JFrame. For example i changed "OpcionesOperario" to extends JFrame instead of extends JPanel.

Also i changed in Inicio this code

Inicio.setNombreLogin(nombre);
OpcionesOperario rec = new OpcionesOperario();    
rec.setVisible(true);
Inicio.super.dispose();

But i get an error when i use rec.setVisible(true). I think is because i have no idea of using a JFrame, so i would appreciate some help. I know is a weird way of doing it, that's why i want to change if i can, because all the program works perfectly.


My OpcionesOperario class is:

public class OpcionesOperario extends javax.swing.JPanel {

/**
 * Creates new form OpcionesOperario
 */
String aux;
boolean realizado = false;
boolean realizado1 = false;
boolean realizado2 = false;
public static String textoImprimible;
String actividad;


public OpcionesOperario() {
    initComponents();
    for (Producto p : Trabajo.getProductos()) {
        jComboBox1.addItem(p.getiD_Producto());    
    } 
    aux=" ---------------------------------------------------------------------\r\n";
    aux+="                             Listado de Registros\r\n";
    aux+=" ---------------------------------------------------------------------\r\n\r\n"; 

   for (Trabajador t : Trabajo.getTrabajadores()) {
        if ((!t.getTipoEmpleado().equals("Sudo")) && (!t.getTipoEmpleado().equals("Administrador"))) {
            if (t.getNombre().equals(Inicio.getNombreLogin())) {
                if (t.isActividadIniciada()){
                    BotonInicio.setEnabled(false);
                    textoVariante.setText("Actividad en curso...");
                    actividad = t.getActividad();
                    jComboBox2.setSelectedItem(actividad);
                    jComboBox2.setEnabled(false);


                } else {
                    BotonFinal.setEnabled(false);
                    textoVariante.setText("Iniciar para comenzar");
                    textoVariante.setForeground(Color.red);
                }
            }
        }
    }
}

i have more methods but i don't think it is useful for the problem

The problem i get is:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: trabajo/OpcionesOperario$5
at trabajo.OpcionesOperario.initComponents(OpcionesOperario.java:163)
at trabajo.OpcionesOperario.<init>(OpcionesOperario.java:40)
at trabajo.Inicio.BotonOKActionPerformed(Inicio.java:325)
at trabajo.Inicio.access$500(Inicio.java:24)
at trabajo.Inicio$6.actionPerformed(Inicio.java:192)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.AbstractButton.doClick(AbstractButton.java:356)
at trabajo.Inicio$1.actionPerformed(Inicio.java:45)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
at javax.swing.JTextField.postActionEvent(JTextField.java:721)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Caused by: java.lang.ClassNotFoundException: trabajo.OpcionesOperario$5
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 51 more

I have changed to JFrame and i still get problems in the line where i say "rec.setVisible(true)". I will put the code of the constructor of Inicio which is the main class.

public Inicio() {

    initComponents();
    setIconImage(new ImageIcon(getClass().getResource("/Imagenes/Icono.png")).getImage()); // Esto es para cambiar el iconito de java
    this.centerScreen(this); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //this is new
    for (Trabajador t : Trabajo.getTrabajadores()) {
        if ((!t.getTipoEmpleado().equals("Sudo")) && (!t.getTipoEmpleado().equals("Administrador"))) {
            BotonNombreOperario.addItem(t.getNombre());                                   //Meter en combobox solo los operarios  
        }
    }

    TextoContraseña.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent e) {
           BotonOK.doClick();                                                       //darle a intro tambien entra
        }
    });
}

I think the problem is about Netbeans

Try this: Go to C:\\Users\\yourname\\.netbeans\\yourversion and delete the content.Then try to compile again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM