swing 颜色、图片边框(MatteBorder)

快速开发简单MatteBorder

目录:

java代码如下:

package cn.cun.border;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

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;

public class MatteBorder extends JFrame {
	public MatteBorder() {
		initUI();
	}

	private void initUI() {
		Border emptyPanl, iconBorder, colorBorder;
		emptyPanl = BorderFactory.createEmptyBorder(10, 10, 10, 10);
		//带颜色边框
		colorBorder = BorderFactory.
				createMatteBorder(5, 5, 1, 1, Color.BLUE);
		
		//带图片边框
		iconBorder = BorderFactory.
				createMatteBorder(-1, -1, -1, -1,
				new ImageIcon("F:\\aaaa.jpg"));
		
		JPanel matteBorder = new JPanel();
		matteBorder.setBorder(emptyPanl);
		matteBorder.setLayout(
                                new BoxLayout(matteBorder, 
				BoxLayout.Y_AXIS));
		
		addCompForBorder(colorBorder,"color Border",matteBorder);
		addCompForBorder(iconBorder,"icon Border",matteBorder);
		
		JTabbedPane jTabbedPane = new JTabbedPane();
		jTabbedPane.setToolTipText("simple");
		jTabbedPane.addTab("matte", matteBorder);
		this.add(jTabbedPane);
		//this.getContentPane().add(jTabbedPane);
	}
	
	private void addCompForBorder(Border border,String lable,Container container) {
		JPanel comp = new JPanel(false);
	    JLabel label = new JLabel(lable, 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 MatteBorder();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(400,400));
		frame.pack();
		frame.setVisible(true);
	}
}
主要方法:

    BorderFactory.createMatteBorder(top, left, bottom, right, color)
    BorderFactory.createMatteBorder(top, left, bottom, right, tileIcon)

效果图:

                      ||==========》  《===疑问===》《=========||

阅读更多
文章标签: Swing
个人分类: Swing
想对作者说点什么? 我来说一句

[JAVA] Swing 边框用法

Border 用法

hwj528 hwj528

2016-12-14 17:16:59

阅读数:252

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭