简体   繁体   中英

In Eclipse I'm using JFrame but no Window is appearing

I started a new project, all the code is right (I think) and no window is appearing. There are no compilation errors, whenever I run the program nothing happens.

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

public class Frame extends JFrame{

    public static String title = "Tower Defense";  
    public static Dimension size = new Dimension(700, 550);  

    public static void main(String args[]){ 

        Frame frame = new Frame(); 
    }

    public Frame()  { 

        setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void init(){

        setVisible(true);
    }
}

You never call init() method. How can your frame be visible?

Just change your main method to:

public static void main(String args[]){ 

    Frame frame = new Frame(); 
    frame.init();
}

You never make a call to init() in your frame constructor:

public Frame() { 
    setTitle(title);
    setSize(size);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    **init();**
}

init() method will never be called in your program.

Set the visibility in your Frame() Constructor itself.

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

public class Frame extends JFrame{

public static String title = "Tower Defense";  
public static Dimension size = new Dimension(700, 550);  

public static void main(String args[]){ 

    Frame frame = new Frame(); 
}

public Frame(){

    setTitle(title);
    setSize(size);
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

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