![](/img/trans.png)
[英]how to link a main class to a jframe form in java using netbeans
[英]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) 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.