Skip to content

Commit 0b896f1

Browse files
Estrella  PopocaEstrella  Popoca
authored andcommitted
Add Set up a GUI with Java
1 parent ef59571 commit 0b896f1

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
5.51 MB
Loading
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
title:
3+
author: Ellie Popoca
4+
uid: 11zhRKeJCWWcD7IkTJBtuK3Mkvo1
5+
datePublished:
6+
published: false
7+
description: Learn how to set up a graphical user interface with Java
8+
header: https://raw.githubusercontent.com/codedex-io/projects/main/projects/set-up-a-gui-with-java/header.png
9+
tags:
10+
- beginner
11+
- java
12+
---
13+
14+
<BannerImage
15+
link="https://raw.githubusercontent.com/codedex-io/projects/main/projects/set-up-a-gui-with-java/header.png"
16+
description="Title Image"
17+
uid={true}
18+
cl="for-sidebar"
19+
/>
20+
21+
# Set up a GUI (Graphical User Interface) with Java
22+
23+
<AuthorAvatar
24+
author_name="Ellie Popoca"
25+
author_avatar="/images/projects/authors/ellie-popoca.jpg"
26+
username="ellie"
27+
uid={true}
28+
/>
29+
30+
<BannerImage
31+
link="https://raw.githubusercontent.com/codedex-io/projects/main/projects/set-up-a-gui-with-java/header.png"
32+
description="Banner"
33+
uid={true}
34+
/>
35+
36+
**Prerequisites**: Java
37+
**Version**: None
38+
**Read Time**: 30 minutes
39+
40+
## Introduction
41+
42+
GUI (Often pronounced "GOO-ee", but actually "Gee-Yuuu-Eye") stands for **graphical user interface**.
43+
44+
Back in the OLD, ANCIENT days of the 1980s, you had to be a knowledgeable tech person to really use computers, but thanks to graphical user interfaces, things like buttons, sliders, windows, and menus let your 5-year-old sibling pull up Roblox on their iPad!
45+
46+
<ImageZoom src="https://i.imgur.com/wfJ167k.png" />
47+
48+
You use GUIs every day, and most commonly, web-based GUIs! As you can imagine, ChatGPT is a GUI, as it lets you prompt without ever interacting with code or the command line.
49+
50+
<ImageZoom src="https://i.imgur.com/TxEy5sO.png" />
51+
52+
## Swing GUI Toolkit
53+
54+
In Java development, you're able to create GUIs for your programs without needing any additional downloads! All you need is the **Swing** GUI Toolkit import statement in the IDE of your choice, and you're able to get started immediately!
55+
56+
57+
```java
58+
import javax.swing.*;
59+
```
60+
61+
If you don't already have a Java IDE, Click [here]() to set yours up!
62+
63+
Create a public class and add a main method to begin.
64+
65+
You'll want to add a `JFrame` object with a title, and use methods like `.setSize`, `.setDefaultCloseOperation`, and `.setLayout` to set up your window screen.
66+
67+
Then, close your program with `frame.setVisible(true);` to see your interface!
68+
69+
```java
70+
public class SimpleGUI {
71+
public static void main(String[] args) {
72+
73+
// Create a new window (JFrame)
74+
JFrame frame = new JFrame("My First GUI");
75+
frame.setSize(300, 150);
76+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
77+
frame.setLayout(null); // absolute positioning
78+
79+
80+
frame.setVisible(true);
81+
}
82+
}
83+
```
84+
85+
Take a look!
86+
87+
<ImageZoom src="https://i.imgur.com/5C8igzQ.png" />
88+
89+
### Components
90+
91+
`JFrame` is a Swing component that allows you to set up your GUI so it shows up when you run your program. Now, you can customize your GUI with other components to add functionality. Some popular Swing components include:
92+
93+
- `JButton` A clickable button
94+
- `JCheckBox` A box that can be checked/unchecked
95+
- `JRadioButton` A radio button (usually in groups)
96+
- `JToggleButton` A button that stays pressed
97+
- `JComboBox` A drop-down list (like a select box)
98+
- `JTextField` A single-line text input
99+
100+
For example, we can add text fields, buttons, and labels by creating objects for each of them and then using `.setBounds` to set up the location of where these components would appear.
101+
102+
```java
103+
// text field
104+
JTextField textField = new JTextField();
105+
textField.setBounds(20, 20, 150, 25);
106+
107+
// button
108+
JButton button = new JButton("Click Me");
109+
button.setBounds(180, 20, 90, 25);
110+
111+
// label
112+
JLabel label = new JLabel("Hello!");
113+
label.setBounds(20, 60, 250, 25);
114+
```
115+
116+
Then, use the `.add()` to add these components to the frame, since we've only initialized them above.
117+
118+
```java
119+
// Add components to the frame
120+
frame.add(textField);
121+
frame.add(button);
122+
frame.add(label);
123+
```
124+
125+
This is your result!
126+
127+
<ImageZoom src="https://i.imgur.com/llQyEYk.png" />
128+
129+
Then, add functionality to your button by adding an action:
130+
131+
```java
132+
button.addActionListener(e -> {
133+
String name = textField.getText();
134+
label.setText("Hello, " + name + "!");
135+
});
136+
```
137+
138+
Now, we can see a name in our frame!
139+
140+
<ImageZoom src="https://i.imgur.com/rF9mZMm.png" />
141+
142+
## Congratulations!
143+
144+
Congrats! You've made your very own GUI with Java's Swing GUI Toolkit. This is just the beginning of the possibilities of what you can build with Java; Games, forms, and image displays are now possible with Swing!
145+
146+
Be sure to check out
147+
- [The Swing API Documentation](https://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html) to see other methods and objects you can create for your GUI
148+
- [Setting up your local Java Development Environment]()
149+
150+
151+
Note that Swing is the default, built-in GUI Toolkit. Other toolkits like [JavaFX](https://openjfx.io/openjfx-docs/#install-javafx) exist with more functionality with their own SDK, or download package.
152+
153+
Other ideas include
154+
- Weather Farrenheit to Celcius converter
155+
- Math calculator
156+
- To-Do list
157+
- Music Player
158+
159+
Share your projects with the team [@codedex_io](https://www.twitter.com/codedex_io) and me, [@exrlla](https://www.twitter.com/exrlla)! Let us know what you come up with!

0 commit comments

Comments
 (0)