Why does Jframe Java not work in Eclipse

Eclipse: Programming Java GUI - this is how it works

Programming a GUI with Java in Eclipse is difficult for many beginners. We will 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:

  1. Open Eclipse (Version: Kepler Service Release 2) and click under "File" on "New" and then on "Java Project". Give your project a suitable name and click "Finish".
  2. Open your new project via the Package Explorer. Now click on "Create new visual classes" in the upper action bar. Hover your mouse over Swing and click JFrame. A new window opens, here you enter the name of the class.
  3. You have now created a "Visual Class" and can continue editing it.

Java GUI: change design

After you have created the "Visual Class", you can proceed with the design of your program:

  1. To do this, click on "Design" in the Eclipse editor.
  2. You will now see a classic window that you can equip with individual elements. For example, under "Components" you will find the "JButton".
  3. Click on any element and drag it into the program window. Note: With "Absolute layout" you can arrange the elements freely in the window.
  4. Give the elements unique names such as "btn1" for the first button. Caution: Do not confuse the name ("Variable") with the label ("Text").

Java programming: adding interaction

Let's assume you now have 2 text fields and a button. In text field 1 (here "txt1") you enter a number that is 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-in value to the variable "a" and multiply it by 2.
  • Finally, we output the result (variable "b") in text field 2. To do this, we use the "setText ()" method.

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 here 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.