Note: This article goes from: Summary of the usage of the Swing programming border (Border). The content according to the author understanding a little finishing.
Function Description:
public void SetBorder (Border Border)
Sets the bounding rectangle for this component. The Border object is responsible for defining the insets of the component (overriding all insets settings directly on the component) and optionally rendering the border decorations within these insets ranges. To create decorative and non-decorative areas (such as margins and padding) for swing components, you should use a border (not insets). You can use compound borders to nest multiple borders in a single component. While it is technically possible to set a border on all objects that inherit from JComponent, many of the standard Swing components ' appearance implementations do not normally use the user-set border. In general, if you want to set a border on a standard Swing component instead of JPanel or JLabel, it is recommended that you put the component in JPanel and set the border on the JPanel.
The definition of the Border object is primarily done through borderfactory, which is the focus of this article, as explained by the API: Borderfactory provides a factory class for standard border objects. Wherever possible, this factory class will provide a reference to the shared border instance. The following is a discussion of the specific application methods:
ImportJava.awt.Color;Importjava.awt.Dimension;Importjava.awt.GridLayout;Importjavax.swing.*;ImportJavax.swing.border.Border;ImportJavax.swing.border.TitledBorder; Public classSetborderdemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubJPanel Panel =NewJPanel (); Panel.setlayout (NewGridLayout (0, 2, 5, 10)); Panel.setborder (Borderfactory.createemptyborder (5, 5, 5, 5)); JButton P1=NewJButton (); P1.setborder (Borderfactory.createlineborder (color.red,3)); P1.add (NewJLabel ("line Border")); Panel.add (p1); JPanel P2=NewJPanel (); P2.setborder (Borderfactory.createetchedborder ()); P2.add (NewJLabel ("Etched border")); Panel.add (p2); JPanel P3=NewJPanel (); P3.setborder (Borderfactory.createraisedbevelborder ()); P3.add (NewJLabel ("Bevel border (convex)")); Panel.add (p3); JPanel P4=NewJPanel (); P4.setborder (Borderfactory.createloweredbevelborder ()); P4.add (NewJLabel ("beveled border (concave)")); Panel.add (p4); JPanel P5=NewJPanel (); P5.setborder (Borderfactory.createtitledborder (Title)); P5.add (NewJLabel ("caption Border")); Panel.add (p5); JPanel P6=NewJPanel (); Titledborder TB= Borderfactory.createtitledborder ("title"); Tb.settitlejustification (Titledborder.right); P6.setborder (TB); P6.add (NewJLabel ("label border (right)")); Panel.add (P6); JPanel P7=NewJPanel (); P7.setborder (Borderfactory.creatematteborder (1, 5, 1, 1, Color.yellow)); P7.add (NewJLabel ("Color border")); Panel.add (P7); JPanel P8=NewJPanel (); Border B1= Borderfactory.createlineborder (Color.Blue, 2); Border B2=Borderfactory.createetchedborder (); P8.setborder (Borderfactory.createcompoundborder (B1, B2)); P8.add (NewJLabel ("Combo Border")); Panel.add (P8); JFrame Frame=NewJFrame ("Swing Border Demo"); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.getcontentpane (). Add (panel); Frame.pack (); Frame.setvisible (true); }}
The effect is as follows:
Run
It is important to note that between panel and frame there is a emptyborder with a width of 5 pixels, which only occupies space and is not displayed.
Swing-setborder () Usage-Getting Started