如何在Swing中创建带有边框阴影的JButton [英] How can I create a JButton with border shadow in Swing

查看:236
本文介绍了如何在Swing中创建带有边框阴影的JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swing中使用内部阴影创建像这样的JButton?

How can I create a JButton like this with inner shadow in Swing?

我希望创建具有不同边框ColorJButton,例如顶部和左侧边框颜色应为黑色,右侧和底部边框颜色应为白色.

I wan to create JButton with different Color of border, like top and left border color should be black and right and bottom border color should be of white color.

但是总的来说,我希望像上面的图像一样,在顶部和左侧形成深灰色的内部阴影.

But all together, I want inner shadow of dark gray color in top and left side like above image.

推荐答案

首先,我认为您可以使用简单的BevelBorder来实现此目的,但是不幸的是,您不能舒适地设置边框的厚度...所以我不得不基本上制作一个定制的Border.如果您不喜欢paintBorder方法中我的按钮的样式,则可以对其进行更多自定义,但是您必须知道如何使用Graphics.这是我所拥有的:

First I thought you can just achieve this with a simple BevelBorder, but unfortunately you can't set the border's thickness comfortably... So I had to basically make a customized Border. You can customize it more if you don't like the style of my button in the paintBorder method, but you have to know how to work with Graphics. Here is what I've got:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;

/**
 *
 */
public class MyBorder implements Border {

    private int thickness_ = 4;
    private Color white = Color.WHITE;
    private Color gray = Color.GRAY;
    private Color black = Color.BLACK;

    public static void main(String[] args) {

        JFrame frm = new JFrame("Border Test");
        frm.setLayout(new FlowLayout());
        JButton btn = new JButton("Button");

        MyBorder border = new MyBorder();

        btn.setBorder(border);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(60,30));
        btn.setBackground(Color.LIGHT_GRAY);

        frm.add(btn);
        frm.setSize(200,200);
        frm.setVisible(true);

    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width,
            int height) {
        Color oldColor = g.getColor();
        int i;

        for (i = 0; i < thickness_; i++) {
            g.setColor(white);
            g.drawRect(x + i, y + i, width - i - i - 1, height - i - i - 1); //White Rectangle
        }
        for (i = 0; i < thickness_/2; i++) {
            g.setColor(black);
            g.drawLine(x + i, y + i, (width - x) - (i * 2), y + i); //Top Outer Edge
            g.drawLine(x + i, y + i, x + i, (height - y) - (i * 2));  //Left Outer Edge
        }
        for (i = thickness_/2; i < thickness_; i++) {
            g.setColor(gray);
            g.drawLine(x + i, y + i, (width - x) - (i * 2), y + i); //Top Inner Edge
            g.drawLine(x + i, y + i, x + i, (height - y) - (i * 2)); //Left Inner Edge

        }
        g.setColor(oldColor);
    }

    public int getThickness() {
        return thickness_;
    }

    public void setThickness(int i) {
        thickness_ = i;
    }

    public boolean isBorderOpaque() {
        return true;
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(thickness_, thickness_, thickness_, thickness_);
    }

}

这篇关于如何在Swing中创建带有边框阴影的JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆