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
Draw
To put some graphics make your own paintable widget. Widget (GUI) - a control element in a graphical user interface
copy
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame();
MyDrawPanel panel = new MyDrawPanel(); // my panel
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyDrawPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(20,50,100,100); // draw a rectangle
}
}
Image
Put images/ on project root, on the same level with build/ and src/.
copy
import java.awt.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame();
MyDrawPanel panel = new MyDrawPanel();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyDrawPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
Image image = new ImageIcon("images/elephant.png").getImage();
g.drawImage(image, 10, 50, this); // x, y
}
}
Circle Gradient
Fill oval object with whatever is loaded on your paintbrush (gradient).
copy
import java.awt.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame();
MyDrawPanel panel = new MyDrawPanel();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyDrawPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradient =
new GradientPaint(70,70,Color.blue,150,150,Color.orange);
// 70,70 starting point
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
// fill oval with
// whatever is loaded on your paintbrush (gradient)
}
}
Layout
By default, a frame has five regions you can add to. You can add only one thing to each region of a frame. That one thing might be a panel that holds three other things.
copy
import java.awt.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
SimpleGui gui = new SimpleGui();
gui.run();
}
}
class SimpleGui {
JFrame frame = new JFrame();
JButton button = new JButton("click me");
MyDrawPanel drawPanel = new MyDrawPanel();
public void run() {
// Add the two widget to the two region of the frame
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyDrawPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillOval(70,70,100,100);
}
}
Console
To simulate a console on a GUI, use a JTextPane. System.out.println can't be used to print to GUI.
copy
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class App {
private static JTextPane panel;
public static void main(String[] args) {
JFrame frame = new JFrame();
panel = new JTextPane(); // my panel
print("abc");
print("abc");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
private static void print(String msg) {
Document doc = panel.getDocument(); // Look Here
try {
doc.insertString(doc.getLength(), msg + "\r\n", null);
} catch (BadLocationException e) {e.printStackTrace();}
}
}
To change color on panel, use styled document.
copy
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class App {
private static JTextPane panel;
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("My Console");
panel = new JTextPane();
panel.setSize(300, 300);
panel.setBackground(Color.BLACK);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
while(true) {
print("Console Message ...");
print("Error Message ...",Color.RED);
Thread.sleep(1000);
}
}
private static void print(String msg, Color color) {
StyledDocument doc =
(StyledDocument) panel.getDocument(); // Look Here
Style style = doc.addStyle("Colors", null);
StyleConstants.setForeground(style, color);
try {
doc.insertString(doc.getLength(), msg + "\r\n", style);
} catch (BadLocationException e) {e.printStackTrace();}
}
private static void print(String msg) {
print(msg, Color.WHITE);
}
}
➥ Questions