Conteúdo do Curso
Advanced Java 2077
Advanced Java 2077
Creating Simple GUI Applications
In this chapter, we will learn how to create a simple GUI application using the elements we have discussed so far, such as buttons, labels, text fields, and event listeners.
Step 1: Create a New Project
The first step in creating a GUI application is to create a new project in your preferred IDE. For this example, we will use Eclipse as our IDE. To create a new project in Eclipse, follow these steps:
- Select File > New > Java Project.
- Enter a name for your project and click Next.
- Select Create a project in workspace and click Finish.
Step 2: Create a JFrame
The next step is to create a JFrame
, which is the main window of your GUI application. To create a JFrame
in Java Swing, you can use the JFrame
class, like this.
Main
// This package is employed to run code on Codefinity. package com.example; // It is not required to be written in typical IDEs. import javax.swing.JFrame; public class MyFrame extends JFrame { public MyFrame() { super("My Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }
This code creates a new JFrame
called "My Frame"
with a size of 400x300 pixels. The setDefaultCloseOperation()
method sets the default close operation for the JFrame
, and the setVisible()
method makes the JFrame
visible.
Step 3: Add Components
The next step is to add components to the JFrame
. In this example, we will add a JLabel
and a JTextField
to the JFrame
. To add components to a JFrame
in Java Swing, you can use the add()
method, like this:
Main
// This package is employed to run code on Codefinity. package com.example; // It is not required to be written in typical IDEs. import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class MyFrame extends JFrame { public MyFrame() { super("My Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); JLabel label = new JLabel("Enter your name:"); add(label); JTextField textField = new JTextField(); add(textField); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }
This code creates a new JLabel
with the text "Enter your name:"
and a JTextField
. The add()
method is used to add the components to the JFrame
.
Step 4: Add Event Listeners
The final step is to add event listeners to the components. In this example, we will add an ActionListener
to the JTextField
to handle the user input. To add an ActionListener
to a JTextField
in Java Swing, you can use the addActionListener()
method, like this:
Main
// This package is employed to run code on Codefinity. package com.example; // It is not required to be written in typical IDEs. import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyFrame extends JFrame { public MyFrame() { super("My Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); JLabel label = new JLabel("Enter your name:"); add(label); JTextField textField = new JTextField(); add(textField); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = textField.getText(); label.setText("Hello, " + name + "!"); } }); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }
This code adds an ActionListener
to the JTextField
. When the user presses the Enter key, the actionPerformed()
method is called, and the text in the JTextField
is displayed in the JLabel
with the message "Hello, [name]!"
.
Additional Tips
Here are some additional tips for creating advanced GUI applications in Java:
- Use layout managers to position and size components on the JFrame.
- Use different types of components, such as
JButtons
,JCheckBoxes
,JRadioButtons
, andJLists
, to create a more complex user interface. - Use dialog boxes to display messages or prompt the user for input.
- Use threads to perform long-running tasks in the background and keep the user interface responsive.
By mastering these techniques and using them in your Java GUI applications, you can create applications that are both functional and visually appealing. With practice and experimentation, you can become proficient in Java GUI development and create applications that meet the needs of your users.
Obrigado pelo seu feedback!