Is there any method of calling a subroutine or function within a newly opened frame? Java

StackOverflow https://stackoverflow.com/questions/21667387

  •  09-10-2022
  •  | 
  •  

Question

I know how to call up one class from within another:

 new frame().setVisible(true);

But I don't know how you do this but call up a subroutine on that at the same time e.g

new frame().setVisible(true);
Sub123();
Was it helpful?

Solution

Assign the result of the new command to a variable and interact with it

frame aframe = new frame();
aframe.setVisible(true);
aframe.Sub123();

You may also consider taking a look at Code Conventions for the Java Programming Language

OTHER TIPS

You need to do less compression of your code. You're creating an object anomymously via:

new Frame().setVisible(true);

and so have no variable with which to call methods on your object.

so instead do:

MyFrame myFrame = new MyFrame();  // create a variable!
myFrame.setVisible(true);
myFrame.someMethod();    // use it!

As an aside, you will want to learn Java naming methods and abide by them, including starting class names with upper-case letters. Doing this will improve the readability of your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top