Java
/
Swing
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Frame
Java Swing is used to create window-based applications. A JFrame is the object that represents a window on the screen.
copy
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame(); // make a frame
frame.setSize(300, 200);
frame.setVisible(true);
// when window closed, stop the program
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a button
JButton button = new JButton("click me");
// add button to frame
frame.getContentPane().add(button);
}
}
A dialog window is an independent window for temporary notice.
copy
import java.awt.*;
import javax.swing.*;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 400, 300);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "Bad format!");
// Message in the middle of the screen
JOptionPane.showMessageDialog(frame, "Bad format!"); // Look Here
// Message in the middle of the frame
}
}
New Frame
Use setVisible() method to open a new frame.
copy
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class App extends JFrame {
static JFrame frame2 = new JFrame();
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 400, 300);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame2.setBounds(350, 350, 300, 200);
frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame2.setVisible(false);
}
public App() {
JPanel panel = new JPanel();
JButton button = new JButton("Open");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame2.setVisible(true); // Look Here
}
});
panel.add(button);
add(panel);
}
}
Use getTopLevelAncestor() to get parent frame.
copy
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class App extends JFrame {
static Frame2 frame2 = new Frame2();
public static void main(String[] args) {
App frame = new App() {{
setBounds(200, 200, 300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}};
}
public App() {
JPanel panel = new JPanel() {{
JButton button = new JButton("Open") {{
addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
frame2.setVisible(true);
}
});
}};
add(button);
}};
add(panel);
}
public static class Frame2 extends JFrame {
public Frame2() {
setBounds(250, 250, 200, 100);
setVisible(false);
JPanel panel = new JPanel() {{
JButton button = new JButton ("Cancel") {{
addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
getTopLevelAncestor().setVisible(false); // corect - Look Here
// setVisible(false); // this only hides the button
}
});
}};
add(button);
}};
add(panel);
}
}
}
➥ Questions