将GUI链接到Java中的Main [英] Linking GUI to Main in Java

查看:319
本文介绍了将GUI链接到Java中的Main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java和OOP一般都很新,我想从我的Main类调用我的GUI类,这将是我的程序的起点。

I'm very new to Java and OOP in general and I want to call my GUI class from my Main class which will be the start point for my program.

这应该很简单,但任何帮助将感激。

This should be pretty simple but any help will be appreciated

GUIForm1代码:

GUIForm1 Code:

import javax.swing.*;
import java.awt.*;

public class GUIForm1 {

private JPanel MainPanel;
private JPanel MenuPanel;
Private JPanel AnimationPanel;
private JButton greenTrail;
private JButton purpleTrail;
private JSeparator animationMenuDivider;
private JSlider rangeSlider;
//more components

public GUIForm1() {


   JFrame frame = new JFrame("GUIForm1");
    frame.setContentPane(new GUIForm1().MainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.pack();
    frame.setVisible(true);

   }
}

主类代码:

public class ProjectileSim {

public static void main(String[] args){
    GUIForm1 gui = new GUIForm1();

}
}


推荐答案

您的代码很好。您的GUIII类的主类实例化也是正确的。您遇到问题的原因如下:

Your code is good. Your main class instantiation of the GUIForm class is also correct. The reason why you are having an issue is here:

 frame.setContentPane(new GUIForm1().MainPanel);






这是您在代码中的问题。你试图调用类中的类的构造函数来设置一个不存在的MainPanel。


That is your issue in the code. You are trying to call the constructor of your class within your class to set a MainPanel that is non-existent.

如果您使用的是NetBeans(或任何IDE),它足够简单,只需将JPanel拖动到GUI上,然后可以将框架内容框架设置为(例如):

If you are using NetBeans (Or any IDE) it is simple enough for you to just drag a JPanel onto your GUI and then you can set your frames content frame to that (for example):

frame.setContentPane(myNewJPanel);







Give it a go. Comment out the line where you are setting your contentframe and see what I mean.

(这是您如何注释掉的方式:

(this is how you comment out by the way :

//frame.setContentPane(new GUIForm1().MainPanel);

您只需在不想执行的行的前面插入2个斜杠。

You just insert 2 forward slashes in front of the line that you do not want to be executed.

知道结果。

这篇关于将GUI链接到Java中的Main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆