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
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Boîte de dialogue de sélection des police .
*
* Le programme applique la police choisie
* dans la boîte de dialogue sur un texte
*/
public final class ExFont extends JDialog {
public ExFont(Frame f) {
super(f, "Font Chooser", true);
JPanel pnl = new JPanel();
JPanel top = new JPanel( );
top.setLayout(new FlowLayout( ));
policeChoisie = new List(8);
top.add(policeChoisie);
// Obtenir la liste des polices disponible
Toolkit toolkit = Toolkit.getDefaultToolkit( );
listePolice = GraphicsEnvironment.getLocalGraphicsEnvironment( ).getAvailableFontFamilyNames( );
for (String listePolice1 : listePolice) {
policeChoisie.add(listePolice1);
}
policeChoisie.select(0);
fontSizeChoice = new List(8);
top.add(fontSizeChoice);
for (String fontSize1 : fontSizes) {
fontSizeChoice.add(fontSize1);
}
fontSizeChoice.select(5);
pnl.add(top, BorderLayout.NORTH);
JPanel attrs = new JPanel( );
top.add(attrs);
attrs.setLayout(new GridLayout(0,1));
attrs.add(bold =new Checkbox("Bold", false));
attrs.add(italic=new Checkbox("Italic", false));
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//label pour les boutons appliquer, apercu et annuler
lblApercu = new JLabel("I see your true color", JLabel.CENTER);
lblApercu.setSize(200, 50);
pnl.add(BorderLayout.CENTER, lblApercu);
JPanel bot = new JPanel( );
//bouton appliquer
Button okButton = new Button("Appliquer");
bot.add(okButton);
okButton.addActionListener((ActionEvent e) -> {
previewFont( );
etat=true;
dispose( );
setVisible(false);
});
//bouton apercu
Button pvButton = new Button("Aperçu");
bot.add(pvButton);
pvButton.addActionListener((ActionEvent e) -> {
previewFont( );
});
//bouton annuler
Button btnAnnuler = new Button("Annuler");
bot.add(btnAnnuler);
btnAnnuler.addActionListener((ActionEvent e) -> {
// Réinitaliser les valeurs si l'utilisateur annule l'opération
selectedFont = null;
fontName = null;
fontSize = 0;
isBold = false;
isItalic = false;
dispose( );
setVisible(false);
});
//position du panel
pnl.add(BorderLayout.SOUTH, bot);
previewFont( );
add(pnl);
pack( );
setSize(450, 200);
setLocationRelativeTo(this);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
ExFont() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
protected void previewFont( ) {
fontName = policeChoisie.getSelectedItem( );
String resultSizeName = fontSizeChoice.getSelectedItem( );
int FontSize = Integer.parseInt(resultSizeName);
isBold = bold.getState( );
isItalic = italic.getState( );
int attrs = Font.PLAIN;
if (isBold) attrs = Font.BOLD;
if (isItalic) attrs |= Font.ITALIC;
selectedFont = new Font(fontName, attrs, FontSize);
lblApercu.setFont(selectedFont);
pack( );
}
/** Obtenir le nom de la police sélectionnée.
* @return */
public String getSelectedName( ) {
return fontName;
}
/** Obtenir la taille sélectionnée
* @return */
public int getSelectedSize( ) {
return fontSize;
}
public Font getSelectedFont( ) {
return selectedFont;
}
public static void main(String[] args) {
final JFrame f = new JFrame("Modificateur de police");
final ExFont fc = new ExFont(f);
final Container pnl = f.getContentPane( );
pnl.setLayout(new GridLayout(0, 1));
JButton btnSelect = new JButton("Sélectionner la police...");
pnl.add(btnSelect);
final JTextArea myText = new JTextArea("Vous pouvez changer la police de ce texte ou tapez" +" votre propre texte ici",80,50);
pnl.add(myText);
btnSelect.addActionListener((ActionEvent e) -> {
fc.setVisible(true);
if(etat)
{
Font myNewFont = fc.getSelectedFont( );
myText.setFont(myNewFont);
f.pack( );
fc.dispose( );
etat=false;
}
});
f.pack( );
f.setVisible(true);
f.setSize(250,150);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected Font selectedFont;
protected String fontName;
protected int fontSize;
protected boolean isBold;
protected boolean isItalic;
protected String listePolice[];
protected List policeChoisie;
protected List fontSizeChoice;
static protected boolean etat=false;
Checkbox bold, italic;
protected String fontSizes[] = {
"8", "10", "11", "12", "14", "16", "18", "20", "24",
"30", "36", "40", "48", "60", "72"
};
protected JLabel lblApercu;
} |
Partager