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 201 202 203 204 205 206 207 208 209 210
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.border.EtchedBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class Acceuil2 extends JFrame {
private static final long serialVersionUID = 1L;
// Il est préfèrable de créer ce genre de ressources comme ça (partagée, et modifiable à un seul endroit dans le code)
private final static Font POLICE_TITRE = new Font("Times new Roman", Font.CENTER_BASELINE, 30);
private final static Font POLICE_TEXTE = new Font("andalus", Font.ROMAN_BASELINE, 20);
private static final int HGAP = 10; // 5 pour espacement horizontal
private static final int VGAP = 10; // 5 pour espacement vertical
// on a besoin que de certaines variables ici (les composants qu'on veut pouvoir manipuler)
private JTextArea grandeZone;
public Acceuil2() {
// paramètrage fenêtre
this.setTitle("");
this.setSize(900, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// arrière plan
ImageBackground container = new ImageBackground(new ImageIcon("image3.jpg").getImage(),false, false);
this.setContentPane(container);
// composants dans la fenêtre
createContent(container);
}
private void createContent(JPanel panel) {
// on utilise un BorderLayout pour gérer la position des composants
panel.setLayout(new BorderLayout(HGAP, VGAP));
// en haut
add(creerTitre(),BorderLayout.NORTH);
// à droite
add(creerBoutons(),BorderLayout.EAST);
// au centre
add(creerTexte(),BorderLayout.CENTER);
//add(creerTitre1(),BorderLayout.SOUTH);
add(crerBoutons(),BorderLayout.WEST);
}
private JComponent creerTexte() {
JTextPane pane=new JTextPane();
ImageIcon icon = new ImageIcon(new ImageIcon("image.jpg").getImage().getScaledInstance(500, 300, Image.SCALE_DEFAULT));
JLabel test = new JLabel(icon);
//JLabel test1 = new JLabel("");
//test1.setFont(POLICE_TITRE);
//test1.setForeground(Color.BLACK);
//test1.setHorizontalAlignment(JLabel.CENTER);
//ImageIcon icon = new ImageIcon(new ImageIcon("exemple.jpg").getImage().getScaledInstance(500, 300, Image.SCALE_DEFAULT));
//JTextPane grandeZone = new JTextPane((StyledDocument) icon);
// on créer la zone de texte
//grandeZone = new JTextArea(10, 10);
// on la décore
// on la met dans un scrollpane
//JScrollPane scrollPane = new JScrollPane(grandeZone);
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout(HGAP, VGAP));
pan.add(pane,BorderLayout.CENTER);
//pan.add(test,BorderLayout.SOUTH);
//pan.add(test1,BorderLayout.NORTH);
return pan ;
}
private Component creerTitre() {
// on veut un label
Dimension dimension = new Dimension(100,70);
// on le créé
JLabel label = new JLabel("site web");
//ImageIcon icon = new ImageIcon(new ImageIcon("capture.png").getImage());
//label.setIcon(icon);
// on le décore
label.setFont(POLICE_TITRE);
label.setForeground(Color.BLACK);
label.setHorizontalAlignment(JLabel.CENTER);
label.setPreferredSize(dimension);
return (label);
}
private JPanel crerBoutons() {
JPanel pa = new JPanel(); // le conteneur
Dimension dimension = new Dimension(150,140);
pa.setPreferredSize(dimension);
pa.setLayout(new GridLayout(0,1)); // on met un layout manager (ici, une grille à une colonne)
JRadioButton birdButton = new JRadioButton("nom");
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand("nom");
birdButton.setSelected(true);
// birdButton.addActionListener( ... ); // il faut bien sûr ajouter un ActionListener (tout dépend comment il est fait)
JRadioButton catButton = new JRadioButton("prenom");
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand("prenom");
// catButton.addActionListener( ... ); // ajout de l'action listener
//Groupe les boutons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
// ajouter les boutons au panel
pa.add(birdButton);
pa.add(catButton);
// on ajoute une bordure de type gravure avec titre
pa.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "liste de choix"));
return pa;
}
private JComponent creerBoutons() {
Dimension dimension = new Dimension(100,170);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 1 colonne de boutons
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 10;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(HGAP,VGAP,20,10); // espacement
JButton bouton1 = new JButton("1");
bouton1.setPreferredSize(dimension);
panel.add(bouton1, gbc);
Dimension space = new Dimension(HGAP,VGAP);
panel.add(Box.createRigidArea(space)); // permet d'avoir un espacement
// pareil pour les autres
JButton bouton2 = new JButton("2");
bouton2.setPreferredSize(dimension);
panel.add(bouton2, gbc);
panel.add(Box.createRigidArea(space)); // permet d'avoir un espacement
JButton bouton3 = new JButton("3");
bouton3.setPreferredSize(dimension);
panel.add(bouton3, gbc);
panel.add(Box.createRigidArea(space)); // permet d'avoir un espacement
JButton bouton4 = new JButton("4");
bouton4.setPreferredSize(dimension);
panel.add(bouton4, gbc);
// on affecte les action aux boutons
bouton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
Fichi.affichage();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
return panel;
}
public static void main(String[] args) throws IOException {
Acceuil2 ac = new Acceuil2();
ac.setVisible(true);
}
} |