출처

- Border 에 타이틀을 넣어준다.

<실행화면>


GradientTitleBorder.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swing_sample.customborder;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.UIManager;
import javax.swing.border.Border;

/**
 *
 * @author Administrator
 */
public class GradientTitleBorder implements Border {

    private String title;

    private int titleHeight;

    private Insets insets = new Insets(titleHeight, 0, 0, 0);

    private Color primaryColor;

    private Color secondaryColor;

    private Color fontColor;

    private Color shadowColor;

    private int indent = 5;

    private Font titleFont;

    /**
    * Constructtor that assumes a title height.
    *
    * @param title - string to display
    * @param primaryColor - first color of gradient
    * @param secondaryColor - second color of gradient (lower)
    * @param fontColor - color for the font
    */
    public GradientTitleBorder(String title, Color primaryColor, Color secondaryColor, Color fontColor) {
        this(title, primaryColor, secondaryColor, fontColor, 30);
    }

    /**
     * Full option constructor
     *
     * @param title - string to display
     * @param primaryColor - first color of gradient
     * @param secondaryColor - second color of gradient (lower)
     * @param fontColor - color for the font
     * @param titleHeight - height of the title bar
     */
    public GradientTitleBorder(String title, Color primaryColor, Color secondaryColor, Color fontColor, int titleHeight) {
        this.title = title;
        this.titleHeight = titleHeight;
        this.insets = new Insets(titleHeight, 2, 2, 2);
        this.primaryColor = primaryColor;
        this.shadowColor = primaryColor.darker();
        this.secondaryColor = secondaryColor;
        this.fontColor = fontColor;
        this.titleFont = UIManager.getFont("TitledBorder.font").deriveFont(Font.BOLD);
    }

    /**
     * Creates a GradientTitleBorder with default values.
     * @param title
     */
    public GradientTitleBorder(String title) {
        this(title, Color.BLACK, Color.GRAY, Color.WHITE, 30);
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

        GradientPaint gp = new GradientPaint(x, y, primaryColor, x, y + titleHeight, secondaryColor);
        g2d.setPaint(gp);
        g2d.fillRect(x, y, width, titleHeight);

        g2d.setColor(shadowColor);
        g2d.drawRect(x, y - 1, width - 1, titleHeight);

        g2d.setFont(titleFont);

        g2d.setColor(shadowColor);
        int titleOffset = (titleHeight / 2) + (c.getFont().getSize() / 2) - 1;

        g2d.drawString(title, x + insets.left + indent + 1, y + titleOffset + 1);

        g2d.setColor(fontColor);
        g2d.drawString(title, x + insets.left + indent, y + titleOffset);

        g2d.setColor(shadowColor);
        g2d.drawRect(x, y - 1, width - 1, height);
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return insets;
    }

    @Override
    public boolean isBorderOpaque() {
        return false;
    }

}

CustomBorderTest.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomBorderTest.java
 *
 * Created on 2009. 12. 23, 오전 10:44:25
 */

package swing_sample.customborder;

import java.awt.Color;
import javax.swing.UIManager;

/**
 *
 * @author Administrator
 */
public class CustomBorderTest extends javax.swing.JFrame {

    /** Creates new form CustomBorderTest */
    public CustomBorderTest() {

        changeSystemLookAndFeel();
      
        initComponents();

        customInit();
    }

    public void changeSystemLookAndFeel() {
        try {
            UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
        } catch (Exception e) {
            System.err.println("Couldn't use system look and feel.");
        }
    }
    public void customInit() {
        mainPanel.setBorder(new GradientTitleBorder("Title of main panel border", new Color(0x418EDC), new Color(0x6B91B8), Color.WHITE, 18));
        scrollPane.setBorder(new GradientTitleBorder("Title of tree scroll pane border"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        scrollPane = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        scrollPane.setViewportView(tree);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustomBorderTest().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel mainPanel;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTree tree;
    // End of variables declaration

}


Posted by 짱가쟁이