Java 如何将 jFrame 添加到 Netbeans 的主类中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21591807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:33:00  来源:igfitidea点击:

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

javaswingnetbeans

提问by user3277602

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.

所以我制作了一个 Jframe,里面有很多元素、按钮和东西,但我是使用 NetBeans 的新手。在创建 java 应用程序时,创建了一个主 class.java,并在添加 jframe 时创建了另一个 jframe.java。如何让主类打开、读取和运行我的 jframe.java?如果需要,我可以上传特定的代码。

Thanks in advance

提前致谢

回答by Sean Jordan

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 jframeto something like frameone. I've heard that naming classes the same as classes in the Java API will cause trouble.

实际的类名重命名也许从JFrame中为类似frameone。我听说将类命名为与 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.

或者,您可以将它们全部放在一个类中,使用两个单独的方法或将它们全部放在 main 方法中。如果这没有帮助,请在 pastebin.org 上粘贴确切的代码并提供链接。

回答by ravibagul91

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
  1. 在 NetBeans IDE 中设计 Swing GUI
  2. 使用 Swing 创建 GUI(正如@MadProgrammer 评论的那样)
  3. 使用 NetBeans IDE 学习 Swing

回答by Dan K

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 中放入了一些代码......以从按钮上的鼠标单击事件显示带有 JOptionPane 的消息对话框,并为某些文本字段设置一些文本。

Hope that helps.

希望有帮助。