imagetitlepanel.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 163 行

SVN-BASE
163
字号
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.spark.component;

import org.jivesoftware.resource.Default;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.geom.AffineTransform;

/**
 * Fancy title panel that displays gradient colors, text and components.
 */
public class ImageTitlePanel extends JPanel {
    private Image backgroundImage;
    private final JLabel titleLabel = new JLabel();
    private final JLabel iconLabel = new JLabel();
    private final GridBagLayout gridBagLayout = new GridBagLayout();
    private final WrappedLabel descriptionLabel = new WrappedLabel();

    /**
     * Creates a new ImageTitlePanel.
     *
     * @param title the title to use for this label.
     */
    public ImageTitlePanel(String title) {
        backgroundImage = Default.getImageIcon(Default.TOP_BOTTOM_BACKGROUND_IMAGE).getImage();

        init();

        titleLabel.setText(title);

        titleLabel.setFont(new Font("Dialog", Font.BOLD, 11));
    }

    /**
     * Creates a new ImageTitlePanel object.
     */
    public ImageTitlePanel() {
        backgroundImage = Default.getImageIcon(Default.TOP_BOTTOM_BACKGROUND_IMAGE).getImage();

        init();

     
        titleLabel.setFont(new Font("Dialog", Font.BOLD, 11));
    }

    public void paintComponent(Graphics g) {
        double scaleX = getWidth() / (double)backgroundImage.getWidth(null);
        double scaleY = getHeight() / (double)backgroundImage.getHeight(null);
        AffineTransform xform = AffineTransform.getScaleInstance(scaleX, scaleY);
        ((Graphics2D)g).drawImage(backgroundImage, xform, this);
    }

    private void init() {
        setLayout(gridBagLayout);
        add(titleLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    }

    /**
     * Set the description for the label.
     *
     * @param description the description for the label.
     */
    public void setDescription(String description) {
        descriptionLabel.setText(description);
        add(descriptionLabel, new GridBagConstraints(0, 1, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    }

    /**
     * Set the font of the description label.
     *
     * @param font the font to use in the description label.
     */
    public void setDescriptionFont(Font font) {
        descriptionLabel.setFont(font);
    }

    /**
     * Returns the description label.
     *
     * @return the description label.
     */
    public JTextArea getDescriptionLabel() {
        return descriptionLabel;
    }

    /**
     * Sets the title to use in the label.
     *
     * @param title the title to use.
     */
    public void setTitle(String title) {
        titleLabel.setText(title);
    }

    /**
     * Returns the title label.
     *
     * @return the title label.
     */
    public JLabel getTitleLabel() {
        return titleLabel;
    }

    /**
     * Set the font of the title label.
     *
     * @param font the font to use for title label.
     */
    public void setTitleFont(Font font) {
        titleLabel.setFont(font);
    }

    /**
     * Specify a component to use on this label.
     *
     * @param component the component to use with this label.
     */
    public void setComponent(JComponent component) {
        add(new JLabel(),
                new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        add(component,
                new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    }

    /**
     * Specify the icon to use with this label.
     *
     * @param icon the icon to use with this label.
     */
    public void setIcon(ImageIcon icon) {
        add(new JLabel(),
                new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        iconLabel.setIcon(icon);
        add(iconLabel,
                new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?