How to create a GUI in Java

I used Java for a while, but never created a GUI - always a CLI. How to create a GUI in Java? Can you offer a good tutorial / link?

I want to create a simple GUI with two long text areas and a few buttons.

+8
source share
7 answers

Here is a simple example.

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; public class Foo{ public static void main(String[] args) { JFrame f = new JFrame("A JFrame"); f.setSize(250, 250); f.setLocation(300,200); final JTextArea textArea = new JTextArea(10, 40); f.getContentPane().add(BorderLayout.CENTER, textArea); final JButton button = new JButton("Click Me"); f.getContentPane().add(BorderLayout.SOUTH, button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textArea.append("Button was clicked\n"); } }); f.setVisible(true); } } 
+9
source

Read Swing on the Oracle Tutorial Pages.

+6
source

Well, what you need to do is create a thin text / code (Note Pad ++ or Notepad) and remember the name.

Then open its type

 import java.awt.*; import javax.swing.*; 

This basically suggests that java gets java.awt and javax.swing from its various code libraries that come with java when it is loaded (understandably, since this is a language that helps developers).

Then you need to make your own function, which will have everything from size, text inside, color, etc. REMEBER, we do not code gui here, as it was already done when we imported the import java.awt.*; and javax.swing.*

When I put the public class work , I work on the name of my file (if it was called by code, it would be public class code .

 public class work { private static void createWindow() { //Create and set up the window JFrame frame = new JFrame ("simple GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel textLabel = new JLabel("Im cool" ,SwingConstants.CENTER); textLabel.setPreferredSize(new Dimension(300, 100)); frame.getContentPane().add(textLabel, BorderLayout.CENTER); //Display the window frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); 

Remember that you didn’t call a function anywhere, you basically expected it there until it was called.

 public static void main(String[] args) { 

This tells the computer what it will someday be when you run the program.

So, inside this you need to put

 createWindow(); 

Since this is what you called your function above, and it calls the function, you do not need to call this function as it is when the program starts.

+2
source

You have different options here, but I would recommend using Swing with an IDE like Netbeans, which provides a very good WYSIWYG editor (Matisse).

Netbeans also has project templates that you can use to quickly launch.

Finally, as others have pointed out, be sure to do your homework and read the Swing beginner's tutorials.

+1
source
+1
source

Creating a JFrame is not as complicated as people think, all you need is a definition class and a GUI class. This is one of the easiest things in Java.

Definition Class:

 public class GetMyJavaWindow { public static void main (String args[]) { new JavaWindow(); } } 

JFrame Class:

 import javax.swing.JFrame; public class JavaWindow { // Class name must match what it says in the def. class public static final long serialVersionUID = 1L; // Needed for the JFrame to work. public JavaWindow() { // Must match class name this.setVisible(true); // Required this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Required this.setSize(800,600); // this.setSize (x,y); } } 

If you need more help with the GUI, come to me.

+1
source

Source: https://habr.com/ru/post/1341308/


All Articles