Adding image to borderlayout CENTER
I have a class Background that extends JPanel. The point of this class is that contains an image that is displayed on it.
I also have a class GraphTest that extends JFrame (this is the programs main window). In this frame, I have a menu with the option "new". When I press new, I am supposed to get a filechooser window where I can select a file. When I select a file, I want to create a Background object that contains the very same file as the image to display, and add the Background object to the Graphtests borderlayout CENTER.
How should I do this?
I am thinking along the lines of writing the Background cla开发者_开发技巧ss so that the constructor for it takes a String, which is the images filename.
So when the listener for the "new" button in Graphtest is pressed, it opens a filechooser that returns the filename of the selected image and sends that into the constructor for Background, creating a "Background bg = new Background(filechooser.filename.toString)" (i know that is not how to write it, but you get the idea) and then adds that object to the GraphTest borderlayout CENTER.
Is this a nice solution? Or would there be better ways of doing this?
Here is my BackgroundPanel class. I choose to pass in the image to be used as the background. But there is no reason you couldn't pass in the filename and then create the image. Having the option to pass in the image directly or the filename just makes the class more convenient.
精彩评论