Java/Swing JFC/Border — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:46, 1 июня 2010
Содержание
- 1 Adding a Title to a Border
- 2 An example of the MatteBorder class
- 3 An example of using a titled border on a label
- 4 Another TitledBorder 1
- 5 A simple demonstration of the LineBorder class built with rounded corners
- 6 BevelBorder
- 7 BevelBorder 2
- 8 Border Demo
- 9 Border of all kinds
- 10 Borders with a BevelBorder used on JLabels as a highlight
- 11 Bottom Border
- 12 Change border text Justification alignment style
- 13 CompoundBorder Demo
- 14 Create a title border from another border
- 15 Custom Border Sample
- 16 demonstrate the custom CurvedBorder class
- 17 Different Swing borders
- 18 EmptyBorder
- 19 EtchedBorder
- 20 Flex Border
- 21 How to create the border
- 22 Icon MatteBorder
- 23 LineBorder Demo
- 24 MatteBorder Demo
- 25 Oval border
- 26 Rectangle border
- 27 Red Green Border
- 28 Right border
- 29 Set title position
- 30 Soft BevelBorder
- 31 Solid border button border 3D border
- 32 TitledBorder
- 33 TitledBorder 3
- 34 TitledBorder Demo
Adding a Title to a Border
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
public class Main {
public static void main(String[] argv) {
TitledBorder titledBorder = BorderFactory.createTitledBorder("Title");
}
}
An example of the MatteBorder class
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
public class MatteExample extends JPanel {
public MatteExample() {
super(true);
this.setLayout(new GridLayout(1, 2, 5, 5));
JLabel label1 = new JLabel("Matte Border");
JLabel label2 = new JLabel("Matte Border (Icon)");
label1.setHorizontalAlignment(JLabel.CENTER);
label2.setHorizontalAlignment(JLabel.CENTER);
Icon icon = new ImageIcon("yourFile.gif");
MatteBorder matte = new MatteBorder(35, 35, 35, 35, Color.blue);
MatteBorder matteicon = new MatteBorder(35, 35, 35, 35, icon);
label1.setBorder(matte);
label2.setBorder(matteicon);
add(label1);
add(label2);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Matte Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setContentPane(new MatteExample());
frame.setVisible(true);
}
}
An example of using a titled border on a label
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class TitledExample extends JPanel {
public TitledExample() {
super(true);
this.setLayout(new GridLayout(1, 1, 5, 5));
JLabel label = new JLabel("Titled Border");
label.setHorizontalAlignment(JLabel.CENTER);
TitledBorder titled = new TitledBorder("Title");
label.setBorder(titled);
add(label);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 100);
frame.setContentPane(new TitledExample());
frame.setVisible(true);
}
}
Another TitledBorder 1
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
public class ATitledBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Titled Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border thisBorder = BorderFactory.createTitledBorder("Easy");
Icon thatIcon = new ImageIcon("diamond.gif");
Border thatBorder1 = new MatteBorder(18, 20, 18, 20, thatIcon);
Border thatBorder2 = new TitledBorder(thatBorder1, "Harder");
Font font = new Font("Serif", Font.ITALIC, 12);
Border thatBorder = new TitledBorder(thatBorder2, "Harder",
TitledBorder.LEFT, TitledBorder.ABOVE_BOTTOM, font, Color.red);
JButton thisButton = new JButton("Easy");
thisButton.setBorder(thisBorder);
JButton thatButton = new JButton("Harder");
thatButton.setBorder(thatBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(thisButton);
contentPane.add(thatButton);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
A simple demonstration of the LineBorder class built with rounded corners
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class RoundedLineBorder extends JPanel {
public RoundedLineBorder() {
super(true);
setLayout(new BorderLayout());
JLabel label = new JLabel("Rounded Corners");
label.setHorizontalAlignment(JLabel.CENTER);
LineBorder line = new LineBorder(Color.blue, 2, true);
label.setBorder(line);
add(label, BorderLayout.CENTER);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Rounded Line Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setContentPane(new RoundedLineBorder());
frame.setVisible(true);
}
}
BevelBorder
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
public class BorderSample {
public static void main(String args[]) {
JFrame frame = new JFrame("Sample Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border bevelBorder = new BevelBorder(BevelBorder.RAISED, Color.red,
Color.red.darker(), Color.pink, Color.pink.brighter());
Border emptyBorder = new EmptyBorder(5, 10, 5, 10);
Border etchedBorder = new EtchedBorder(EtchedBorder.RAISED, Color.red,
Color.pink);
Border lineBorder = new LineBorder(Color.red, 5);
Icon diamondIcon = new DiamondIcon(Color.red);
Border matteBorder = new MatteBorder(5, 10, 5, 10, diamondIcon);
Border softBevelBorder = new SoftBevelBorder(BevelBorder.RAISED,
Color.red, Color.red.darker(), Color.pink, Color.pink
.brighter());
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = new TitledBorder(lineBorder, "Hello",
TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font, Color.red);
Border compoundBorder = new CompoundBorder(lineBorder, matteBorder);
JLabel bevelLabel = new JLabel("Bevel");
bevelLabel.setBorder(bevelBorder);
bevelLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel emptyLabel = new JLabel("Empty");
emptyLabel.setBorder(emptyBorder);
emptyLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel etchedLabel = new JLabel("Etched");
etchedLabel.setBorder(etchedBorder);
etchedLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel lineLabel = new JLabel("Line");
lineLabel.setBorder(lineBorder);
lineLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel matteLabel = new JLabel("Matte");
matteLabel.setBorder(matteBorder);
matteLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel softBevelLabel = new JLabel("Soft Bevel");
softBevelLabel.setBorder(softBevelBorder);
softBevelLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel titledLabel = new JLabel("Titled");
titledLabel.setBorder(titledBorder);
titledLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel compoundLabel = new JLabel("Compound");
compoundLabel.setBorder(compoundBorder);
compoundLabel.setHorizontalAlignment(JLabel.CENTER);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(2, 4, 5, 5));
contentPane.add(bevelLabel);
contentPane.add(emptyLabel);
contentPane.add(etchedLabel);
contentPane.add(lineLabel);
contentPane.add(matteLabel);
contentPane.add(softBevelLabel);
contentPane.add(titledLabel);
contentPane.add(compoundLabel);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
class DiamondIcon implements Icon {
private Color color;
private boolean selected;
private int width;
private int height;
private Polygon poly;
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = 10;
public DiamondIcon(Color color) {
this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected) {
this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected, int width, int height) {
this.color = color;
this.selected = selected;
this.width = width;
this.height = height;
initPolygon();
}
private void initPolygon() {
poly = new Polygon();
int halfWidth = width / 2;
int halfHeight = height / 2;
poly.addPoint(0, halfHeight);
poly.addPoint(halfWidth, 0);
poly.addPoint(width, halfHeight);
poly.addPoint(halfWidth, height);
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.translate(x, y);
if (selected) {
g.fillPolygon(poly);
} else {
g.drawPolygon(poly);
}
g.translate(-x, -y);
}
}
BevelBorder 2
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
public class ABevelBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Bevel Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border raisedBorder = BorderFactory.createRaisedBevelBorder();
Border loweredBorder = BorderFactory.createLoweredBevelBorder();
Border myRaisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.pink, Color.red);
Border myLoweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.pink, Color.red);
JButton raisedButton = new JButton("Raised");
raisedButton.setBorder(raisedBorder);
JButton loweredButton = new JButton("Lowered");
loweredButton.setBorder(loweredBorder);
JButton myRaisedButton = new JButton("My Raised");
myRaisedButton.setBorder(myRaisedBorder);
JButton myLoweredButton = new JButton("My Lowered");
myLoweredButton.setBorder(myLoweredBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(2, 2, 5, 5));
contentPane.add(raisedButton);
contentPane.add(loweredButton);
contentPane.add(myRaisedButton);
contentPane.add(myLoweredButton);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Border Demo
/* From http://java.sun.ru/docs/books/tutorial/index.html */
/*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class BorderDemo extends JFrame {
public BorderDemo() {
super("BorderDemo");
Border blackline, etched, raisedbevel, loweredbevel, empty;
//A border that puts 10 extra pixels at the sides and
//bottom of each pane.
Border paneEdge = BorderFactory.createEmptyBorder(0, 10, 10, 10);
blackline = BorderFactory.createLineBorder(Color.black);
etched = BorderFactory.createEtchedBorder();
raisedbevel = BorderFactory.createRaisedBevelBorder();
loweredbevel = BorderFactory.createLoweredBevelBorder();
empty = BorderFactory.createEmptyBorder();
//First pane: simple borders
JPanel simpleBorders = new JPanel();
simpleBorders.setBorder(paneEdge);
simpleBorders.setLayout(new BoxLayout(simpleBorders, BoxLayout.Y_AXIS));
addCompForBorder(blackline, "line border", simpleBorders);
addCompForBorder(etched, "etched border", simpleBorders);
addCompForBorder(raisedbevel, "raised bevel border", simpleBorders);
addCompForBorder(loweredbevel, "lowered bevel border", simpleBorders);
addCompForBorder(empty, "empty border", simpleBorders);
//Second pane: matte borders
JPanel matteBorders = new JPanel();
matteBorders.setBorder(paneEdge);
matteBorders.setLayout(new BoxLayout(matteBorders, BoxLayout.Y_AXIS));
//XXX: We *should* size the component so that the
//XXX: icons tile OK. Without that, the icons are
//XXX: likely to be cut off and look bad.
ImageIcon icon = new ImageIcon("images/left.gif"); //20x22
Border border = BorderFactory.createMatteBorder(-1, -1, -1, -1, icon);
addCompForBorder(border, "matte border (-1,-1,-1,-1,icon)",
matteBorders);
border = BorderFactory.createMatteBorder(1, 5, 1, 1, Color.red);
addCompForBorder(border, "matte border (1,5,1,1,Color.red)",
matteBorders);
border = BorderFactory.createMatteBorder(0, 20, 0, 0, icon);
addCompForBorder(border, "matte border (0,20,0,0,icon)", matteBorders);
//Third pane: titled borders
JPanel titledBorders = new JPanel();
titledBorders.setBorder(paneEdge);
titledBorders.setLayout(new BoxLayout(titledBorders, BoxLayout.Y_AXIS));
TitledBorder titled;
titled = BorderFactory.createTitledBorder("title");
addCompForBorder(titled, "default titled border"
+ " (default just., default pos.)", titledBorders);
titled = BorderFactory.createTitledBorder(blackline, "title");
addCompForTitledBorder(titled, "titled line border"
+ " (centered, default pos.)", TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION, titledBorders);
titled = BorderFactory.createTitledBorder(etched, "title");
addCompForTitledBorder(titled, "titled etched border"
+ " (right just., default pos.)", TitledBorder.RIGHT,
TitledBorder.DEFAULT_POSITION, titledBorders);
titled = BorderFactory.createTitledBorder(loweredbevel, "title");
addCompForTitledBorder(titled, "titled lowered bevel border"
+ " (default just., above top)",
TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP,
titledBorders);
titled = BorderFactory.createTitledBorder(empty, "title");
addCompForTitledBorder(titled, "titled empty border"
+ " (default just., bottom)",
TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.BOTTOM,
titledBorders);
//Fourth pane: compound borders
JPanel compoundBorders = new JPanel();
compoundBorders.setBorder(paneEdge);
compoundBorders.setLayout(new BoxLayout(compoundBorders,
BoxLayout.Y_AXIS));
Border redline = BorderFactory.createLineBorder(Color.red);
Border compound;
compound = BorderFactory
.createCompoundBorder(raisedbevel, loweredbevel);
addCompForBorder(compound, "compound border (two bevels)",
compoundBorders);
compound = BorderFactory.createCompoundBorder(redline, compound);
addCompForBorder(compound, "compound border (add a red outline)",
compoundBorders);
titled = BorderFactory.createTitledBorder(compound, "title",
TitledBorder.CENTER, TitledBorder.BELOW_BOTTOM);
addCompForBorder(titled, "titled compound border"
+ " (centered, below bottom)", compoundBorders);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Simple", null, simpleBorders, null);
tabbedPane.addTab("Matte", null, matteBorders, null);
tabbedPane.addTab("Titled", null, titledBorders, null);
tabbedPane.addTab("Compound", null, compoundBorders, null);
tabbedPane.setSelectedIndex(0);
getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
void addCompForTitledBorder(TitledBorder border, String description,
int justification, int position, Container container) {
border.setTitleJustification(justification);
border.setTitlePosition(position);
addCompForBorder(border, description, container);
}
void addCompForBorder(Border border, String description, Container container) {
JPanel comp = new JPanel(false);
JLabel label = new JLabel(description, JLabel.CENTER);
comp.setLayout(new GridLayout(1, 1));
comp.add(label);
comp.setBorder(border);
container.add(Box.createRigidArea(new Dimension(0, 10)));
container.add(comp);
}
public static void main(String[] args) {
JFrame frame = new BorderDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
Border of all kinds
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
public class AnotherBorderTest extends JFrame {
public AnotherBorderTest() {
setTitle("Border Test");
setSize(450, 450);
JPanel content = (JPanel) getContentPane();
content.setLayout(new GridLayout(6, 2,3,3));
JPanel p = new JPanel();
p.setBorder(new BevelBorder(BevelBorder.RAISED));
p.add(new JLabel("RAISED BevelBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new BevelBorder(BevelBorder.LOWERED));
p.add(new JLabel("LOWERED BevelBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new LineBorder(Color.black, 5));
p.add(new JLabel("Black LineBorder, thickness = 5"));
content.add(p);
p = new JPanel();
p.setBorder(new EmptyBorder(10, 10, 10, 10));
p.add(new JLabel("EmptyBorder with thickness of 10"));
content.add(p);
p = new JPanel();
p.setBorder(new EtchedBorder(EtchedBorder.RAISED));
p.add(new JLabel("RAISED EtchedBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
p.add(new JLabel("LOWERED EtchedBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
p.add(new JLabel("RAISED SoftBevelBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
p.add(new JLabel("LOWERED SoftBevelBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new MatteBorder(new ImageIcon("BALL.GIF")));
p.add(new JLabel("MatteBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new TitledBorder(new MatteBorder(
new ImageIcon("jexpLogo.gif")), "Title String"));
p.add(new JLabel("TitledBorder using MatteBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new TitledBorder(new LineBorder(Color.black, 5),
"Title String"));
p.add(new JLabel("TitledBorder using LineBorder"));
content.add(p);
p = new JPanel();
p.setBorder(new TitledBorder(new EmptyBorder(10, 10, 10, 10),
"Title String"));
p.add(new JLabel("TitledBorder using EmptyBorder"));
content.add(p);
setVisible(true);
}
public static void main(String args[]) {
new AnotherBorderTest();
}
}
Borders with a BevelBorder used on JLabels as a highlight
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
public class BevelExample extends JPanel {
BevelBorder bevel;
EmptyBorder empty;
JLabel label[] = new JLabel[4];
public BevelExample() {
super(true);
setLayout(new GridLayout(1, 4));
bevel = new BevelBorder(BevelBorder.RAISED);
empty = new EmptyBorder(5, 5, 5, 5);
label[0] = new JLabel("Home");
label[1] = new JLabel("Back");
label[2] = new JLabel("Forward");
label[3] = new JLabel("Stop");
for (int i = 0; i < label.length; i++) {
label[i].setHorizontalAlignment(JLabel.CENTER);
label[i].addMouseListener(new RolloverListener());
label[i].setBorder(empty);
add(label[i]);
}
}
public static void main(String s[]) {
JFrame frame = new JFrame("Bevel Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 100);
frame.setContentPane(new BevelExample());
frame.setVisible(true);
}
// Inner class to respond to mouse events for the "rollover" effect
class RolloverListener extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
((JLabel) e.getComponent()).setBorder(bevel);
repaint();
}
public void mouseExited(MouseEvent e) {
((JLabel) e.getComponent()).setBorder(empty);
repaint();
}
public void mouseClicked(MouseEvent e) {
String text = ((JLabel) e.getComponent()).getText();
System.out.println("You clicked " + text + "!");
}
}
}
Bottom Border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.AbstractBorder;
public class BottomBorder extends AbstractBorder {
protected int thickness;
protected Color lineColor;
protected int gap;
public BottomBorder(Color color) {
this(color, 1, 1);
}
public BottomBorder(Color color, int thickness) {
this(color, thickness, thickness);
}
public BottomBorder(Color color, int thickness, int gap) {
lineColor = color;
this.thickness = thickness;
this.gap = gap;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
int i;
g.setColor(lineColor);
for (i = 0; i < thickness; i++) {
g.drawLine(x, y + height - i - 1, x + width, y + height - i - 1);
}
g.setColor(oldColor);
}
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, gap, 0);
}
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = 0;
insets.top = 0;
insets.right = 0;
insets.bottom = gap;
return insets;
}
/**
* Returns the color of the border.
*/
public Color getLineColor() {
return lineColor;
}
/**
* Returns the thickness of the border.
*/
public int getThickness() {
return thickness;
}
/**
* Returns whether or not the border is opaque.
*/
public boolean isBorderOpaque() {
return false;
}
public int getGap() {
return gap;
}
}
Change border text Justification alignment style
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
public static void main(String[] argv) {
LineBorder border = new LineBorder(Color.red);
TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Title");
// Or: DEFAULT_JUSTIFICATION, LEFT, RIGHT
titledBorder.setTitleJustification(TitledBorder.CENTER);
}
}
CompoundBorder Demo
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
import javax.swing.border.LineBorder;
public class ACompoundBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Compound Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border lineBorder = LineBorder.createBlackLineBorder();
Border bevelBorder = BorderFactory.createRaisedBevelBorder();
Border bevelLineBorder = new CompoundBorder(bevelBorder, lineBorder);
JButton bevelLineButton = new JButton("Bevel Line");
bevelLineButton.setBorder(bevelLineBorder);
Border redBorder = BorderFactory.createLineBorder(Color.red);
Border orangeBorder = BorderFactory.createLineBorder(Color.orange);
Border yellowBorder = BorderFactory.createLineBorder(Color.yellow);
Border greenBorder = BorderFactory.createLineBorder(Color.green);
Border blueBorder = BorderFactory.createLineBorder(Color.blue);
Border magentaBorder = BorderFactory.createLineBorder(Color.magenta);
Border twoColorBorder = new CompoundBorder(magentaBorder, blueBorder);
Border threeColorBorder = new CompoundBorder(twoColorBorder,
greenBorder);
Border fourColorBorder = new CompoundBorder(threeColorBorder,
yellowBorder);
Border fiveColorBorder = new CompoundBorder(fourColorBorder,
orangeBorder);
Border sixColorBorder = new CompoundBorder(fiveColorBorder, redBorder);
JButton rainbowButton = new JButton("Rainbow");
rainbowButton.setBorder(sixColorBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(bevelLineButton);
contentPane.add(rainbowButton);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Create a title border from another border
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
public static void main(String[] argv) {
LineBorder border = new LineBorder(Color.red);
TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Title");
}
}
Custom Border Sample
import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.Container;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class CustomBorderSample {
public static void main(String args[]) {
String labels[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };
JFrame frame = new JFrame("Custom Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JList jlist = new JList(labels);
ListCellRenderer renderer = new FocusedTitleListCellRenderer();
jlist.setCellRenderer(renderer);
JScrollPane sp = new JScrollPane(jlist);
contentPane.add(sp, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class FocusedTitleListCellRenderer implements ListCellRenderer {
protected static Border noFocusBorder = new EmptyBorder(15, 1, 1, 1);
protected static TitledBorder focusBorder = new TitledBorder(LineBorder
.createGrayLineBorder(), "Focused");
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
public String getTitle() {
return focusBorder.getTitle();
}
public void setTitle(String newValue) {
focusBorder.setTitle(newValue);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel renderer = (JLabel) defaultRenderer
.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
renderer.setBorder(cellHasFocus ? focusBorder : noFocusBorder);
return renderer;
}
}
demonstrate the custom CurvedBorder class
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.AbstractBorder;
public class CurvedExample extends JPanel {
public CurvedExample() {
super(true);
JSlider mySlider = new JSlider();
mySlider.setMajorTickSpacing(20);
mySlider.setMinorTickSpacing(10);
mySlider.setPaintTicks(true);
mySlider.setPaintLabels(true);
CurvedBorder border = new CurvedBorder(10, Color.darkGray);
mySlider.setBorder(border);
add(mySlider);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Custom Curved Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setContentPane(new CurvedExample());
frame.setVisible(true);
}
}
class CurvedBorder extends AbstractBorder {
private Color wallColor = Color.gray;
private int sinkLevel = 10;
public CurvedBorder() {
}
public CurvedBorder(int sinkLevel) {
this.sinkLevel = sinkLevel;
}
public CurvedBorder(Color wall) {
this.wallColor = wall;
}
public CurvedBorder(int sinkLevel, Color wall) {
this.sinkLevel = sinkLevel;
this.wallColor = wall;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
g.setColor(getWallColor());
// Paint a tall wall around the component
for (int i = 0; i < sinkLevel; i++) {
g.drawRoundRect(x + i, y + i, w - i - 1, h - i - 1, sinkLevel - i,
sinkLevel);
g.drawRoundRect(x + i, y + i, w - i - 1, h - i - 1, sinkLevel,
sinkLevel - i);
g.drawRoundRect(x + i, y, w - i - 1, h - 1, sinkLevel - i,
sinkLevel);
g.drawRoundRect(x, y + i, w - 1, h - i - 1, sinkLevel, sinkLevel
- i);
}
}
public Insets getBorderInsets(Component c) {
return new Insets(sinkLevel, sinkLevel, sinkLevel, sinkLevel);
}
public Insets getBorderInsets(Component c, Insets i) {
i.left = i.right = i.bottom = i.top = sinkLevel;
return i;
}
public boolean isBorderOpaque() {
return true;
}
public int getSinkLevel() {
return sinkLevel;
}
public Color getWallColor() {
return wallColor;
}
}
Different Swing borders
// : c14:Borders.java
// Different Swing borders.
// <applet code=Borders width=500 height=300></applet>
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
public class Borders extends JApplet {
static JPanel showBorder(Border b) {
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
String nm = b.getClass().toString();
nm = nm.substring(nm.lastIndexOf(".") + 1);
jp.add(new JLabel(nm, JLabel.CENTER), BorderLayout.CENTER);
jp.setBorder(b);
return jp;
}
public void init() {
Container cp = getContentPane();
cp.setLayout(new GridLayout(2, 4));
cp.add(showBorder(new TitledBorder("Title")));
cp.add(showBorder(new EtchedBorder()));
cp.add(showBorder(new LineBorder(Color.BLUE)));
cp.add(showBorder(new MatteBorder(5, 5, 30, 30, Color.GREEN)));
cp.add(showBorder(new BevelBorder(BevelBorder.RAISED)));
cp.add(showBorder(new SoftBevelBorder(BevelBorder.LOWERED)));
cp.add(showBorder(new CompoundBorder(new EtchedBorder(),
new LineBorder(Color.RED))));
}
public static void main(String[] args) {
run(new Borders(), 500, 300);
}
public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
EmptyBorder
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class AnEmptyBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Empty Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border emptyBorder = BorderFactory.createEmptyBorder(20, 20, 0, 0);
JButton emptyButton = new JButton("With Empty");
emptyButton.setBorder(emptyBorder);
JButton nonemptyButton = new JButton("Without Empty");
Container contentPane = frame.getContentPane();
contentPane.add(emptyButton, BorderLayout.NORTH);
contentPane.add(nonemptyButton, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
EtchedBorder
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class AnEtchedBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Etched Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);
Border loweredBorder = new EtchedBorder(EtchedBorder.LOWERED);
JButton raisedButton = new JButton("Raised");
raisedButton.setBorder(raisedBorder);
JButton loweredButton = new JButton("Lowered");
loweredButton.setBorder(loweredBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 2, 5, 5));
contentPane.add(raisedButton);
contentPane.add(loweredButton);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Flex Border
// FlexBorder
//
// Copyright (C) by Andrea Carboni.
// This file may be distributed under the terms of the LGPL license.
//
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.Border;
public class FlexBorder implements Border
{
public static final int NONE = 0;
public static final int TOP_LINE = 1;
public static final int BOTTOM_LINE = 2;
public static final int RECT = 3;
private Insets insets = new Insets(0,0,0,0);
private Color color = Color.blue;
private int type = RECT;
//---------------------------------------------------------------------------
public FlexBorder()
{
}
//---------------------------------------------------------------------------
public FlexBorder(Color c, int t)
{
color = c;
type = t;
}
//---------------------------------------------------------------------------
public void setColor(Color c)
{
color = c;
}
//---------------------------------------------------------------------------
public void setType(int t)
{
type = t;
}
//---------------------------------------------------------------------------
//---
//--- Border interface methods
//---
//---------------------------------------------------------------------------
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
{
g.setColor(color);
switch(type)
{
case TOP_LINE: g.drawLine(x,y, x+width-1, y);
break;
case BOTTOM_LINE: g.drawLine(x,y+height-1, x+width-1, y+height-1);
break;
case RECT: g.drawRect(x, y, width-1, height-1);
break;
}
}
//---------------------------------------------------------------------------
public Insets getBorderInsets(Component c)
{
return insets;
}
//---------------------------------------------------------------------------
public boolean isBorderOpaque() { return false; }
}
//
How to create the border
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class BorderTest extends JFrame {
public BorderTest() {
JPanel p = new JPanel();
Border[] border = new Border[] { BorderFactory.createEtchedBorder(),
BorderFactory.createTitledBorder("Border types"),
BorderFactory.createLoweredBevelBorder(),
BorderFactory.createRaisedBevelBorder(),
BorderFactory.createEtchedBorder(),
BorderFactory.createLineBorder(Color.blue),
BorderFactory.createMatteBorder(10, 10, 10, 10, Color.blue),
BorderFactory.createEmptyBorder()
};
p.setLayout(new GridLayout(border.length, 0, 3, 3));
for (int i = 0; i < border.length; i++) {
JPanel borderPanel = new JPanel();
borderPanel.setBorder(border[i]);
p.add(borderPanel);
}
getContentPane().add(p, "Center");
setTitle("BorderTest");
setSize(600, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
JFrame frame = new BorderTest();
frame.show();
}
}
Icon MatteBorder
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
public class IconMatteBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Icon Matted Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon diamondIcon = new DiamondIcon(Color.red, true, 10, 10);
Border diamondBorder = new MatteBorder(10, 10, 10, 10, diamondIcon);
JButton diamondButton = new JButton("diamonds");
diamondButton.setBorder(diamondBorder);
Icon diamondIcon2 = new ImageIcon("diamond.gif");
Border diamondBorder2 = new MatteBorder(36, 40, 36, 40, diamondIcon2);
JButton diamondButton2 = new JButton("diamonds2");
diamondButton2.setBorder(diamondBorder2);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(2, 1));
contentPane.add(diamondButton);
contentPane.add(diamondButton2);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class DiamondIcon implements Icon {
private Color color;
private boolean selected;
private int width;
private int height;
private Polygon poly;
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = 10;
public DiamondIcon(Color color) {
this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected) {
this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected, int width, int height) {
this.color = color;
this.selected = selected;
this.width = width;
this.height = height;
initPolygon();
}
private void initPolygon() {
poly = new Polygon();
int halfWidth = width / 2;
int halfHeight = height / 2;
poly.addPoint(0, halfHeight);
poly.addPoint(halfWidth, 0);
poly.addPoint(width, halfHeight);
poly.addPoint(halfWidth, height);
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.translate(x, y);
if (selected) {
g.fillPolygon(poly);
} else {
g.drawPolygon(poly);
}
g.translate(-x, -y);
}
}
LineBorder Demo
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class ALineBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Line Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border thinBorder = LineBorder.createBlackLineBorder();
Border thickBorder = new LineBorder(Color.white, 12);
Border roundedBorder = new LineBorder(Color.black, 12, true);
JButton thinButton = new JButton("1 Pixel");
thinButton.setBorder(thinBorder);
JButton thickButton = new JButton("12 Pixel");
thickButton.setBorder(thickBorder);
JButton roundedButton = new JButton("Rounded 12 Pixel");
roundedButton.setBorder(roundedBorder);
Container contentPane = frame.getContentPane();
contentPane.add(thinButton, BorderLayout.NORTH);
contentPane.add(thickButton, BorderLayout.CENTER);
contentPane.add(roundedButton, BorderLayout.SOUTH);
frame.pack();
frame.setSize(300, frame.getHeight());
frame.setVisible(true);
}
}
MatteBorder Demo
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
public class ColorMatteBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Color Matted Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border solidBorder = new MatteBorder(10, 5, 2, 20, Color.red);
JButton solidButton = new JButton("10x5x2x20");
solidButton.setBorder(solidBorder);
Container contentPane = frame.getContentPane();
contentPane.add(solidButton, BorderLayout.CENTER);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Oval border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class OvalBorder implements Border {
protected int ovalWidth = 6;
protected int ovalHeight = 6;
protected Color lightColor = Color.white;
protected Color darkColor = Color.gray;
public OvalBorder() {
ovalWidth = 6;
ovalHeight = 6;
}
public OvalBorder(int w, int h) {
ovalWidth = w;
ovalHeight = h;
}
public OvalBorder(int w, int h, Color topColor, Color bottomColor) {
ovalWidth = w;
ovalHeight = h;
lightColor = topColor;
darkColor = bottomColor;
}
public Insets getBorderInsets(Component c) {
return new Insets(ovalHeight, ovalWidth, ovalHeight, ovalWidth);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
width--;
height--;
g.setColor(lightColor);
g.drawLine(x, y + height - ovalHeight, x, y + ovalHeight);
g.drawArc(x, y, 2 * ovalWidth, 2 * ovalHeight, 180, -90);
g.drawLine(x + ovalWidth, y, x + width - ovalWidth, y);
g.drawArc(x + width - 2 * ovalWidth, y, 2 * ovalWidth, 2 * ovalHeight,
90, -90);
g.setColor(darkColor);
g.drawLine(x + width, y + ovalHeight, x + width, y + height
- ovalHeight);
g.drawArc(x + width - 2 * ovalWidth, y + height - 2 * ovalHeight,
2 * ovalWidth, 2 * ovalHeight, 0, -90);
g
.drawLine(x + ovalWidth, y + height, x + width - ovalWidth, y
+ height);
g.drawArc(x, y + height - 2 * ovalHeight, 2 * ovalWidth,
2 * ovalHeight, -90, -90);
}
public static void main(String[] s) {
JFrame f = new JFrame("Oval Border");
f.setSize(100, 100);
JPanel p = new JPanel(new GridLayout(0, 1, 5, 5));
JLabel l = new JLabel("Oval Border");
l.setBorder(new OvalBorder());
p.add(l);
p.setBorder(new OvalBorder());
f.getContentPane().add(p);
f.show();
}
}
Rectangle border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.AbstractBorder;
public class RectangleBorder extends AbstractBorder {
protected Insets thickness;
protected Color lineColor;
protected Insets gap;
public RectangleBorder(Color color) {
this(color, new Insets(1, 1, 1, 1));
}
public RectangleBorder(Color color, Insets thickness) {
this(color, thickness, thickness);
}
public RectangleBorder(Color color, Insets thickness, Insets gap) {
lineColor = color;
this.thickness = thickness;
this.gap = gap;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
g.setColor(lineColor);
// top
for (int i = 0; i < thickness.top; i++) {
g.drawLine(x, y + i, x + width, y + i);
}
// bottom
for (int i = 0; i < thickness.bottom; i++) {
g.drawLine(x, y + height - i - 1, x + width, y + height - i - 1);
}
// right
for (int i = 0; i < thickness.right; i++) {
g.drawLine(x + width - i - 1, y, x + width - i - 1, y + height);
}
// left
for (int i = 0; i < thickness.left; i++) {
g.drawLine(x + i, y, x + i, y + height);
}
g.setColor(oldColor);
}
/**
* Returns the insets of the border.
*
* @param c
* the component for which this border insets value applies
*/
public Insets getBorderInsets(Component c) {
return gap;
}
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = gap.left;
insets.top = gap.top;
insets.right = gap.right;
insets.bottom = gap.bottom;
return insets;
}
/**
* Returns the color of the border.
*/
public Color getLineColor() {
return lineColor;
}
/**
* Returns the thickness of the border.
*/
public Insets getThickness() {
return thickness;
}
/**
* Returns whether or not the border is opaque.
*/
public boolean isBorderOpaque() {
return false;
}
public Insets getGap() {
return gap;
}
}
Red Green Border
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
public class RedGreenBorder extends AbstractBorder {
public boolean isBorderOpaque() {
return true;
}
public Insets getBorderInsets(Component c) {
return new Insets(3, 3, 3, 3);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Insets insets = getBorderInsets(c);
Color horizontalColor;
Color verticalColor;
if (c.isEnabled()) {
boolean pressed = false;
if (c instanceof AbstractButton) {
ButtonModel model = ((AbstractButton) c).getModel();
pressed = model.isPressed();
}
if (pressed) {
horizontalColor = Color.red;
verticalColor = Color.green;
} else {
horizontalColor = Color.green;
verticalColor = Color.red;
}
} else {
horizontalColor = Color.lightGray;
verticalColor = Color.lightGray;
}
g.setColor(horizontalColor);
g.translate(x, y);
// top
g.fillRect(0, 0, width, insets.top);
// bottom
g.fillRect(0, height - insets.bottom, width, insets.bottom);
g.setColor(verticalColor);
// left
g.fillRect(0, insets.top, insets.left, height - insets.top - insets.bottom);
g.fillRect(width - insets.right, insets.top, insets.right, height- insets.top - insets.bottom);
g.translate(-x, -y);
}
public static void main(String args[]) {
JFrame frame = new JFrame("My Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border border = new RedGreenBorder();
JButton helloButton = new JButton("Hello");
helloButton.setBorder(border);
JButton braveButton = new JButton("Brave New");
braveButton.setBorder(border);
braveButton.setEnabled(false);
JButton worldButton = new JButton("World");
worldButton.setBorder(border);
Container contentPane = frame.getContentPane();
contentPane.add(helloButton, BorderLayout.NORTH);
contentPane.add(braveButton, BorderLayout.CENTER);
contentPane.add(worldButton, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Right border
//com.amkai.borders;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.border.AbstractBorder;
public class RightSideBorder extends AbstractBorder
{
protected int thickness;
protected Color lineColor;
protected int gap;
public RightSideBorder(Color color) {
this(color, 1, 1);
}
public RightSideBorder(Color color, int thickness) {
this(color, thickness, thickness);
}
public RightSideBorder(Color color, int thickness, int gap) {
lineColor = color;
this.thickness = thickness;
this.gap = gap;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
int i;
g.setColor(lineColor);
for(i = 0; i < thickness; i++) {
g.drawLine(x+width-i-1, y, x+width-i-1, y+height);
}
g.setColor(oldColor);
}
/**
* Returns the insets of the border.
* @param c the component for which this border insets value applies
*/
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, 0,gap);
}
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = 0;
insets.top = 0;
insets.right = gap;
insets.bottom = 0;
return insets;
}
/**
* Returns the color of the border.
*/
public Color getLineColor() {
return lineColor;
}
/**
* Returns the thickness of the border.
*/
public int getThickness() {
return thickness;
}
/**
* Returns whether or not the border is opaque.
*/
public boolean isBorderOpaque() {
return false;
}
public int getGap() {
return gap;
}
}
Set title position
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
public static void main(String[] argv) {
LineBorder border = new LineBorder(Color.red);
TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Title");
// Or: DEFAULT_POSITION, ABOVE_TOP, TOP, ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM
titledBorder.setTitlePosition(TitledBorder.BELOW_TOP);
}
}
Soft BevelBorder
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.SoftBevelBorder;
public class ASoftBevelBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Soft Bevel Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border raisedBorder = new SoftBevelBorder(SoftBevelBorder.RAISED);
Border loweredBorder = new SoftBevelBorder(SoftBevelBorder.LOWERED);
JButton raisedButton = new JButton("Raised");
raisedButton.setBorder(raisedBorder);
JButton loweredButton = new JButton("Lowered");
loweredButton.setBorder(loweredBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(raisedButton);
contentPane.add(loweredButton);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Solid border button border 3D border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
import javax.swing.border.EmptyBorder;
public class SolidBorder implements Border {
protected Color topColor = Color.white;
protected Color bottomColor = Color.gray;
public SolidBorder() {
}
public Insets getBorderInsets(Component c) {
return new Insets(2, 2, 2, 2);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
width--;
height--;
g.setColor(topColor);
g.drawLine(x, y + height, x, y);
g.drawLine(x , y, x + width , y);
g.setColor(bottomColor);
g.drawLine(x + width, y, x + width, y + height);
g.drawLine(x , y + height, x + width, y + height);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Custom Border: SolidBorder");
JLabel label = new JLabel("SolidBorder");
((JPanel) frame.getContentPane()).setBorder(new CompoundBorder(
new EmptyBorder(10, 10, 10, 10), new SolidBorder()));
frame.getContentPane().add(label);
frame.setBounds(0, 0, 300, 150);
frame.setVisible(true);
}
}
TitledBorder
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
public class TitledJustBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Justified Titled Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TitledBorder leftBorder = BorderFactory.createTitledBorder("Left");
leftBorder.setTitleJustification(TitledBorder.LEFT);
JButton leftButton = new JButton();
leftButton.setBorder(leftBorder);
TitledBorder rightBorder = BorderFactory.createTitledBorder("Right");
rightBorder.setTitleJustification(TitledBorder.RIGHT);
JButton rightButton = new JButton();
rightButton.setBorder(rightBorder);
TitledBorder centerBorder = BorderFactory.createTitledBorder("Center");
centerBorder.setTitleJustification(TitledBorder.CENTER);
JButton centerButton = new JButton();
centerButton.setBorder(centerBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(3, 1));
contentPane.add(leftButton);
contentPane.add(rightButton);
contentPane.add(centerButton);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
TitledBorder 3
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
public class TitledPostBorder {
public static void main(String args[]) {
JFrame frame = new JFrame("Positioned Titled Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TitledBorder aboveTopBorder = BorderFactory
.createTitledBorder("AboveTop");
aboveTopBorder.setTitlePosition(TitledBorder.ABOVE_TOP);
JButton aboveTopButton = new JButton();
aboveTopButton.setBorder(aboveTopBorder);
TitledBorder topBorder = BorderFactory.createTitledBorder("Top");
topBorder.setTitlePosition(TitledBorder.TOP);
JButton topButton = new JButton();
topButton.setBorder(topBorder);
TitledBorder belowTopBorder = BorderFactory
.createTitledBorder("BelowTop");
belowTopBorder.setTitlePosition(TitledBorder.BELOW_TOP);
JButton belowTopButton = new JButton();
belowTopButton.setBorder(belowTopBorder);
TitledBorder aboveBottomBorder = BorderFactory
.createTitledBorder("AboveBottom");
aboveBottomBorder.setTitlePosition(TitledBorder.ABOVE_BOTTOM);
JButton aboveBottomButton = new JButton();
aboveBottomButton.setBorder(aboveBottomBorder);
TitledBorder bottomBorder = BorderFactory.createTitledBorder("Bottom");
bottomBorder.setTitlePosition(TitledBorder.BOTTOM);
JButton bottomButton = new JButton();
bottomButton.setBorder(bottomBorder);
TitledBorder belowBottomBorder = BorderFactory
.createTitledBorder("BelowBottom");
belowBottomBorder.setTitlePosition(TitledBorder.BELOW_BOTTOM);
JButton belowBottomButton = new JButton();
belowBottomButton.setBorder(belowBottomBorder);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(3, 2));
contentPane.add(aboveTopButton);
contentPane.add(aboveBottomButton);
contentPane.add(topButton);
contentPane.add(bottomButton);
contentPane.add(belowTopButton);
contentPane.add(belowBottomButton);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
TitledBorder Demo
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
public class DoubleTitle {
public static void main(String args[]) {
JFrame frame = new JFrame("Double Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TitledBorder topBorder = BorderFactory.createTitledBorder("Top");
topBorder.setTitlePosition(TitledBorder.TOP);
TitledBorder doubleBorder = new TitledBorder(topBorder, "Bottom",
TitledBorder.RIGHT, TitledBorder.BOTTOM);
JButton doubleButton = new JButton();
doubleButton.setBorder(doubleBorder);
Container contentPane = frame.getContentPane();
contentPane.add(doubleButton, BorderLayout.CENTER);
frame.setSize(300, 100);
frame.setVisible(true);
}
}