Banned Banned - 7 months ago 66
Java Question

how to call a public class to display gui in java using jformdesigner

I wish to display my gui in the main method however it doesn't seem to do so...
I've used the suggestion here:
jformdesigner design it won't display?

But that did not work,

My error at the moment is that eclipse is suggesting I need to create a method called setDefaultCloseOperation which is already defined in the class and the same for the setvisible.

"The method setDefaultCloseOperation(int) is undefined for the type bmicalc

The method setVisible(boolean) is undefined for the type bmicalc"

main method:

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class iu {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
bmicalc GUI = new bmicalc();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
class bmicalc extends JFrame{

public bmicalc() {
initComponents();

}
private void initComponents() {
JFrame bmiCalculatorFrame = new JFrame();
{
bmiCalculatorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
bmiCalculatorFrame.setTitle("BMI Calculator");
Container bmiCalculatorFrameContentPane = bmiCalculatorFrame.getContentPane();
bmiCalculatorFrameContentPane.setLayout(new GridLayout());
}
}}}}

Answer

Your bmicalc class should extend JFrame, but it is not. setDefaultCloseOperation and setVisible methods belong to JFrame.

Also, it is not very clear, but it looks like the JFormDesigner generated another JFrame.

EDIT:

Here is a example of JFrame generated by JFormDesigner:

public class TestFrame extends JFrame {
     public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    TestFrame frame = new TestFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
     }

    public TestFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new FormLayout(
            "default",
            "default"));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

EDIT - according to the last question edit

To address your compilation issues see the snippet below. However, it is not clear what are you trying to achieve with JFrame bmiCalculatorFrame.

class bmicalc extends JFrame{

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            bmicalc GUI = new bmicalc();
            GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GUI.setVisible(true);
        }
    });
}

public bmicalc() {
    initComponents();

}

private void initComponents() {
    //............
}
}