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.
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!
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With