关闭

swing 颜色、图片边框(MatteBorder)

标签: Swing
645人阅读 评论(0) 收藏 举报
分类:

快速开发简单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)

效果图:

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26788次
    • 积分:562
    • 等级:
    • 排名:千里之外
    • 原创:31篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类