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
| import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import javax.activation.MimetypesFileTypeMap;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
public class DemoCardLayout extends JFrame {
public DemoCardLayout() {
CardLayout cardLayout = new CardLayout();
JPanel panel = new JPanel(cardLayout);
JLabel titleLabel = new JLabel("Affichage de page truc.");
titleLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5),BorderFactory.createLineBorder(Color.RED, 1, true)));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
addPage(buttonPanel, titleLabel, panel, cardLayout, new PageTruc(), "TRUC", "Page truc");
addPage(buttonPanel, titleLabel, panel, cardLayout, new PageMachin(), "MACHIN", "Page machin");
addPage(buttonPanel, titleLabel, panel, cardLayout, new PageBidule(), "BIDULE", "Page bidule");
add(titleLabel, BorderLayout.NORTH);
add(panel);
add(buttonPanel, BorderLayout.SOUTH);
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addPage(JPanel buttonPanel, JLabel titlelabel, JPanel panel, CardLayout cardLayout, JPanel page, String pageId, String actionName) {
panel.add(page, pageId);
JButton button = new JButton(actionName);
buttonPanel.add(button);
button.addActionListener(e-> {
cardLayout.show(panel, pageId);
titlelabel.setText("Affichage de "+actionName.toLowerCase()+".");
});
}
public static void main(String[] args) {
new DemoCardLayout().setVisible(true);
}
public static class PageTruc extends JPanel {
private final JTextField[] fields = new JTextField[3];
public PageTruc() {
super(new BorderLayout());
JPanel content = new JPanel(new GridBagLayout());
for(int i=0; i<fields.length; i++) {
JLabel label = new JLabel("Champ "+(i+1)+" :");
content.add(label, new GridBagConstraints(0, i, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1,1,1,1), 0, 0));
fields[i]=new JTextField();
label.setLabelFor(fields[i]);
content.add(fields[i], new GridBagConstraints(1, i, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1,1,1,1), 0, 0));
}
add(content,BorderLayout.NORTH);
}
}
public static class PageMachin extends JPanel {
private JTextField textField;
private JButton button;
public PageMachin() {
super(new FlowLayout(FlowLayout.CENTER));
textField = new JTextField(10);
add(textField);
button = new JButton("Action");
add(button);
}
}
public static class PageBidule extends JPanel {
private File lastDir;
public PageBidule() {
super(new BorderLayout());
JLabel label = new JLabel();
add(new JScrollPane(label));
JButton button = new JButton("Changer image");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(button);
add(buttonPanel, BorderLayout.NORTH);
button.addActionListener(e-> changeImage(label));
}
private void changeImage(JLabel label) {
JFileChooser chooser = new JFileChooser(lastDir);
chooser.setDialogTitle("Choisir un fichier image");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "Fichiers image";
}
@Override
public boolean accept(File f) {
return f.isDirectory() || MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(f.getName()).startsWith("image/");
}
});
if ( chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
try {
Image image = ImageIO.read(chooser.getSelectedFile());
label.setIcon(new ImageIcon(image));
lastDir = chooser.getSelectedFile().getParentFile();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Impossible de charger le fichier "+ chooser.getSelectedFile().getName(), "Erreur", JOptionPane.ERROR_MESSAGE);
}
}
}
}
} |
Partager