简体   繁体   中英

How do you add a jFrame to your main class in Netbeans?

So I have made a Jframe with a lot of elements and buttons and things in it, but I am new to using NetBeans. Upon creating the java application a main class.java was created and upon adding the jframe another jframe.java was created. How do I get the main class to open, read, and run my jframe.java? I can upload the specific code if need be.

Thanks in advance

To call a certain method from another class, you must first create a new object for that class, like this:

Jframe frame = new Jframe();
frame.setVisible(true); //or whatever the method is in jframe.class

Maybe rename the actual class name from jframe to something like frameone . I've heard that naming classes the same as classes in the Java API will cause trouble.

Or, you could put it all in one class, with either two separate methods or put it all in the main method. If this doesn't help, then please paste the exact code on pastebin.org and give a link.

Look at this sample example and learn how to set frame visible

import java.awt.*;
import javax.swing.*;
public class exp{  
    public static void main(String args[]){ 
        JFrame jf=new JFrame("This is JFrame");
        JPanel h=new JPanel();
        h.setSize(100,100);

        h.add(new JButton("Button"));
        h.add(new JLabel("this is JLabel"));
        h.setBackground(Color.RED);

        jf.add(h);
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);

    }  
}

Useful Links

  1. Designing a Swing GUI in NetBeans IDE
  2. Creating a GUI With Swing (As @MadProgrammer Commented)
  3. Learning Swing with the NetBeans IDE

I'm new to this, but I got a form up. Woo hoo!

1) The project created my main function in japp1.java
2) I created a JFrame, file jfMain.java
3) While there was probably a way to reference it as it was, I didn't see how right away, so I moved it to a peer level with the japp1 file, both in a folder called japp1 which will cause them to get built together, having the same parent reference available.

src\
    japp1\
        japp1.java
        jfMain.java

4) Then instead of creating a generic JFrame with a title, I created an instance of my class... 
5) I gave it a size... 
7) Then showed it...

public static void main(String[] args) {
    // TODO code application logic here
    JFrame frame = new japp1.jfMain();
    frame.setPreferredSize(new Dimension(700, 500));
    frame.pack();
    frame.setVisible(true);
}

I had already put some code in my jframe... to show a messagedialog with JOptionPane from a mouseclick event on a button and set some text for some textfields.

Hope that helps.

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