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
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FontDialogFrame extends JFrame
{
public FontDialogFrame()
{
setTitle("FontDialog");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
ActionListener listener = new FontAction();
// construction des composants
JLabel faceLabel = new JLabel("Face: ");
face = new JComboBox(new String[]
{
"Serif", "SansSerif", "Monospaced",
"Dialog", "DialogInput"
});
face.addActionListener(listener);
JLabel sizeLabel = new JLabel("Size: ");
size = new JComboBox(new String[]
{
"8", "10", "12", "15", "18", "24", "36", "48"
});
size.addActionListener(listener);
bold = new JCheckBox("Bold");
bold.addActionListener(listener);
italic = new JCheckBox("Italic");
italic.addActionListener(listener);
sample = new JTextArea();
sample.setText("Tout ca pour ca ! Y'a pas a dire, mais faut en taper du code...");
sample.setEditable(false);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder());
// construction des contraintes et ajout des couples composant/contraintes
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.anchor = GridBagConstraints.EAST;
getContentPane().add(faceLabel,c1);
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 1;
c2.gridy = 0;
c2.fill = GridBagConstraints.HORIZONTAL;
c2.weightx = 100;
c2.weighty = 0;
c2.insets = new Insets(1,0,0,0);
getContentPane().add(face,c2);
GridBagConstraints c3 = new GridBagConstraints();
c3.gridx = 0;
c3.gridy = 1;
c3.anchor = GridBagConstraints.EAST;
add(sizeLabel,c3);
GridBagConstraints c4 = new GridBagConstraints();
c4.gridx = 1;
c4.gridy = 1;
c4.weightx = 100;
c4.weighty = 0;
c4.insets = new Insets(1,0,0,0);
getContentPane().add(size,c4);
GridBagConstraints c5 = new GridBagConstraints();
c5.gridx = 0;
c5.gridy = 2;
c5.gridwidth = 2;
c5.gridheight = 1;
c5.anchor = GridBagConstraints.CENTER;
c5.weightx = 100;
c5.weighty = 100;
getContentPane().add(bold,c5);
GridBagConstraints c6 = new GridBagConstraints();
c6.gridx = 0;
c6.gridy = 3;
c6.gridwidth = 2;
c6.gridheight = 1;
c6.anchor = GridBagConstraints.CENTER;
c6.weightx = 100;
c6.weighty = 100;
getContentPane().add(italic,c6);
GridBagConstraints c7 = new GridBagConstraints();
c7.gridx = 2;
c7.gridy = 0;
c7.gridwidth = 1;
c7.gridheight = 4;
c7.fill = GridBagConstraints.BOTH;
c7.weightx = 100;
c7.weighty = 100;
getContentPane().add(sample,c7);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
private JComboBox face;
private JComboBox size;
private JCheckBox bold;
private JCheckBox italic;
private JTextArea sample;
private class FontAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String fontFace = (String) face.getSelectedItem();
int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
+ (italic.isSelected() ? Font.ITALIC : 0);
int fontSize = Integer.parseInt((String) size.getSelectedItem());
Font font = new Font(fontFace, fontStyle, fontSize);
sample.setFont(font);
sample.repaint();
}
}
public static void main(String [] args)
{
FontDialogFrame frame = new FontDialogFrame();
}
} |
Partager