1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Polygon;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.DefaultFocusManager;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
public class CustomDialog extends JDialog {
private static final long serialVersionUID = 1L;
private static final String FONT = "Courier New";
private static final Font FONTBUTTON = new Font(FONT,Font.PLAIN,27);
private static final Color COLORTEXT = Color.WHITE;
private static final Color COLORBACKGROUND = new Color(255,0,0);
private static final Color COLORBUTTON = new Color(240,0,0);
private static final Color COLORBUTTONBORDER = new Color(250,0,0);
private static final Color COLORICON = Color.BLACK;
private static final int MARGINBUTTON = 10;
private static final int MARGINDIALOG = 10;
private static final Icon FLOPPY_DISK_ICON = createFloppyDiskIcon();
private boolean ok;
private JButton buttonValider;
private JButton buttonAnnuler;
private CustomDialog(Frame owner) {
super(owner, true);
setUndecorated(true); // pour enlever les décorations
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(MARGINDIALOG,MARGINDIALOG,MARGINDIALOG,MARGINDIALOG));
panel.setBackground(COLORBACKGROUND);
JTextPane messagePane = new JTextPane();
messagePane.setContentType("text/html");
messagePane.setFont(getFont());
messagePane.setEditable(false);
messagePane.setText("<html><font face='"+FONT+"' color='white'><b><center><font size='37'>VOULEZ-VOUS FAIRE L'ENREGISTREMENT ?</font></center><center>Enter: VALIDER --- Escape: ANNULER</center></b></font>");
messagePane.setOpaque(false);
messagePane.setForeground(COLORTEXT);
panel.add(messagePane);
JLabel iconLabel = new JLabel(FLOPPY_DISK_ICON);
JPanel iconPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
iconPanel.setOpaque(false);
iconPanel.add(iconLabel);
panel.add(iconPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,10,10));
buttonPanel.setOpaque(false);
buttonValider = createValiderButton();
buttonAnnuler = createAnnulerButton();
buttonPanel.add(buttonValider);
buttonPanel.add(buttonAnnuler);
panel.add(buttonPanel, BorderLayout.SOUTH);
add(panel);
DefaultFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if ( e.getID()==KeyEvent.KEY_PRESSED ) {
switch(e.getKeyCode()) {
case KeyEvent.VK_ENTER:
ok=true;
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this);
dispose();
return true;
case KeyEvent.VK_ESCAPE:
ok=false;
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this);
dispose();
return true;
}
}
return false;
}
});
}
private JButton createValiderButton() {
JButton jbutton = createButton("OK",e->ok=true);
return jbutton;
}
private JButton createAnnulerButton() {
JButton jbutton = createButton("ANNULER",e->ok=false);
return jbutton;
}
private JButton createButton(String text, ActionListener listener) {
JButton jbutton = new JButton(text);
jbutton.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(COLORBUTTONBORDER),
BorderFactory.createEmptyBorder(MARGINBUTTON,MARGINBUTTON,MARGINBUTTON,MARGINBUTTON)
));
jbutton.setFont(FONTBUTTON);
jbutton.setForeground(COLORTEXT);
jbutton.setBackground(COLORBUTTON);
jbutton.setPreferredSize(new Dimension(150,jbutton.getPreferredSize().height));
jbutton.addActionListener(e-> {
listener.actionPerformed(e);
dispose();
});
jbutton.setFocusable(false);
jbutton.setFocusPainted(false);
return jbutton;
}
private void showCentered() {
if ( !isVisible() ) {
pack();
setLocationRelativeTo(getParent());
setVisible(true);
}
}
public static boolean showDialog(Frame owner) {
CustomDialog dialog = new CustomDialog(owner);
dialog.showCentered();
return dialog.ok;
}
private static Icon createFloppyDiskIcon() {
BufferedImage image = new BufferedImage(160, 160, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(COLORICON);
g.setStroke(new BasicStroke(9, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
Polygon polygon = new Polygon(
new int[] {15, 130, 140, 140, 15},
new int[] {15, 15, 30, 140, 140},
5
);
g.draw(polygon);
g.drawRect(45, 15, 65, 45);
g.drawRect(85, 25, 15, 25);
g.drawRect(35, 80, 85, 60);
g.dispose();
return new ImageIcon(image);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel buttonpanel = new JPanel(new GridBagLayout());
JButton button = new JButton("Afficher dialogue");
buttonpanel.add(button);
JPanel labelpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel reponse = new JLabel(" ");
labelpanel.add(reponse);
frame.add(buttonpanel);
frame.add(labelpanel, BorderLayout.SOUTH);
button.addActionListener(e-> {
boolean ok = CustomDialog.showDialog(null);
reponse.setText("Réponse : " + (ok?"VALIDER":"ANNULER"));
});
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager