Skip to content

Commit ccd7fec

Browse files
committed
gui-programming
1 parent 22f6fe6 commit ccd7fec

File tree

5 files changed

+605
-5
lines changed

5 files changed

+605
-5
lines changed

docs/java/gui-programming-with-swing/creating-gui-components.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,138 @@ sidebar_label: Creating GUI Components
55
sidebar_position: 2
66
tags: [java, swing, gui, programming, java swing]
77
description: In this tutorial, we will learn about creating GUI components in Java using Swing. We will learn about how to create various GUI components such as buttons, labels, text fields, and more.
8-
---
8+
---
9+
Creating GUI components in Java Swing involves instantiating and configuring various classes provided by the Swing framework. Here's an overview of how to create some common GUI components:
10+
11+
### 1. JFrame (Main Window)
12+
13+
```java
14+
import javax.swing.*;
15+
16+
public class MyFrame extends JFrame {
17+
public MyFrame() {
18+
super("My Application"); // Set window title
19+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close operation
20+
setSize(400, 300); // Set window size
21+
setLocationRelativeTo(null); // Center window
22+
setVisible(true); // Make window visible
23+
}
24+
25+
public static void main(String[] args) {
26+
new MyFrame(); // Create an instance of MyFrame
27+
}
28+
}
29+
```
30+
31+
### 2. JPanel (Container)
32+
33+
```java
34+
import javax.swing.*;
35+
36+
public class MyPanel extends JPanel {
37+
public MyPanel() {
38+
add(new JButton("Click Me")); // Add a button to the panel
39+
}
40+
41+
public static void main(String[] args) {
42+
JFrame frame = new JFrame("My Panel");
43+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44+
frame.add(new MyPanel()); // Add an instance of MyPanel to the frame
45+
frame.pack(); // Pack the components
46+
frame.setLocationRelativeTo(null); // Center window
47+
frame.setVisible(true); // Make window visible
48+
}
49+
}
50+
```
51+
52+
### 3. JButton (Button)
53+
54+
```java
55+
import javax.swing.*;
56+
57+
public class MyButton extends JFrame {
58+
public MyButton() {
59+
super("Button Example");
60+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
61+
JButton button = new JButton("Click Me");
62+
button.addActionListener(e -> JOptionPane.showMessageDialog(this, "Button clicked!"));
63+
add(button);
64+
pack();
65+
setLocationRelativeTo(null);
66+
setVisible(true);
67+
}
68+
69+
public static void main(String[] args) {
70+
new MyButton();
71+
}
72+
}
73+
```
74+
75+
### 4. JLabel (Label)
76+
77+
```java
78+
import javax.swing.*;
79+
80+
public class MyLabel extends JFrame {
81+
public MyLabel() {
82+
super("Label Example");
83+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84+
JLabel label = new JLabel("Hello, Swing!");
85+
add(label);
86+
pack();
87+
setLocationRelativeTo(null);
88+
setVisible(true);
89+
}
90+
91+
public static void main(String[] args) {
92+
new MyLabel();
93+
}
94+
}
95+
```
96+
97+
### 5. JTextField (Text Field)
98+
99+
```java
100+
import javax.swing.*;
101+
102+
public class MyTextField extends JFrame {
103+
public MyTextField() {
104+
super("Text Field Example");
105+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
106+
JTextField textField = new JTextField(20); // Set preferred width
107+
add(textField);
108+
pack();
109+
setLocationRelativeTo(null);
110+
setVisible(true);
111+
}
112+
113+
public static void main(String[] args) {
114+
new MyTextField();
115+
}
116+
}
117+
```
118+
119+
### 6. JTextArea (Text Area)
120+
121+
```java
122+
import javax.swing.*;
123+
124+
public class MyTextArea extends JFrame {
125+
public MyTextArea() {
126+
super("Text Area Example");
127+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
128+
JTextArea textArea = new JTextArea(10, 30); // Set rows and columns
129+
JScrollPane scrollPane = new JScrollPane(textArea); // Add scroll bars
130+
add(scrollPane);
131+
pack();
132+
setLocationRelativeTo(null);
133+
setVisible(true);
134+
}
135+
136+
public static void main(String[] args) {
137+
new MyTextArea();
138+
}
139+
}
140+
```
141+
142+
These examples demonstrate how to create various GUI components in Java Swing. By instantiating and configuring these components, you can build rich and interactive user interfaces for your Java applications.

docs/java/gui-programming-with-swing/event-handling-and-listeners.md

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,155 @@ sidebar_label: Event Handling and Listeners
55
sidebar_position: 3
66
tags: [java, swing, event handling, listeners]
77
description: In this tutorial, we will learn about event handling and listeners in Java. We will learn about how to handle events in Java Swing applications using event listeners.
8-
---
8+
---
9+
Event handling in Java Swing involves registering event listeners to respond to user interactions with GUI components. Here's an overview of how to handle events using listeners:
10+
11+
### 1. ActionListener (Button Click)
12+
13+
```java
14+
import javax.swing.*;
15+
import java.awt.event.*;
16+
17+
public class ButtonClickExample extends JFrame implements ActionListener {
18+
private JButton button;
19+
20+
public ButtonClickExample() {
21+
super("Button Click Example");
22+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
24+
button = new JButton("Click Me");
25+
button.addActionListener(this); // Register ActionListener
26+
add(button);
27+
28+
pack();
29+
setLocationRelativeTo(null);
30+
setVisible(true);
31+
}
32+
33+
@Override
34+
public void actionPerformed(ActionEvent e) {
35+
if (e.getSource() == button) {
36+
JOptionPane.showMessageDialog(this, "Button clicked!");
37+
}
38+
}
39+
40+
public static void main(String[] args) {
41+
new ButtonClickExample();
42+
}
43+
}
44+
```
45+
46+
### 2. MouseListener (Mouse Click)
47+
48+
```java
49+
import javax.swing.*;
50+
import java.awt.event.*;
51+
52+
public class MouseClickExample extends JFrame implements MouseListener {
53+
public MouseClickExample() {
54+
super("Mouse Click Example");
55+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
56+
57+
JButton button = new JButton("Click Me");
58+
button.addMouseListener(this); // Register MouseListener
59+
add(button);
60+
61+
pack();
62+
setLocationRelativeTo(null);
63+
setVisible(true);
64+
}
65+
66+
@Override
67+
public void mouseClicked(MouseEvent e) {
68+
JOptionPane.showMessageDialog(this, "Mouse clicked!");
69+
}
70+
71+
@Override
72+
public void mousePressed(MouseEvent e) {}
73+
74+
@Override
75+
public void mouseReleased(MouseEvent e) {}
76+
77+
@Override
78+
public void mouseEntered(MouseEvent e) {}
79+
80+
@Override
81+
public void mouseExited(MouseEvent e) {}
82+
83+
public static void main(String[] args) {
84+
new MouseClickExample();
85+
}
86+
}
87+
```
88+
89+
### 3. KeyListener (Keyboard Input)
90+
91+
```java
92+
import javax.swing.*;
93+
import java.awt.event.*;
94+
95+
public class KeyInputExample extends JFrame implements KeyListener {
96+
public KeyInputExample() {
97+
super("Key Input Example");
98+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
99+
100+
JTextField textField = new JTextField(20);
101+
textField.addKeyListener(this); // Register KeyListener
102+
add(textField);
103+
104+
pack();
105+
setLocationRelativeTo(null);
106+
setVisible(true);
107+
}
108+
109+
@Override
110+
public void keyTyped(KeyEvent e) {}
111+
112+
@Override
113+
public void keyPressed(KeyEvent e) {
114+
JOptionPane.showMessageDialog(this, "Key pressed: " + e.getKeyChar());
115+
}
116+
117+
@Override
118+
public void keyReleased(KeyEvent e) {}
119+
120+
public static void main(String[] args) {
121+
new KeyInputExample();
122+
}
123+
}
124+
```
125+
126+
### 4. ItemListener (Checkbox/Radio Button Selection)
127+
128+
```java
129+
import javax.swing.*;
130+
import java.awt.event.*;
131+
132+
public class ItemSelectionExample extends JFrame implements ItemListener {
133+
public ItemSelectionExample() {
134+
super("Item Selection Example");
135+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
136+
137+
JCheckBox checkBox = new JCheckBox("Check Me");
138+
checkBox.addItemListener(this); // Register ItemListener
139+
add(checkBox);
140+
141+
pack();
142+
setLocationRelativeTo(null);
143+
setVisible(true);
144+
}
145+
146+
@Override
147+
public void itemStateChanged(ItemEvent e) {
148+
if (e.getStateChange() == ItemEvent.SELECTED) {
149+
JOptionPane.showMessageDialog(this, "Item selected!");
150+
}
151+
}
152+
153+
public static void main(String[] args) {
154+
new ItemSelectionExample();
155+
}
156+
}
157+
```
158+
159+
These examples demonstrate how to handle various events in Java Swing using event listeners. By implementing and registering listeners, you can respond to user interactions with GUI components and perform appropriate actions in your application.

docs/java/gui-programming-with-swing/introduction-to-swing.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,65 @@ sidebar_label: Introduction to Swing
55
sidebar_position: 1
66
tags: [java, swing, gui, programming, java swing]
77
description: In this tutorial, we will learn about Swing, a GUI toolkit for Java. We will learn about what Swing is, how it is used to create graphical user interfaces in Java, and some of the components provided by Swing.
8-
---
8+
---
9+
10+
11+
## Introduction to Swing
12+
13+
Swing is a GUI (Graphical User Interface) toolkit for Java. It provides a set of lightweight components that allow developers to create rich, platform-independent graphical user interfaces for Java applications. Swing was introduced as part of the Java Foundation Classes (JFC) in Java 2 (JDK 1.2) and has been the primary GUI toolkit for Java desktop applications since then.
14+
15+
### Features of Swing
16+
17+
1. **Platform Independence:** Swing components are written entirely in Java and are not dependent on the underlying operating system's native GUI components. This allows Swing applications to run on any platform that supports Java without modification.
18+
19+
2. **Rich Set of Components:** Swing provides a wide range of components for building GUIs, including buttons, labels, text fields, checkboxes, radio buttons, list boxes, combo boxes, tables, trees, and more. These components can be customized and combined to create complex user interfaces.
20+
21+
3. **Custom Look and Feel:** Swing supports pluggable look and feel (PLAF), allowing developers to customize the appearance of their applications. Swing applications can have the native look and feel of the underlying operating system or a custom look and feel defined by the developer.
22+
23+
4. **Event-Driven Programming Model:** Swing follows an event-driven programming model, where user interactions (such as mouse clicks, keyboard input, and window events) trigger events that are handled by event listeners. This allows developers to create interactive and responsive user interfaces.
24+
25+
5. **Layout Managers:** Swing provides layout managers that allow developers to arrange components within containers dynamically. Layout managers handle the sizing and positioning of components, ensuring that GUIs adapt to different screen sizes and resolutions.
26+
27+
### Getting Started with Swing
28+
29+
To start developing Swing applications, you need to have the Java Development Kit (JDK) installed on your system. Once you have the JDK installed, you can create Swing applications using any Java IDE (Integrated Development Environment) such as IntelliJ IDEA, Eclipse, or NetBeans, or you can use a simple text editor and the command line.
30+
31+
### Example Swing Application
32+
33+
Here's a simple "Hello, Swing!" application written in Java:
34+
35+
```java
36+
import javax.swing.*;
37+
38+
public class HelloWorldSwing {
39+
public static void createAndShowGUI() {
40+
// Create and set up the window
41+
JFrame frame = new JFrame("HelloWorldSwing");
42+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43+
44+
// Add the "Hello, Swing!" label to the window
45+
JLabel label = new JLabel("Hello, Swing!", SwingConstants.CENTER);
46+
frame.getContentPane().add(label);
47+
48+
// Display the window
49+
frame.pack();
50+
frame.setVisible(true);
51+
}
52+
53+
public static void main(String[] args) {
54+
// Schedule a job for the event dispatch thread:
55+
// creating and showing this application's GUI.
56+
javax.swing.SwingUtilities.invokeLater(new Runnable() {
57+
public void run() {
58+
createAndShowGUI();
59+
}
60+
});
61+
}
62+
}
63+
```
64+
65+
This code creates a simple Swing window with a label that displays "Hello, Swing!". It demonstrates the basic structure of a Swing application, including creating a JFrame window, adding components to the window, and displaying the window on the screen.
66+
67+
### Conclusion
68+
69+
Swing provides a powerful and flexible toolkit for building GUI applications in Java. With its platform independence, rich set of components, and customizability, Swing remains a popular choice for developing desktop applications in Java. Whether you're building a simple calculator or a complex business application, Swing offers the tools you need to create modern, responsive user interfaces.

0 commit comments

Comments
 (0)