Tuesday, July 28, 2009

How do I use an existing Java class in a GUI?

I am trying to use an existing Java class in a GUI. I have the frame set up, but I can not figure out how to display the needed Java class in the frame. Thanks for the help!

How do I use an existing Java class in a GUI?
JFrame has automatic add() and automatic BorderLayout(). So, keep your JFrame class.





The other GUI should be a JPanel as the easiest solution. Then, it is ...





class MyDisplayPanel


extends DisplayPanel() {


//





If the original DisplayPanel has anything in its constructor, you have to provide that also.





class DisplayPanel() {


int[] myNumbers;





pubilc DisplayPanel( int[] nums ) {


myNumbers = nums;





}


Then, in the class MyDisplay...





class MyDisplay extends Display {





public MyDisplay( int [] intArray ) {


super(intArray);


}





If the class with the GUI is already a JFrame, you have a little more work. You will have to overwrite the method that adds the GUI or rewrite the code entirely.


No comments:

Post a Comment