簡體   English   中英

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

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

因此,我制作了一個包含很多元素和按鈕以及其中內容的Jframe,但是我對使用NetBeans並不陌生。 創建Java應用程序后,將創建一個主類.java,並添加了jframe后,將創建另一個jframe.java。 如何獲得主類來打開,閱讀和運行我的jframe.java? 如果需要,我可以上傳特定代碼。

提前致謝

要從另一個類調用某個方法,必須首先為該類創建一個新對象,如下所示:

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

也許將實際的類名從jframe重命名為frameone 我聽說,將類命名為與Java API中的類相同會引起麻煩。

或者,您可以將它們全部放在一個類中,可以使用兩個單獨的方法,也可以全部放在主方法中。 如果這樣做沒有幫助,請將確切的代碼粘貼到pastebin.org上並提供鏈接。

查看此示例示例,了解如何設置框架可見

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. 在NetBeans IDE中設計Swing GUI
  2. 使用Swing創建GUI (如@MadProgrammer注釋)
  3. 使用NetBeans IDE學習Swing

我對此並不陌生,但我已經提出了要求。 嗚嗚!

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的messagedialog,並為某些文本字段設置了一些文本。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM