Une implementation de la class javax.swing.border.Border qui simule une fenêtre. La barre de titre est personnalisable (icon, texte et position du texte, police). Le contour est aussi modifiable (couleur et largeur).
Source / Exemple :
package org.swingExtension.border;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
/**
public class TitleBarBorder implements Border {
private String text;
private Icon icon;
private int textAlignment = SwingConstants.LEFT;
private Color background = Color.BLUE;
private Color foreground = UIManager.getColor("Label.foreground");
private Color borderColor = Color.GRAY;
private Font font = UIManager.getFont("Label.font");
private int borderWidth = 1;
private int iconTextGap = 4;
private int vGap = 2;
private int hGap = 2;
public TitleBarBorder() {
this(null);
}
public TitleBarBorder(String title) {
setText(title);
}
public TitleBarBorder(String title, Icon icon) {
setText(title);
setIcon(icon);
}
public TitleBarBorder(String title, Icon icon, int textAlignment) {
setText(title);
setIcon(icon);
setTextAlignment(textAlignment);
}
/**
- @see javax.swing.border.Border#getBorderInsets(java.awt.Component)
- /
public Insets getBorderInsets(Component c) {
return new Insets(borderWidth + getTitleBarHeight(c) + vGap, borderWidth, borderWidth, borderWidth);
}
private int getTitleBarHeight(Component c) {
int h = 0;
if (text != null) {
h = c.getFontMetrics(font).getHeight();
}
if (icon != null) {
h = Math.max(icon.getIconHeight(), h);
}
return h;
}
/**
- @see javax.swing.border.Border#isBorderOpaque()
- /
public boolean isBorderOpaque() {
return false;
}
/**
- @see javax.swing.border.Border#paintBorder(java.awt.Component, java.awt.Graphics, int, int, int, int)
- /
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
Font oldFont = g.getFont();
int h = getTitleBarHeight(c);
g.setColor(background);
g.fillRect(x + borderWidth, y + borderWidth, width - borderWidth - borderWidth, h + vGap + vGap);
int x2 = x + hGap + borderWidth;
int y2 = y + vGap + borderWidth;
if (icon != null) {
y2 = (h + vGap + vGap) / 2 - icon.getIconHeight() / 2 + borderWidth;
icon.paintIcon(c, g, x2, y2);
x2 += icon.getIconWidth() + iconTextGap;
}
if (text != null) {
g.setFont(font);
g.setColor(foreground);
FontMetrics fm = g.getFontMetrics();
y2 = y + vGap + borderWidth;
int h2 = (h - fm.getHeight()) / 2;
if (textAlignment != SwingConstants.LEFT) {
if (textAlignment == SwingConstants.RIGHT) {
x2 = x + width - hGap - borderWidth - fm.stringWidth(text);
} else if (textAlignment == SwingConstants.CENTER) {
x2 = (x + width - borderWidth - borderWidth - hGap - hGap) / 2 - fm.stringWidth(text) / 2;
}
}
g.drawString(text, x2, y2 + h - h2 - fm.getDescent());
}
g.setColor(borderColor);
for (int i = 0; i < borderWidth; i++) {
g.drawRect(x + i, y + i, width - 1 - (2 * i), height - 1 - (2 * i));
}
g.setColor(oldColor);
g.setFont(oldFont);
}
/**
public String getText() {
return text;
}
/**
- @param text the text to set
- /
public void setText(String text) {
this.text = text;
}
/**
- @param color the backgroundColor to set
- /
public void setBackground(Color color) {
this.background = color;
}
/**
- @param borderWidth the borderWidth to set
- /
public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
}
/**
- @param gap the hGap to set
- /
public void setHGap(int gap) {
hGap = gap;
}
/**
- @param icon the icon to set
- /
public void setIcon(Icon icon) {
this.icon = icon;
}
/**
- @param textAlignment the textAlignment to set
- /
public void setTextAlignment(int textAlignment) {
this.textAlignment = textAlignment;
}
/**
- @param gap the vGap to set
- /
public void setVGap(int gap) {
vGap = gap;
}
/**
- @param borderColor the borderColor to set
- /
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
/**
- @param color the foregroundColor to set
- /
public void setForeground(Color color) {
foreground = color;
}
/**
- @param iconTextGap the iconTextGap to set
- /
public void setIconTextGap(int iconTextGap) {
this.iconTextGap = iconTextGap;
}
/**
- @param font the font to set
- /
public void setFont(Font font) {
this.font = font;
}
}
Conclusion :
Renommer le .zip en .jar et lancer: java -jar TitleBarBorder.jar
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.