简体   繁体   中英

Jframe is not showing up

I'm having problems creating a JFrame in my application, so I tried to run the simplest code I could in order to locate what was happening.

public class prueba {
     public static void main(String[] args) {
       JFrame f = new JFrame("Primer JFrame");
        f.add(new JLabel("Intenteu tancar per la creueta..."));

        f.setSize(800,600);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

I'm astounded, this does nothing, but this code is re-used from another project in which everything goes OK. This doesen't create the JFrame and I only get this in the console as response:

C:\\Users\\Ivan\\AppData\\Local\\NetBeans\\Cache\\8.2\\executor-snippets\\run.xml:53: Java returned: -1073740791

I got an error when running this program also:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: EXIT_ON_CLOSE cannot be resolved to a variable

But when I deleted this line:

f.setDefaultCloseOperation(EXIT_ON_CLOSE);

It ran fine, but when the window was exited, the program did not quit right away. So you can either delete this line, or if you want the program to terminate right after window is closed, extend JFrame, and use a constructor, like this:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class prueba extends JFrame {
    private static final long serialVersionUID = 1L;

    public prueba() {
        add(new JLabel("Intenteu tancar per la creueta..."));
        setTitle("Primer JFrame");
        setSize(800,600);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public static void main(String[] args) {
        new prueba();
     }
 }

I get issues like these in Netbeans all the time. (Frankly, I'm considering switching IDEs). There's no guarantee if these suggestions will work, but it's worth a shot. One of these usually fixes my issues.

  1. The usual solution is to clean and build your project, as sometimes Netbeans will compile older code instead. Do this by clicking on "Run", then "Clean and build main project", then try running it again.

  2. Sometimes a simple close-and-reboot Netbeans will fix this issue.

  3. If neither of the above two work, I just make a new class and copy/paste the code into it, then delete the older class. This is usually a "hard-fix" for me, although it can be tedious.

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