简体   繁体   English

如何在Netbeans的主类中添加jFrame?

[英]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. 因此,我制作了一个包含很多元素和按钮以及其中内容的Jframe,但是我对使用NetBeans并不陌生。 Upon creating the java application a main class.java was created and upon adding the jframe another jframe.java was created. 创建Java应用程序后,将创建一个主类.java,并添加了jframe后,将创建另一个jframe.java。 How do I get the main class to open, read, and run my jframe.java? 如何获得主类来打开,阅读和运行我的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 . 也许将实际的类名从jframe重命名为frameone I've heard that naming classes the same as classes in the Java API will cause trouble. 我听说,将类命名为与Java API中的类相同会引起麻烦。

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. 如果这样做没有帮助,请将确切的代码粘贴到pastebin.org上并提供链接。

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 在NetBeans IDE中设计Swing GUI
  2. Creating a GUI With Swing (As @MadProgrammer Commented) 使用Swing创建GUI (如@MadProgrammer注释)
  3. Learning Swing with the NetBeans IDE 使用NetBeans IDE学习Swing

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. 我已经在我的jframe中放了一些代码...以通过按钮上的mouseclick事件显示带有JOptionPane的messagedialog,并为某些文本字段设置了一些文本。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM