grouppanel.java

来自「此文档针对开发人员和测试人员。第二章对软件进行了全面的描述。第三章对接口进行了分」· Java 代码 · 共 92 行

JAVA
92
字号
package com.ciash.common.gui;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.border.Border;
import java.awt.Component;
import java.awt.Insets;

/**
 * 该类简单封装了一个有边框绘制,并附加边框字的控件
 */

public class GroupPanel extends JPanel{

    private String groupName;

    public GroupPanel() {
        super();
        setBorder(new GroupBorder());
    }

    public GroupPanel(String tt){
        this();
        setGroupName(tt);
    }

    public void paint(Graphics g) {
        paintBack(g);
        paintChildren(g);
        paintBorder(g);
    }

    public void setGroupName(String tt) {
        this.groupName = tt;
    }

    public String getGroupName(){
        return this.groupName;
    }

    private void paintBack(Graphics g) {
        int width = getSize().width;
        int height = getSize().height;
        g.setColor(getBackground());
        g.fillRect(0, 0, width, height);
    }

    private class GroupBorder implements Border{

        public void paintBorder(Component component, Graphics g
            , int x, int y, int width, int height) {
            paintLine(g, width, height);
            paintString(g);
        }

        public Insets getBorderInsets(Component component) {
            if(groupName != null){
                return new Insets(10, 5, 5, 5);
            }
            else{
                return new Insets(5, 5, 5, 5);
            }

        }

        private void paintLine(Graphics g, int width, int height) {
            int d = 10;
            g.setColor(Color.DARK_GRAY);
            g.drawRect(d, d, width - d * 2, height - d * 2);
            d += 1;
            g.setColor(Color.WHITE);
            g.drawRect(d, d, width - d * 2, height - d * 2);
        }

        private void paintString(Graphics g) {
            if (groupName != null) {
                g.setColor(getBackground());
                g.fillRect(28, 5, 7 * groupName.getBytes().length, 18);
                g.setColor(Color.BLACK);
                g.drawString(groupName, 30, 15);
            }
        }

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

⌨️ 快捷键说明

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