有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Netbeans中向主类添加jFrame?

所以我制作了一个包含很多元素、按钮和东西的Jframe,但我对使用NetBeans还不熟悉。在创建java应用程序时,将创建一个主类。创建了java,并在添加jframe后创建了另一个jframe。java被创建了。如何让主类打开、阅读和运行jframe。JAVA我可以上传具体的代码,如果需要的话

提前谢谢


共 (3) 个答案

  1. # 1 楼答案

    我是新手,但我有一张表格。呜呜

    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中添加了一些代码。。。要从按钮上的鼠标单击事件显示带有JOptionPane的messagedialog,并为某些文本字段设置一些文本

    希望有帮助

  2. # 2 楼答案

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

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

    可能将实际类名从jframe重命名为类似frameone的名称。我听说,将类命名为与JavaAPI中的类相同的名称会带来麻烦

    或者,您可以将其全部放在一个类中,使用两个单独的方法或将其全部放在主方法中如果这没有帮助,请将准确的代码粘贴到pastebin上。组织并提供一个链接

  3. # 3 楼答案

    看看这个示例,了解如何设置框架可见

    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