+ 2

how can i summon a class in annother file

this is the package package Los_señore; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import javax.swing.JFrame; public class Juego extends Canvas{ private static final long serialVersionUID = 1L; private static final int ANCHO = 800; private static final int ALTO = 600; private static final String NOMBRE = "Juego"; private static JFrame ventana; private Juego(){ setPreferredSize(new Dimension(ANCHO, ALTO)); ventana = new JFrame(NOMBRE); ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ventana.setResizable(false); ventana.setLayout(new BorderLayout()); ventana.add(this, BorderLayout.CENTER); ventana.pack(); ventana.setLocationRelativeTo(null); ventana.setVisible(true); } } this is the main class on another file import Los_señore.Juego; public static void main(String[] args){ Juego pepe = new Juego } this doesnt work help plz

24th Feb 2017, 8:33 PM
Ismael Perbech
Ismael Perbech - avatar
23 odpowiedzi
+ 5
No problem, nice you're telling us ^^
24th Feb 2017, 10:52 PM
Tashi N
Tashi N - avatar
+ 5
Could you test if this very simple example is working? public class Juego { public Juego(){ System.out.print("yes"); } public static void main(String[] args){ Juego pepe = new Juego(); } }
25th Feb 2017, 3:40 PM
Tashi N
Tashi N - avatar
+ 4
Which error does your ide throw?
24th Feb 2017, 8:42 PM
Tashi N
Tashi N - avatar
+ 4
class declaration of the class which contains the main is missing. class myClass { public static void main(String[] args){ Juego pepe = new Juego(); } }
24th Feb 2017, 8:48 PM
Tashi N
Tashi N - avatar
+ 4
Is there a new error message? There must be one if the code you posted is complete... Your constructor for Juego must be public. And the next one will be the missing method you call there.
24th Feb 2017, 9:41 PM
Tashi N
Tashi N - avatar
+ 4
Please post the edited version of your code or publish it at the code playground.
24th Feb 2017, 10:00 PM
Tashi N
Tashi N - avatar
+ 4
The constructor must be public.
25th Feb 2017, 3:09 PM
Tashi N
Tashi N - avatar
+ 4
Only if you create another object. I don't think that will help you here. What do you need another canvas for?
25th Feb 2017, 3:17 PM
Tashi N
Tashi N - avatar
+ 4
I think it will help you to start over again and use this example. Just copy the code, try to understand the tutorial and extend the example then. http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
25th Feb 2017, 3:20 PM
Tashi N
Tashi N - avatar
+ 1
1. When something doesn't work, please specify what you expected to happen, what happens instead, and include any errors. 2. When creating objects, you need to use parentheses: Juego pepe = new Juego()
24th Feb 2017, 8:44 PM
Igor B
Igor B - avatar
+ 1
this is the final working code thnx import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import javax.swing.JFrame; public class window extends Canvas{ private static final long serialVersionUID = 1L; private static final int ANCHO = 1000; private static final int ALTO = 800; private static final String NOMBRE = "Juego"; private static JFrame ventana; private void juego(){ setPreferredSize(new Dimension(ANCHO, ALTO)); ventana = new JFrame(NOMBRE); ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ventana.setResizable(false); ventana.setLayout(new BorderLayout()); ventana.add(this, BorderLayout.CENTER); ventana.pack(); ventana.setLocationRelativeTo(null); ventana.setVisible(true); } public static void main(String[] args){ window hola=new window(); hola.juego(); } }
25th Feb 2017, 4:17 PM
Ismael Perbech
Ismael Perbech - avatar
0
public static void main ^ expected class enum or interface
24th Feb 2017, 8:45 PM
Ismael Perbech
Ismael Perbech - avatar
0
doesnt work
24th Feb 2017, 8:46 PM
Ismael Perbech
Ismael Perbech - avatar
0
now the class main dont execute
24th Feb 2017, 8:56 PM
Ismael Perbech
Ismael Perbech - avatar
0
not charging the principal class
24th Feb 2017, 9:57 PM
Ismael Perbech
Ismael Perbech - avatar
0
i will do tomorrow
24th Feb 2017, 10:31 PM
Ismael Perbech
Ismael Perbech - avatar
0
sorry
24th Feb 2017, 10:31 PM
Ismael Perbech
Ismael Perbech - avatar
0
im here my code error now is private acces juego but i need to make private how can i call this is the code now package Los_señore; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import javax.swing.JFrame; public class Juego extends Canvas{ private static final long serialVersionUID = 1L; private static final int ANCHO = 800; private static final int ALTO = 600; private static final String NOMBRE = "Juego"; private static JFrame ventana; private Juego(){ setPreferredSize(new Dimension(ANCHO, ALTO)); ventana = new JFrame(NOMBRE); ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ventana.setResizable(false); ventana.setLayout(new BorderLayout()); ventana.add(this, BorderLayout.CENTER); ventana.pack(); ventana.setLocationRelativeTo(null); ventana.setVisible(true); } } and the main class is in this file import Los_señore.Juego; class prueba{ public static void main(String[] args){ Juego pepe = new Juego(); } } the directory los_señore exist and the file juego.class is inside
25th Feb 2017, 2:39 PM
Ismael Perbech
Ismael Perbech - avatar
0
okey then
25th Feb 2017, 3:11 PM
Ismael Perbech
Ismael Perbech - avatar
0
can i call a constructor if already is created
25th Feb 2017, 3:11 PM
Ismael Perbech
Ismael Perbech - avatar