I'm a freshman with all the GUI stuff and I want to know why do I have to declare all the JFrames with "static" in my code(otherwise it wont compile) and how can I modify my code to compile without declaring them "static"?
public class Ventana extends JFrame {
private static JFrame frameInicio = new JFrame("Ingresar Datos");
private static JFrame frameCrearUsuario = new JFrame("Crear Usuario");
private static JFrame frameMenu = new JFrame("Menu Del Juego");
private static JFrame frameJuego = new JFrame("El Ahorcado");
public static void main(String[] args) {
Inicio();
}
public static void Inicio(){
frameCrearUsuario.dispose();
frameMenu.dispose();
frameInicio.setSize(450,200);
frameInicio.setLocationRelativeTo(null);
frameInicio.add(new FrameInicio());
frameInicio.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameInicio.setVisible(true);
}
public void CrearUsuario(){
frameInicio.dispose();
frameCrearUsuario.setSize(450,300);
frameCrearUsuario.setLocationRelativeTo(null);
frameCrearUsuario.add(new FrameCrearUsuario());
frameCrearUsuario.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameCrearUsuario.setVisible(true);
}
public void Menu(){
frameInicio.dispose();
frameJuego.dispose();
frameMenu.setSize(660,290);
frameMenu.setLocationRelativeTo(null);
frameMenu.add(new FrameMenu());
frameMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameMenu.setVisible(true);
}
public void Juego(){
frameMenu.dispose();
frameJuego.setSize(1100,800);
frameJuego.setLocationRelativeTo(null);
frameJuego.add(new FrameJuego());
frameJuego.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameJuego.validate();
frameJuego.setVisible(true);
}
It's becuase your public static void Inicio()
is static. You can't call a non static property from a static
method
Here's a common way to use a JFrame
. Create a constructor for the JFrame and add all the components inside of it. Then just Create the JFrame object in the main
method
public class Ventana extends JFrame {
JPanel panel = new JPanel();
JButton button = new JButton("button");
JTextArea jta = new JTextArea();
public Vantan(){
add(panel);
add(button);
add(jta);
}
public static void main(String[] args){
JFrame frame = new Ventana();
frame.setTitle("Frame);
frame.setSize(500, 500);
frame.pack();
frame.setDefaulCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This way you don't need the static properties as in your case.
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.