Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/08/2013, 03:13
enderzote
 
Fecha de Ingreso: agosto-2013
Ubicación: Barcelona
Mensajes: 7
Antigüedad: 10 años, 9 meses
Puntos: 0
Error sin localizar en programa

Hola, soy programador novato en Java, aunque tengo bastante experiencia en TBasic, Pascal y Assembler.

Estoy diseñando una aplicación a modo de prueba para implementar la estructura en otro proyecto de mayores dimensiones. La idea es trabajar con 2 ventanas idénticas, las cuales están provistas de 3 botones:

- botonIncrementar: añade 1 a la variable contador1 o al 2, según esté activa la ventana 1 ó 2
- botonSalir: Sale de la aplicación
- botonCambia: Cierra la ventana actual y activa la otra

Se podría añadir a cada ventana 2 displays con los contadores acumulados, pero aún estoy buscando la forma.

El caso es que Eclipse me detecta un error en la línea:

Pantalla1 frame1 = new Pantalla1();

Concretamente me subraya "new Pantalla1()", mientras que no señala error en la siguiente línea de código, que es su gemela. Seguro que hay más errores de concepto que quizá influyan (o no) en ese error u otros, pero ya dije que soy novato en Java. El error específico que me marca Eclipse es:

"No enclosing instance of type CambiaPantalla is accessible. Must qualify the allocation with an enclosing instance of type CambiaPantalla (e.g. x.new A() where x is an instance of CambiaPantalla)"

Les agradecería cualquier tipo de orientación para la solución del error. Les adjunto el código a continuación.

Saludos.

Cita:
Código Java:
Ver original
  1. package Default;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. public class CambiaPantalla {
  11.    
  12.     Pantalla1 frame1;
  13.     Pantalla2 frame2;
  14.     public int contador1 = 0;
  15.     public int contador2 = 0;
  16.            
  17.     public static void main (String[] args) {
  18.         Pantalla1 frame1 = new Pantalla1(); // AQUÍ INDICA EL ERROR
  19.         Pantalla2 frame2 = new Pantalla2();
  20.         frame2.setVisible(false);
  21.         frame1.setVisible(true);
  22.     }
  23.    
  24.     public class Pantalla1 extends JFrame implements ActionListener {
  25.        
  26.         private JPanel contentPane;
  27.        
  28.         public Pantalla1() {
  29.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.             contentPane = new JPanel();
  31.             contentPane.setLayout(null);
  32.             setBounds(10,10,640,480);
  33.             setContentPane(contentPane);
  34.             JButton botonSalir = new JButton("SALIR");
  35.                 botonSalir.setBounds(500,300,100,50);
  36.                 botonSalir.setActionCommand("salir");
  37.                 botonSalir.addActionListener(this);
  38.                 contentPane.add(botonSalir);
  39.             JButton botonIncrementar = new JButton("SUMAR");
  40.                 botonIncrementar.setBounds(50,100,100,50);
  41.                 botonIncrementar.setActionCommand("incr1");
  42.                 botonIncrementar.addActionListener(this);
  43.                 contentPane.add(botonIncrementar);
  44.             JButton botonCambia1 = new JButton("CAMBIA");
  45.                 botonCambia1.setBounds(50,300,100,50);
  46.                 botonCambia1.setActionCommand("cambia2");
  47.                 botonCambia1.addActionListener(this);
  48.                 contentPane.add(botonCambia1);
  49.         }
  50.        
  51.         public void incrementa1() {
  52.             ++contador1;
  53.         }
  54.        
  55.         @Override
  56.         public void actionPerformed(ActionEvent arg1) {
  57.             if (arg1.getActionCommand().equals("salir")) {
  58.                 System.exit(0);}
  59.            
  60.             else if (arg1.getActionCommand().equals("incr1")) {
  61.                 incrementa1();
  62.                 System.out.println("El Contador1 acumula: "+contador1);}
  63.            
  64.             else if (arg1.getActionCommand().equals("cambia2")) {
  65.                 frame1.setVisible(false);
  66.                 frame2.setVisible(true);}
  67.         }
  68.     }
  69.    
  70.     public class Pantalla2 extends JFrame implements ActionListener {
  71.  
  72.         private JPanel contentPane;
  73.        
  74.         public Pantalla2() {
  75.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.             contentPane = new JPanel();
  77.             contentPane.setLayout(null);
  78.             setBounds(100,100,640,480);
  79.             setContentPane(contentPane);
  80.             JButton botonSalir = new JButton("SALIR");
  81.                 botonSalir.setBounds(500,300,100,50);
  82.                 botonSalir.setActionCommand("salir");
  83.                 botonSalir.addActionListener(this);
  84.                 contentPane.add(botonSalir);
  85.             JButton botonIncrementar = new JButton("SUMAR");
  86.                 botonIncrementar.setBounds(50,100,100,50);
  87.                 botonIncrementar.setActionCommand("incr2");
  88.                 botonIncrementar.addActionListener(this);
  89.                 contentPane.add(botonIncrementar);
  90.             JButton botonCambia2 = new JButton("CAMBIA");
  91.                 botonCambia2.setBounds(50,300,100,50);
  92.                 botonCambia2.setActionCommand("cambia1");
  93.                 botonCambia2.addActionListener(this);
  94.                 contentPane.add(botonCambia2);
  95.         }
  96.        
  97.         public void incrementa2() {
  98.             ++contador2;
  99.         }
  100.  
  101.         @Override
  102.         public void actionPerformed(ActionEvent arg2) {
  103.             if (arg2.getActionCommand().equals("salir")) {
  104.                 System.exit(0);}
  105.            
  106.             else if (arg2.getActionCommand().equals("incr2")) {
  107.                 incrementa2();
  108.                 System.out.println("El Contador2 acumula: "+contador2);}
  109.            
  110.             else if (arg2.getActionCommand().equals("cambia1")) {
  111.                 frame2.setVisible(false);
  112.                 frame1.setVisible(true);}
  113.         }
  114.     }
  115. }