/*
GBC - Une classe pour maitriser GridBagLayout
Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.awt.*;
/**
* Cette classe simplifie l'utilisation de la classe GridBagLayout
*/
public class GBC extends GridBagConstraints {
/**
* construit un GBC avec une position gridx and gridy donnée
* et toutes les autres valeurs de GridBagLayout définies
* sur le paramètre par defaut .
* @param gridx La position gridx
* @param gridy La position gridy
*/
public GBC(int gridx, int gridy) {
this.gridx = gridx;
this.gridy = gridy;
}
/**
* Construit un GBC avec gridx, gridy, gridwidth, gridheight
* et toutes les autres valeurs de GridBagConstraints definies sur la
* valeur par defaut.
* @param gridx La position gridx
* @param gridy La position gridyn
* @param gridwidth L'étirement de cellule dans la direction x
* @param gridheight L'étirement de cellule dans la direction x
*/
public GBC(int gridx, int gridy, int gridwidth, int gridheight) {
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
}
/**
* Défini l'ancrage.
* @param anchor La valeur de l'ancrage
* @return this object pour une future modification
*/
public GBC setAnchor(int anchor) {
this.anchor = anchor;
return this;
}
/**
* Définit la direction de fill.
* @param fill La direction de fill
* @return this object pour une future modification
*/
public GBC setFill(int fill) {
this.fill = fill;
return this;
}
/**
* Définit les poids de cellule.
* @param weightx Le pods de cellule dans la direction x
* @param weighty Le pods de cellule dans la direction y
* @return this object pour une future modification
*/
public GBC setWeight(double weightx, double weighty) {
this.weightx = weightx;
this.weighty = weighty;
return this;
}
/**
* Définit les insets de cette cellule.
* @param distance L'espacement à utiliser dans toute les directions
* @return this object pour une future modification
*/
public GBC setInsets(int distance) {
this.insets = new Insets(distance, distance, distance, distance);
return this;
}
/**
* Définit les insets de cette cellule.
* @param top L'espacement à utiliser en haut
* @param left L'espacement à utiliser à gauche
* @param bottom L'espacement à utiliser en bas
* @param right L'espacement à utiliser à droite
* @return this object pour une future modification
*/
public GBC setInsets(int top, int left, int bottom, int right) {
this.insets = new Insets(top, left, bottom, right);
return this;
}
/**
* Définit le remplissage interne
* @param ipadx Le remplissage interne dans la direction x
* @param ipady Le remplissage interne dans la direction x
* @return this object pour une future modification
*/
public GBC setIpad(int ipadx, int ipady) {
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}
}
Exemple pour utiliser la classe :
/**
@version 1.32 2004-05-06
@author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FontDialog
{
public static void main(String[] args)
{
FontDialogFrame frame = new FontDialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
A frame that uses a grid bag layout to arrange font
selection components.
*/
class FontDialogFrame extends JFrame
{
public FontDialogFrame()
{
setTitle("FontDialog");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
ActionListener listener = new FontAction();
// construct components
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("The quick brown fox jumps over the lazy dog");
sample.setEditable(false);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder());
// add components to grid, using GBC convenience class
add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(1));
add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(1));
add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(italic, new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
}
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;
/**
An action listener that changes the font of the
sample text.
*/
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();
}
}
}