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
Menu
You can attach a menu to a frame. By convention, the menus aren't placed with the other components in the UI. A menu usually appears either in a menu bar or as a popup menu.
copy
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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);
}
public App() {
initMenu();
}
public void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItemNew = new JMenuItem("New");
JMenuItem menuItemOpen = new JMenuItem("Open");
menu.setMnemonic('F');
menuItemNew.setMnemonic('N');
menuItemOpen.setMnemonic('O');
menuBar.add(menu);
menu.add(menuItemNew);
menu.add(menuItemOpen);
// View
JMenu menuView = new JMenu("View") {{
setMnemonic('V');
add (new JMenu("Toolbars") {{
add (new JCheckBoxMenuItem("Buttons") {{
setMnemonic('B');
addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
//
}
});
}});
}});
}};
menuBar.add(menuView);
this.setJMenuBar(menuBar); // Look Here
}
}
Switch panels on menu actions.
copy
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 600, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
}
public App() {
initMenu();
}
public void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuItemNew = new JMenuItem("New");
JMenu menuOpen = new JMenu("Open");
JMenuItem submenuOpen1 = new JMenuItem("Open submenu");
menuFile.setMnemonic('F');
menuItemNew.setMnemonic('N');
menuOpen.setMnemonic('O');
menuBar.add(menuFile);
menuFile.add(menuItemNew);
menuFile.add(menuOpen);
menuOpen.add(submenuOpen1);
setJMenuBar(menuBar);
// Look here
menuItemNew.addActionListener(new MenuAction(new Action1()));
submenuOpen1.addActionListener(new MenuAction(new Action2()));
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel panel) {
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) { // Look Here
changePanel(panel);
}
}
public void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
validate();
repaint();
}
}
class Action1 extends JPanel { // Look Here
public Action1() {
setBackground(Color.gray);
JLabel message = new JLabel();
message.setPreferredSize(new Dimension(440, 27));
message.setForeground(Color.white);
message.setBorder(BorderFactory.createLineBorder(Color.white));
message.setText("Action 1");
add(message);
}
}
class Action2 extends JPanel { // Look Here
public Action2() {
setBackground(Color.black);
JLabel message = new JLabel();
message.setForeground(Color.white);
message.setBorder(BorderFactory.createLineBorder(Color.red));
message.setText("Action 2");
add(message);
}
}
Default action
Set the default action on window open
copy
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 600, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
}
public App() {
initMenu();
// default action
this.addWindowListener( new WindowAdapter() { // Look Here
@Override
public void windowOpened( WindowEvent e ){
changePanel(new Action2());
}
});
}
public void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuItemNew = new JMenuItem("New");
JMenuItem menuItemOpen = new JMenuItem("Open");
menuFile.setMnemonic('F');
menuBar.add(menuFile);
menuFile.add(menuItemNew);
menuFile.add(menuItemOpen);
setJMenuBar(menuBar);
menuItemNew.addActionListener(new MenuAction(new Action1()));
menuItemOpen.addActionListener(new MenuAction(new Action2()));
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel panel) {
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
public void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
validate();
repaint();
}
}
class Action1 extends JPanel {
public Action1() {
setBackground(Color.gray);
JLabel message = new JLabel();
message.setPreferredSize(new Dimension(440, 27));
message.setForeground(Color.white);
message.setBorder(BorderFactory.createLineBorder(Color.white));
message.setText("Action 1");
add(message);
}
}
class Action2 extends JPanel {
public Action2() {
setBackground(Color.black);
JLabel message = new JLabel();
message.setForeground(Color.white);
message.setBorder(BorderFactory.createLineBorder(Color.red));
message.setText("Action 2");
add(message);
}
}
Look & Fill
Look refers to the appearances of GUI widgets (or JComponents). Feel refers to the way the widgets behave.
copy
import javax.swing.*;
import java.awt.*;
public class App extends JFrame {
public static void main(String[] args) {
setLookAndFill();
App frame = new App();
frame.setBounds(200, 200, 250, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
}
private static void setLookAndFill() {
/**
* Nimbus look and fill (introduced in Java SE 6)
* If not available keep default
*/
try {
/*UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
);*/
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) { // Look Here
javax.swing.UIManager.setLookAndFeel(
info.getClassName()
);
break;
}
}
} catch (Exception ex) {}
}
public App() {
initMenu();
}
public void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItemNew = new JMenuItem("New");
JMenuItem menuItemOpen = new JMenuItem("Open");
menu.setMnemonic('F');
menuItemNew.setMnemonic('N');
menuItemOpen.setMnemonic('O');
menuBar.add(menu);
menu.add(menuItemNew);
menu.add(menuItemOpen);
this.setJMenuBar(menuBar);
}
}
➥ Questions