Java by API/javax.swing/BorderFactory

Материал из Java эксперт
Перейти к: навигация, поиск

BorderFactory: createBevelBorder(int type,Color highlight,Color shadow)

 
import java.awt.Color;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Justified Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border myRaisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED,
        Color.PINK, Color.RED);
    JButton myRaisedButton = new JButton("My Raised");
    myRaisedButton.setBorder(myRaisedBorder);
    Container contentPane = frame.getContentPane();
    contentPane.add(myRaisedButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





BorderFactory: createCompoundBorder(Border outsideBorder, Border insideBorder)

 
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class MainClass extends JFrame {
  public MainClass(String name) {
    getContentPane().setLayout(new FlowLayout());
    JLabel labelTwo = new JLabel("www.jexp.ru");
    labelTwo.setBorder(BorderFactory.createEtchedBorder());
    
    add(labelTwo);
    
    
    JLabel labelThree = new JLabel("MatteBorder");
    labelThree.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.pink));
    add(labelThree);
    
    
    JLabel labelFour = new JLabel("TitledBorder");
    labelFour.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(10, 10,
        10, 10, Color.pink), "Title", TitledBorder.RIGHT, TitledBorder.BOTTOM));
    add(labelFour);
    
    JLabel labelSix = new JLabel("CompoundBorder");
    Border one = BorderFactory.createEtchedBorder();
    Border two = BorderFactory.createMatteBorder(4, 4, 4, 4, Color.blue);
    labelSix.setBorder(BorderFactory.createCompoundBorder(one, two));
    add(labelSix);
    
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





BorderFactory: createEmptyBorder(int top,int left,int bottom,int right)

 
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 MainClass {
  public static void main(String[] a) {
    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.pack();
    frame.setSize(300, frame.getHeight());
    frame.setVisible(true);
  }
}





BorderFactory: createEtchedBorder()

 
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class MainClass extends JFrame {
  public MainClass(String name) {
    getContentPane().setLayout(new FlowLayout());
    JLabel labelTwo = new JLabel("www.jexp.ru");
    labelTwo.setBorder(BorderFactory.createEtchedBorder());
    
    add(labelTwo);
    
    
    JLabel labelThree = new JLabel("MatteBorder");
    labelThree.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.pink));
    add(labelThree);
    
    
    JLabel labelFour = new JLabel("TitledBorder");
    labelFour.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(10, 10,
        10, 10, Color.pink), "Title", TitledBorder.RIGHT, TitledBorder.BOTTOM));
    add(labelFour);
    
    JLabel labelSix = new JLabel("CompoundBorder");
    Border one = BorderFactory.createEtchedBorder();
    Border two = BorderFactory.createMatteBorder(4, 4, 4, 4, Color.blue);
    labelSix.setBorder(BorderFactory.createCompoundBorder(one, two));
    add(labelSix);
    
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





BorderFactory: createLineBorder(Color color)

 
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Main {
  public static void main(String[] args) {
    JLabel label1 = new JLabel("BottomRight", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("CenterLeft", SwingConstants.LEFT);
    JLabel label3 = new JLabel("TopCenter", SwingConstants.CENTER);
    label1.setVerticalAlignment(SwingConstants.BOTTOM);
    label2.setVerticalAlignment(SwingConstants.CENTER);
    label3.setVerticalAlignment(SwingConstants.TOP);
    label1.setBorder(BorderFactory.createLineBorder(Color.black));
    label2.setBorder(BorderFactory.createLineBorder(Color.black));
    label3.setBorder(BorderFactory.createLineBorder(Color.black));
    JFrame frame = new JFrame("AlignmentExample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new GridLayout(3, 1, 8, 8));
    p.add(label1);
    p.add(label2);
    p.add(label3);
    p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    frame.setContentPane(p);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}





BorderFactory: createLineBorder(Color color, int thickness)

 
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 Main {
  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.MAGENTA, 2);
    Border orangeBorder = BorderFactory.createLineBorder(Color.BLUE, 2);
    Border yellowBorder = BorderFactory.createLineBorder(Color.YELLOW, 5);
    Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 2);
    Border blueBorder = BorderFactory.createLineBorder(Color.ORANGE, 4);
    Border magentaBorder = BorderFactory.createLineBorder(Color.RED, 3);
    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);
  }
}





BorderFactory: createLoweredBevelBorder()

 
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Justified Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border loweredBorder = BorderFactory.createLoweredBevelBorder();
    JButton loweredButton = new JButton("Lowered");
    loweredButton.setBorder(loweredBorder);
    
    Container contentPane = frame.getContentPane();
    contentPane.add(loweredButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





BorderFactory: createMatteBorder(int top, int left, int bottom, int right, Color color)

 
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class MainClass extends JFrame {
  public MainClass(String name) {
    getContentPane().setLayout(new FlowLayout());
    JLabel labelTwo = new JLabel("www.jexp.ru");
    labelTwo.setBorder(BorderFactory.createEtchedBorder());
    
    add(labelTwo);
    
    
    JLabel labelThree = new JLabel("MatteBorder");
    labelThree.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.pink));
    add(labelThree);
    
    
    JLabel labelFour = new JLabel("TitledBorder");
    labelFour.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(10, 10,
        10, 10, Color.pink), "Title", TitledBorder.RIGHT, TitledBorder.BOTTOM));
    add(labelFour);
    
    JLabel labelSix = new JLabel("CompoundBorder");
    Border one = BorderFactory.createEtchedBorder();
    Border two = BorderFactory.createMatteBorder(4, 4, 4, 4, Color.blue);
    labelSix.setBorder(BorderFactory.createCompoundBorder(one, two));
    add(labelSix);
    
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





BorderFactory: createRaisedBevelBorder()

 
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Justified Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border raisedBorder = BorderFactory.createRaisedBevelBorder();
    JButton raisedButton = new JButton("Raised");
    raisedButton.setBorder(raisedBorder);
    
    Container contentPane = frame.getContentPane();
    contentPane.add(raisedButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





BorderFactory: createTitledBorder(Border b, String t, int j, int p)

 
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class MainClass extends JFrame {
  public MainClass(String name) {
    getContentPane().setLayout(new FlowLayout());
    JLabel labelTwo = new JLabel("www.jexp.ru");
    labelTwo.setBorder(BorderFactory.createEtchedBorder());
    
    add(labelTwo);
    
    
    JLabel labelThree = new JLabel("MatteBorder");
    labelThree.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.pink));
    add(labelThree);
    
    
    JLabel labelFour = new JLabel("TitledBorder");
    labelFour.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(10, 10,
        10, 10, Color.pink), "Title", TitledBorder.RIGHT, TitledBorder.BOTTOM));
    add(labelFour);
    
    JLabel labelSix = new JLabel("CompoundBorder");
    Border one = BorderFactory.createEtchedBorder();
    Border two = BorderFactory.createMatteBorder(4, 4, 4, 4, Color.blue);
    labelSix.setBorder(BorderFactory.createCompoundBorder(one, two));
    add(labelSix);
    
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





BorderFactory.createTitledBorder(String text)

 
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JProgressBar Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JProgressBar progressBar = new JProgressBar();
    progressBar.setValue(25);
    progressBar.setStringPainted(true);
    Border border = BorderFactory.createTitledBorder("Reading...");
    progressBar.setBorder(border);
    f.add(progressBar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}