[Java的挥杆] 在Java中使用边境

简单的边界
开放边界
标题边界
结合边界

不时不写左右摇摆, 在这篇文章中,我会引导你去学习如何使用边界. 从字面上看是在Java中的轮廓. 我们将转向找出 4 轮廓所列类别.

使用简单的边框

创建边界, 您使用以下语法,并根据边界的类型,你想创建.
边境边界= BorderFactory.create…
现在我们的代码来创建一些简单的边框,如下图所示.

在Java中使用简单的边框

线边界 与某些相关的一种边界清晰彩色
空边框 边框是空的, 它经常被用于创建空间, 作为用于填充用来把内容和外部之间的距离.
募集浮雕化边框 边界清格式的流行, 浮雕上.
阴刻浮雕边框 蚀刻边框,但沉没低于.
募集斜面边框 为边界的形式出现斜边.
降低斜面边框 是创建的格式边框转角水槽.

package nguyenvanquan7826.border;

import java.awt.BorderLayout;
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.border.Border;
import javax.swing.border.EtchedBorder;

/**
 * --------------------- @author nguyenvanquan7826 ---------------------
 * ------------------ website: cachhoc.net -------------------
 */
public class DemoSimpleBorders extends JFrame {

  /** create border type */

  private Border lineBorder = BorderFactory.createLineBorder(Color.blue);
  private Border raisedEtched = BorderFactory
      .createEtchedBorder(EtchedBorder.RAISED);
  private Border loweredEtched = BorderFactory
      .createEtchedBorder(EtchedBorder.LOWERED);
  private Border raisedBevel = BorderFactory.createRaisedBevelBorder();
  private Border loweredBevel = BorderFactory.createLoweredBevelBorder();
  private Border empty = BorderFactory.createEmptyBorder();

  /**
   * create border empty for JPanel contain content
   */
  private Border panelBorder = BorderFactory
      .createEmptyBorder(20, 20, 20, 20);

  public DemoSimpleBorders() {
    add(createContent());
    setDisplay();
  }

  private void setDisplay() {
    setTitle("Demo Simple Border");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * create JPanel contain six JPanel with borders
   */
  private JPanel createContent() {
    JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10));
    panel.setBorder(panelBorder);
    panel.add(createPaneBorder(lineBorder, "line border"));
    panel.add(createPaneBorder(empty, "empty border"));
    panel.add(createPaneBorder(raisedEtched, "raised etched"));
    panel.add(createPaneBorder(loweredEtched, "lowered etched border"));
    panel.add(createPaneBorder(raisedBevel, "raised bevel border"));
    panel.add(createPaneBorder(loweredBevel, "lowered bevel border"));
    return panel;
  }

  /**
   * create JPanel contain a border
   */
  private JPanel createPaneBorder(Border border, String content) {
    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(border);
    JLabel lb = new JLabel(content, JLabel.CENTER);
    panel.add(lb);
    return panel;
  }

  public static void main(String[] args) {
    new DemoSimpleBorders();
  }
}

使用磨砂边框

边框磨砂是一种格式,使我们能够使用生成的颜色或轮廓图像. 它也给我们提出了另一个层面的边缘. 但是,拍照的时候,你应该把数据大小 -1 同时尺寸出现不受干扰, 不会丢失. 代码示例,.

在Java磨砂边框

package nguyenvanquan7826.border;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 * --------------------- @author nguyenvanquan7826 ---------------------
 * ------------------ website: cachhoc.net -------------------
 */
public class DemoMatteBorder extends JFrame {

  private Border matteBorderColor;
  private Border matteBorderIcon;

  /**
   * create border empty for JPanel contain content
   */
  private Border panelBorder = BorderFactory
      .createEmptyBorder(20, 20, 20, 20);

  public DemoMatteBorder() {
    createBorder();
    add(createContent());
    setDisplay();
  }

  private void setDisplay() {
    setTitle("Demo Matte Border");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * create JPanel contain six JPanel with borders
   */
  private JPanel createContent() {
    JPanel panel = new JPanel(new GridLayout(2, 1, 10, 10));
    panel.setBorder(panelBorder);

    panel.add(createPaneBorder(matteBorderColor, "matte border with color"));
    if (matteBorderIcon != null) {
      panel.add(createPaneBorder(matteBorderIcon,
          "matte border with icon"));
    } else {
      panel.add(createPaneBorder(matteBorderColor, "don't found icon"));
    }

    return panel;
  }

  /**
   * create JPanel contain a border
   */
  private JPanel createPaneBorder(Border border, String content) {
    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(border);
    JLabel lb = new JLabel(content, JLabel.CENTER);
    panel.add(lb);
    return panel;
  }

  /**
   * create types border
   */
  private void createBorder() {
    matteBorderColor = BorderFactory.createMatteBorder(1, 1, 5, 5,
        Color.blue);
    ImageIcon icon = createIcon("icon.gif");
    if (icon != null) {
      matteBorderIcon = BorderFactory.createMatteBorder(-1, -1, -1, -1,
          icon);
    }
  }

  /**
   * create a icon from a path
   */
  private ImageIcon createIcon(String path) {
    URL url = getClass().getResource(path);
    if (url != null) {
      return new ImageIcon(url);
    } else {
      return null;
    }
  }

  public static void main(String[] args) {
    new DemoMatteBorder();
  }
}

使用标题边框

标题边框是一种形式的标题边框. 你可以把左边的称号, 权, 之间, 在下边框,… 此外,您可以设置边界已创建,在某些形式的边界, 否则,默认的颜色为蓝色边框将被模糊.

在java中的标题边界

package nguyenvanquan7826.border;

import java.awt.BorderLayout;
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.border.Border;
import javax.swing.border.TitledBorder;

/**
 * --------------------- @author nguyenvanquan7826 ---------------------
 * ------------------ website: cachhoc.net -------------------
 */
public class DemoTitleBorder extends JFrame {

  /**
   * create border empty for JPanel contain content
   */
  private Border panelBorder = BorderFactory
      .createEmptyBorder(20, 20, 20, 20);

  public DemoTitleBorder() {
    add(createContent());
    setDisplay();
  }

  private void setDisplay() {
    setTitle("Demo Title Border");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * create JPanel contain six JPanel with borders
   */
  private JPanel createContent() {
    JPanel panel = new JPanel(new GridLayout(6, 1, 10, 10));
    panel.setBorder(panelBorder);

    Border titleBorder;
    Border blueBorder = BorderFactory.createLineBorder(Color.BLUE);

    titleBorder = BorderFactory.createTitledBorder("title");
    panel.add(createPaneBorder(titleBorder, "default title border"));

    titleBorder = BorderFactory.createTitledBorder(blueBorder, "title");
    panel.add(createPaneBorder(titleBorder, "title border with color blue"));

    titleBorder = BorderFactory.createTitledBorder(blueBorder, "title",
        TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);
    panel.add(createPaneBorder(titleBorder,
        "title border center just and default prsition"));

    titleBorder = BorderFactory.createTitledBorder(null, "title",
        TitledBorder.CENTER, TitledBorder.ABOVE_TOP);
    panel.add(createPaneBorder(titleBorder,
        "title border center just and above top"));

    titleBorder = BorderFactory.createTitledBorder(blueBorder, "title",
        TitledBorder.RIGHT, TitledBorder.BELOW_TOP);
    panel.add(createPaneBorder(titleBorder,
        "title border right just and below top"));

    titleBorder = BorderFactory.createTitledBorder(null, "title",
        TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.BELOW_BOTTOM);
    panel.add(createPaneBorder(titleBorder,
        "title border default just and below bottom"));

    return panel;
  }

  /**
   * create JPanel contain a border
   */
  private JPanel createPaneBorder(Border border, String content) {
    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(border);
    JLabel lb = new JLabel(content, JLabel.CENTER);
    panel.add(lb);
    return panel;
  }

  public static void main(String[] args) {
    new DemoTitleBorder();
  }
}

使用复合边框

复合边界是边界的结合在了一起. 我们可以使用它作为您要创建优美的边界,也可以通过空边框边框到另一个边境被边防化合物的组合创建内容填充或保证金.

在java中的标题边界

package nguyenvanquan7826.border;

import java.awt.BorderLayout;
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.border.Border;
import javax.swing.border.TitledBorder;

/**
 * --------------------- @author nguyenvanquan7826 ---------------------
 * ------------------ website: cachhoc.net -------------------
 */
public class DemoCompoundBorder extends JFrame {

	/** create border type */

	private Border lineBorder = BorderFactory.createLineBorder(Color.blue);
	private Border raisedBevel = BorderFactory.createRaisedBevelBorder();
	private Border loweredBevel = BorderFactory.createLoweredBevelBorder();

	/**
	 * create border empty for JPanel contain content
	 */
	private Border panelBorder = BorderFactory
			.createEmptyBorder(20, 20, 20, 20);

	public DemoCompoundBorder() {
		add(createContent());
		setDisplay();
	}

	private void setDisplay() {
		setTitle("Demo Compound Border");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * create JPanel contain six JPanel with borders
	 */
	private JPanel createContent() {
		JPanel panel = new JPanel(new GridLayout(3, 1, 10, 10));
		panel.setBorder(panelBorder);

		Border compound = BorderFactory.createCompoundBorder(raisedBevel,
				loweredBevel);
		panel.add(createPaneBorder(compound, "raisedBevel and loweredBevel"));

		compound = BorderFactory.createCompoundBorder(lineBorder, compound);
		panel.add(createPaneBorder(compound, "add lineBorder to compound"));

		Border titleBorder = BorderFactory.createTitledBorder(compound,
				"title", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);
		panel.add(createPaneBorder(titleBorder, "title border with compound"));

		return panel;
	}

	/**
	 * create JPanel contain a border
	 */
	private JPanel createPaneBorder(Border border, String content) {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(border);
		JLabel lb = new JLabel(content, JLabel.CENTER);
		panel.add(lb);
		return panel;
	}

	public static void main(String[] args) {
		new DemoCompoundBorder();
	}
}

在系列的帖子 的Java Swing指南.

参考和进一步阅读: 使用边界 BorderFactory