【问题标题】:How do you add a jFrame to your main class in Netbeans?如何将 jFrame 添加到 Netbeans 的主类中?
【发布时间】:2014-02-06 00:46:38
【问题描述】:

所以我制作了一个 Jframe,其中包含很多元素、按钮和其他东西,但我对使用 NetBeans 很陌生。在创建 java 应用程序时,创建了一个 main class.java,并在添加 jframe 时创建了另一个 jframe.java。如何让主类打开、读取和运行我的 jframe.java?如果需要,我可以上传具体代码。

提前致谢

【问题讨论】:

  • 又一个“摇尾巴”的案例。在开始使用 Netbeans 提供的 GUI 构建器之前,了解如何手动编写 GUI。它们是出色的生产力工具,但在您了解如何使用普通的旧 Java 代码制作 GUI 之前毫无用处。

标签: java swing netbeans


【解决方案1】:

要从另一个类调用某个方法,您必须首先为该类创建一个新对象,如下所示:

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

也许将实际的类名从 jframe 重命名为 frameone。我听说将类命名为与 Java API 中的类相同会引起麻烦。

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

【讨论】:

  • "如果这没有帮助,请在 pastebin.org 上粘贴确切的代码并提供链接。" 我投反对票'到那个。 1)外部链接过时。 2) 许多人不能或不会关注链接。 3) 它鼓励人们返回类似“这里是链接到使用的4个源文件的内容。” -- 最好发布MCTaRE(最小的完整测试和可读示例)作为edit to the question
【解决方案2】:

查看这个示例并学习如何设置框架可见

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);

    }  
}

有用的链接

  1. Designing a Swing GUI in NetBeans IDE
  2. Creating a GUI With Swing(正如@MadProgrammer 评论的那样)
  3. Learning Swing with the NetBeans IDE

【讨论】:

    【解决方案3】:

    我是新来的,但我有一个表格。呜呼!

    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);
    }
    

    我已经在我的 jframe 中放了一些代码...从按钮上的 mouseclick 事件中显示带有 JOptionPane 的消息对话框,并为一些文本字段设置一些文本。

    希望对您有所帮助。

    【讨论】:

    • 移动它看起来破坏了 GUI builder 对它的进一步编辑,所以稍后我会弄清楚如何在不移动它的情况下引用它。
    • 顺便说一句,位置很好,我只是在错误的地方创建了我的表单类文件。在正确的位置重新创建表单后,它的工作方式相同,我不需要手动将它移动到任何地方的第 3 步。重要的是要了解 IDE 对其所有生成的代码做了什么,以及如何很好地使用 IDE 以免弄乱事情。 :) 我在想,大量备份(可能是整个项目的备份)是一个好主意,而你正在学习。
    • 现在最后一件事,这有点好。我将表单居中,但我有 2 个监视器,所以我在调用 setVisible 之前想出了这个(因为我的框架在我所在的任何屏幕上逻辑上显示为 0,0)... Dimension dim = Toolkit.getDefaultToolkit() .getScreenSize(); int x = dim.width/2-frame.getSize().width/2; int y = dim.height/2-frame.getSize().height/2; frame.setLocation(frame.getLocation().x + x, frame.getLocation().y + y);
    • 另一个注意事项,我将新逻辑的类型更改为... japp1.jfMain frame = new japp1.jfMain();这允许我在显示 jframe frame.setVisible(true); 之后调用我在名为 mainLoad 的 jframe 类中创建的公共 rtn;框架.mainLoad();
    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多