【问题标题】:How can I create a JButton with border shadow in Swing如何在 Swing 中创建带有边框阴影的 JButton
【发布时间】:2015-08-22 10:31:27
【问题描述】:

如何在 Swing 中创建一个像这样带有内阴影的JButton

我想用不同的Color 边框创建JButton,比如上下边框颜色应该是黑色,右边框颜色应该是白色。

但总的来说,我想要像上图一样在顶部和左侧有深灰色的内阴影。

【问题讨论】:

  • 这是一个“笑话问题”还是什么?因为link.
  • 我已经删除了我的答案,因为您似乎自己回答了这个问题......
  • @LuxxMiner:请重新考虑;另请参阅相关的example
  • @trashgod 我不太明白为什么。他的回答基本上比我的好,因为给定的示例恰好产生了图片中显示的 JButton。我的回答是为了给他一个“起点”——他可以在这个起点上进一步自定义按钮。但是,如果您认为我的回答对“一般兴趣”有用(当其他人点击此问题并想知道答案时),当然可以。我会取消删除它。
  • @LuxxMiner:在装饰 Swing 按钮的众多方法中,您的答案包括直接解决问题的complete example,而他的链接是无用的。他的回答总体上在改进,我希望鼓励你们俩。

标签: java swing jbutton


【解决方案1】:

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

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_);
    }

}

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2012-03-26
    • 2021-10-06
    相关资源
    最近更新 更多