【问题标题】:Transparent JButton透明 JButton
【发布时间】:2011-06-02 22:05:30
【问题描述】:

是否可以使 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();
}

但它使一切变得透明,包括文本。谢谢。

【问题讨论】:

  • 所以你基本上想要一个没有文字的JButton
  • 我想他想要只有文本的 JButton。
  • 自定义绘画(需要时)是通过覆盖paintComponent()方法而不是paint()方法来完成的。
  • @camickr 它来自some other site

标签: java swing jbutton


【解决方案1】:
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

【讨论】:

  • 糟糕,我的答案和你的一样。
  • setOpaque 救了我的命 :)
  • button.setBorderPainted(false); 如果我们想在按钮周围有一条线,可以省略
  • "setOpaque救了我的命" 其实@Birdy,这是不必要的(至少在我刚才在Windows上测试的五个PLAF中)。 setContentAreaFilled 使按钮BG 消失。this code 为例。很抱歉在这件事上加入(检查手表)有点晚,但它只是今天作为一个问题的重复引起了我的注意。
  • 就我而言,我还必须添加button.setFocusPainted(false) 才能完全删除按钮的边框:)
【解决方案2】:

以下应该可以解决问题。

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

【讨论】:

    【解决方案3】:

    您程序的用户不能将您的 Button 引用为按钮。使用可以认为它是一个标签。所以我的解决方案是半透明的,有出现和消失的边框。

    这个代码是改进的,改进的代码,更好,更好的性能,更漂亮,适合。

    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

    【讨论】:

    • JPanel 可以使用半透明
    猜你喜欢
    • 2021-11-01
    • 2014-10-10
    • 1970-01-01
    • 2020-07-07
    • 2014-01-28
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多