Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans JFrame initialization; build is fine, but no window is made

I'm using Java, and I am trying to create a GUI with NetBeans. I've done this before, and I'm very puzzled because my code, while NetBeans doesn't give errors, will not produce a new JFrame window when I run it in NetBeans. However, the code that initializes the JFrame is essentially identical to my previous GUI-possessing program ("program one"). When I try running "program one", it works just fine. Here is my problem code;

package aircannoncalculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalcGUI extends JFrame {

public CalcGUI(){
    setTitle("Air Cannon Modeler");
    setSize(400,400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args){

        CalcGUI gui = new CalcGUI();
        gui.setVisible(true);

}
}

According to NetBeans, the build always goes fine, but as I said, no actual window is produced. What am I doing wrong?

Side note; ignore my gratuitous import list.

like image 209
TheMike25 Avatar asked Apr 05 '13 00:04

TheMike25


2 Answers

You have to set the JFrame as your project's main class. Right click the name of the project(coffee cup icon) -> set configuration -> customize -> in the section 'Run' click on the Browse button to the right of "Main Class:" to select the default Main Class (your desired JFrame), done!

like image 51
Gabriel Avatar answered Sep 19 '22 14:09

Gabriel


package aircannoncalculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalcGUI extends JFrame {

    public CalcGUI(){
        setTitle("Air Cannon Modeler");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args){

        CalcGUI gui = new CalcGUI();

        //Try adding some JComponents
        this.pack();    //this tends to compact the JFrame container & displays it when you setVisible(true)

        gui.setVisible(true);


    }
}
like image 45
ANTi7kZ Avatar answered Sep 22 '22 14:09

ANTi7kZ