简体   繁体   English

Java NetBeans将值传递给JFrame会产生编译错误

[英]Java NetBeans pass value to JFrame produces a compilation error

There are two jFrames. 有两个jFrame。

  1. FirstPage 第一页
  2. SecondPage 第二页

There is a button on FirstPage . FirstPage上有一个按钮。 When user clicks it, I need to open SecondPage . 当用户单击它时,我需要打开SecondPage

This is the code in FirstPage : 这是FirstPage的代码:

private void btn_testActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String testName="Damith";
    SecondFrame win1=new SecondFrame(testName);
    win1.setVisible(true);
} 

This is how I modify SecondPage : 这就是我修改SecondPage

public SecondFrame(String anyname) {
    initComponents();

}

When I run the project it says: 当我运行项目时,它说:

One or more projects were complied with error 一个或多个项目符合错误

However, when I click "Run Anyway" it works as I expected. 但是,当我单击“仍然运行”时,它会按预期工作。

So, why they said "One or more projects were complied with error"? 那么,为什么他们说“一个或多个项目有错误”?

I see that you modified the default constructor that NetBeans generated for you to: 我看到您将NetBeans为您生成的默认构造函数修改为:

public SecondFrame(String anyname) {
    initComponents();

}

This means that if you replaced (instead of adding) the above with the default constructor that NetBeans generated for you, you caused a compilation error, as the auto-generated code from NetBeans invokes the default constructor that it generated, not the one that you explicitly created: 这意味着,如果用NetBeans为您生成的默认构造函数替换(而不是添加)上述内容,则会导致编译错误,因为从NetBeans自动生成的代码将调用其生成的默认构造函数,而不是您生成的默认构造函数明确创建:

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        new SecondFrame().setVisible(true);
    }
});

So, if the above assumption is correct (and it's the only one, one could make, with the details you provided), the following line is the error's cause: 因此,如果上述假设是正确的(使用您提供的详细信息,这是唯一可以做出的假设),则以下行是导致错误的原因:

new SecondFrame().setVisible(true);

If not, just hover your mouse over the red sign of the exact line on your editor to let the compiler inform you about the specific error. 如果不是,只需将鼠标悬停在编辑器上确切行的红色符号上,以使编译器通知您有关特定错误的信息。

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

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