简体   繁体   English

Java-如何添加将使用初始类的gui

[英]Java - how to add gui which will make use of initial classes

I've written a standard console application (web scraping) with several classes. 我用几个类编写了一个标准的控制台应用程序(网络抓取)。 The only thing that main fucntion does, is: - create one instance of a class - call one method from created object 主要功能唯一要做的是:-创建一个类的实例-从创建的对象中调用一个方法

Now I need to add the simplest GUI which will contain one button to perform second of those aforementioned actions (call method) and textarea to display everything that NetBeans output shows now. 现在,我需要添加最简单的GUI,其中将包含一个按钮来执行上述动作(调用方法)中的第二个按钮和文本区域,以显示NetBeans输出现在显示的所有内容。

I created new file with GUI class. 我使用GUI类创建了新文件。 It's basically a jFrame with jButton and jTextArea. 它基本上是一个带有jButton和jTextArea的jFrame。 I managed to get text output to work like I assumed. 我设法使文本输出正常工作。 However I have no idea how to set the button. 但是我不知道如何设置按钮。 Netbeans creator redirects me to this part of code: Netbeans创建者将我重定向到这部分代码:

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:

    p1.use(); // <<< my initial try
}

where p1 is object created in main class in original file. 其中p1是在原始文件的主类中创建的对象。 Netbeans marks "p1" as "cannot find symbol" which is understandable. Netbeans将“ p1”标记为“找不到符号”,这是可以理解的。 However, how can I perform such a simple operation? 但是,如何执行这种简单的操作? I basically need that Button to push the program forward. 我基本上需要那个Button来推动程序前进。

You need to pass the p1 object from your main class into the class for your GUI through a constructor or setter, otherwise it doesn't know what you're trying to access. 您需要通过构造函数或设置器将p1对象从主类传递到GUI的类中,否则它将不知道您要访问的内容。

For example, 例如,

public class MainClass {
    private MyGuiClass gui;
    private P1Class    p1;

    public static void main(String[] args) {
        p1  = new P1Class( /*arguments*/ );
        gui = new MyGuiClass( p1, /*other arguments*/ );
    }
    // Other logic...
}

public class MyGuiClass extends JFrame {
    private P1Class p1;
    public MyGuiClass( P1Class p1, /*other arguments*/ ) {
        this.p1 = p1;
    }
    // Other logic...
}

At this point, you can refer to the p1 object in the rest of your code. 此时,您可以在其余代码中引用p1对象。

There are many ways to achieve that. 有很多方法可以实现这一目标。

Scenario 1 场景1

Make the object p1 accessible to the jFrame object by making it static in the Main class. 通过在Main类中使其成为静态对象,使jFrame对象可以访问对象p1。

public class Main {
    public static P1 p1;

    public static void main(String[] args) {
        p1 = new P1();
        //open window etc.
    }
}

public class MyFrame extends JFrame {
    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
        // Access object by static reference
        Main.p1.use();
    }
}

Scenario 2 方案2

You can pass the object P1 as a parameter to the constructor of your jFrame object. 您可以将对象P1作为参数传递给jFrame对象的构造函数。

public class Main {
    public static void main(String[] args) {
        P1 p1 = new P1();
        new MyFrame(p1);
        //open window etc.
    }
}

public class MyFrame extends JFrame {
    private P1 p1;

    public MyFrame(P1 param) {
        // Save object inside the JFrame object
        this.p1 = param;
    }

    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
        p1.use();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM