简体   繁体   中英

Java NetBeans pass value to JFrame produces a compilation error

There are two jFrames.

  1. FirstPage
  2. SecondPage

There is a button on FirstPage . When user clicks it, I need to open SecondPage .

This is the code in 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 :

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:

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:

/* 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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