透明JButton

发布于 2024-10-10 00:16:26 字数 322 浏览 0 评论 0原文

是否可以使 JButton 透明(包括边框)但不透明文本?我扩展了 swing 的 JButton 并覆盖了它:

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
    super.paint(g2);
    g2.dispose();
}

但它使所有内容都透明,包括文本。谢谢。

Is it possible to make a JButton transparent (including the border) but not the text? I extend swing's JButton and override this:

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
    super.paint(g2);
    g2.dispose();
}

but it makes everything transparent, including the text. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅刃 2024-10-17 00:16:26
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
怼怹恏 2024-10-17 00:16:26

以下应该可以解决问题。

public class PlainJButton extends JButton {

    public PlainJButton (String text){
        super(text);
        setBorder(null);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setOpaque(false);
    }

    // sample test method
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(new PlainJButton("HI!!!!"));
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
    }
}

The following should do the trick.

public class PlainJButton extends JButton {

    public PlainJButton (String text){
        super(text);
        setBorder(null);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setOpaque(false);
    }

    // sample test method
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(new PlainJButton("HI!!!!"));
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
    }
}
梦开始←不甜 2024-10-17 00:16:26

您的程序的用户不能将您的按钮视为按钮。使用时可以认为它是一个Label。所以我的解决方案是半透明的,出现 &消失的边界。

此代码改进,改进的代码,更好,性能更好,更美观,更适合。

import java.awt.Color;
import static java.awt.Color.blue;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Test extends JFrame {

    Test() {
        setLayout(null);

        JButton testButton = new JButton("Test_Button");
        testButton.setBounds(200, 200, 200, 20);
        testButton.setOpaque(false); // Must add
        testButton.setContentAreaFilled(false); // No fill
        testButton.setFocusable(false); // I'd like to set focusable false to the button.
        testButton.setBorderPainted(true); // I'd like to enable it.
        testButton.setBorder(null); // Border (No border for now)
        add(testButton);
        
        testButton.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e){
                testButton.setBorder(BorderFactory.createLineBorder(blue, 2,true));
                //When enter we can not know our mouse successfully entered to the button. So I'd like to add Border
            }
            @Override
            public void mouseExited(MouseEvent e){
                testButton.setBorder(null);
                //When mouse exits no border.
            }
        });

        // For a half of transparent I'd like a add JPanel behind JButton
        JPanel backPanel = new JPanel();
        backPanel.setBounds(testButton.getBounds()); // Same to buttons bounds.
        backPanel.setBackground(new Color(0, 0, 0, 50)); // Background with transeparent
        add(backPanel);

        JLabel icon = new JLabel();
        icon.setBounds(0, 0, 3840, 2160);
        icon.setIcon(new ImageIcon(getClass().getResource("wallpaper_16105238065ffea49e5ec9d2.04136813.jpeg")));
        add(icon);
        //Background image for test up it.

        testButton.addActionListener((ActionEvent e) -> {
            testButton.setBorder(null); // When mouse clicked border will dissaprear.
            System.out.println("Button clicked!");
            //For check the button works correctly.
        });

        setSize(800, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }
}

我的输出

The user of you program can not citified your Button as a button. Use can think it is a Label. So my Solution is half transparent with appearing & disappearing border.

This code is improved, improved codes, better, better performance, more beautiful, pro-fit.

import java.awt.Color;
import static java.awt.Color.blue;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Test extends JFrame {

    Test() {
        setLayout(null);

        JButton testButton = new JButton("Test_Button");
        testButton.setBounds(200, 200, 200, 20);
        testButton.setOpaque(false); // Must add
        testButton.setContentAreaFilled(false); // No fill
        testButton.setFocusable(false); // I'd like to set focusable false to the button.
        testButton.setBorderPainted(true); // I'd like to enable it.
        testButton.setBorder(null); // Border (No border for now)
        add(testButton);
        
        testButton.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e){
                testButton.setBorder(BorderFactory.createLineBorder(blue, 2,true));
                //When enter we can not know our mouse successfully entered to the button. So I'd like to add Border
            }
            @Override
            public void mouseExited(MouseEvent e){
                testButton.setBorder(null);
                //When mouse exits no border.
            }
        });

        // For a half of transparent I'd like a add JPanel behind JButton
        JPanel backPanel = new JPanel();
        backPanel.setBounds(testButton.getBounds()); // Same to buttons bounds.
        backPanel.setBackground(new Color(0, 0, 0, 50)); // Background with transeparent
        add(backPanel);

        JLabel icon = new JLabel();
        icon.setBounds(0, 0, 3840, 2160);
        icon.setIcon(new ImageIcon(getClass().getResource("wallpaper_16105238065ffea49e5ec9d2.04136813.jpeg")));
        add(icon);
        //Background image for test up it.

        testButton.addActionListener((ActionEvent e) -> {
            testButton.setBorder(null); // When mouse clicked border will dissaprear.
            System.out.println("Button clicked!");
            //For check the button works correctly.
        });

        setSize(800, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }
}

My output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文