Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a GUI to existing Java console based program

I've been working on a console based program that acts as an inventory of Plant objects.

I have a parent class "Plant" that has child classes of "Flower", "Weed", etc... These objects are added, removed, displayed, searched through another class containing the main method and methods for the actions above.

The methods/actions are chosen by the user via console input processed with a switch statement.

My question is this: We are adding a GUI to this console based program using a JFrame, JPanels, etc... Would the proper way to go about this be to create a new class for the interface and a new main method in that class to run the program? I would of course change the former main method to a method called by the new main.

like image 502
imnotmarvin Avatar asked Feb 14 '23 05:02

imnotmarvin


1 Answers

Moving from a console program requires a lot more than just changing main methods. GUI programs are event driven. So you're not going to be running endless loops like you would in a console program.

What I mean by event driven is, for example, a button get pressed, an event is fired. You as the programmer are responsible for coding what happens when that event is fired.

So some advice.

  • You should go through the tutorials and learn some of the basic components and how they work. Some of the basic ones are JLabel, JTextField, JButton

  • You will definitely need to focus on how to write event listeners. Some of the basic ones you may want to focus on are ActionListener for button presses MouseListener for mouse events.

  • Should learn to layout out components correctly. Some of the basic layouts you may want to focus on are GridLayout, BorderLayout, and FlowLayout

  • You want to learn about the basic containers like JFrame and JPanel and learn their capabilities

The Swing tutorials are always a good place to start. Once you pick up on the basics, then move on to some more complex material.

like image 193
Paul Samsotha Avatar answered Feb 15 '23 17:02

Paul Samsotha