Eclipse: program Java GUI - how it works
Programming a GUI with Java in Eclipse is difficult for many beginners. We give you tips and show you how to give your program a face.
Eclipse: Create a GUI project
GUI stands for Graphical User Interface and means the graphical user interface of a program such as buttons, text fields and other elements. Read here how to start a suitable project in Eclipse:
- Open Eclipse (Version: Kepler Service Release 2) and under "File" click on "New" and then on "Java Project". Give your project a suitable name and click on "Finish".
- Open your new project using the Package Explorer. Now click on "Create new visual classes" in the top action bar. Move the mouse over "Swing" and click on "JFrame". A new window opens, here you enter the name of the class.
- Now you have created a "Visual Class" and can continue to edit it.
Java GUI: change design
After you have created the "Visual Class", you can continue with the design of your program:
- To do this, click on "Design" in the Eclipse editor.
- You now see a classic window that you can equip with individual elements. For example, under "Components" you will find the "JButton".
- Click on any element and drag it into the program window. Note: With "Absolute layout" you can arrange the elements freely in the window.
- Give the elements unique names such as "btn1" for the first button. Attention: Do not confuse the name ("Variable") with the label ("Text").
Java programming: add interaction
Let's say you now have 2 text fields and a button. In text box 1 (here "txt1"), enter a number to be multiplied by 2. The result is output in text field 2 (here "txt2").
- Double click on your button (here "btn1"). You are now back in the editor.
- With the method "getText ()" you can read the input from the first text field. Since you need an Integer data type, convert the string data type with "Integer.parseInt".
- Now assign the read value to the variable "a" and multiply this by 2.
- Finally, we output the result (variable "b") in text field 2. For this we use the method "setText ()".
CHIP job market
In our CHIP job market you will find thousands of current jobs in the digital world. Whether as a software developer or marketing expert, at Microsoft or Amazon - we cover the entire breadth of the German tech world. And if you want to start one of these jobs as a career changer, distance learning is worthwhile. Request an information brochure about the current distance learning courses from ILS, Germany's largest distance learning school.
If you want to learn more about Java, you will find a tutorial on the While and For loops in our practical tip.