簡體   English   中英

鼠標懸停在java中刪除圓形JButton的透明背景

[英]mouseover removes transparent background of round JButton in java

一旦我將鼠標懸停在任何圓形按鈕上,它后面就會出現一個方形背景,並且再也不會消失。 當方形背景不可點擊時,該按鈕仍可點擊。 我沒有實現任何 MouseEvents 所以我不知道為什么會出現背景

https://i.stack.imgur.com/I1MIq.png左:點擊后,中心:鼠標懸停后,右:原始狀態

代碼示例

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GUI implements ActionListener {
    private JLabel textLabel;
    private JButton buttonL, buttonConfirm, buttonR;
    
    public GUI() {
        JPanel mainDisplay = new JPanel();
        mainDisplay.setBackground(new Color(172, 181, 176));
        mainDisplay.setPreferredSize(new Dimension(150, 160));
        mainDisplay.setLayout(new BorderLayout());
        textLabel = new JLabel("1");
        textLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainDisplay.add(textLabel, BorderLayout.CENTER);
        
        JPanel container = new JPanel();
        container.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.insets = new Insets(75, 0, 0, 0);
        container.add(mainDisplay, gc);
        
        Dimension buttonDimension = new Dimension(18, 18);
        Color buttonColor = new Color(242, 240, 241);
        buttonL = new RoundButton();
        buttonL.setBackground(buttonColor);
        buttonL.setPreferredSize(buttonDimension);
        buttonL.addActionListener(this);
        gc.gridwidth = GridBagConstraints.RELATIVE;
        gc.insets = new Insets(10, 20, 0, 0);
        container.add(buttonL, gc);
        
        buttonConfirm = new RoundButton();
        buttonConfirm.setBackground(buttonColor);
        buttonConfirm.setPreferredSize(buttonDimension);
        buttonConfirm.addActionListener(this);
        gc.insets = new Insets(40, -35, 0, 0);
        container.add(buttonConfirm, gc);
        
        buttonR = new RoundButton();
        buttonR.setBackground(buttonColor);
        buttonR.setPreferredSize(buttonDimension);
        buttonR.addActionListener(this);
        gc.insets = new Insets(10, -50, 0, 0);
        container.add(buttonR, gc);
        
        JFrame frame = new JFrame();
        frame.add(container);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == buttonL) {
            textLabel.setText("L");;
        } else if(e.getSource() == buttonConfirm) {
            textLabel.setText("");
        } else if(e.getSource() == buttonR) {
            textLabel.setText("R");
        }
    }
    
    public static void main(String[] args) {        
            new GUI();
    }
}

圓形 JButton

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;

public class RoundButton extends JButton {
    Shape shape;
    
    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(getBackground());
        g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
    }

    @Override
    protected void paintBorder(Graphics g) {
        g.setColor(Color.darkGray);
        g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
    }

    @Override
    public boolean contains(int x, int y) {
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
        }
        return shape.contains(x, y);
    }
}

進行自定義繪畫時,基本代碼應為:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    // add custom painting here
}

這將確保清除背景,因此沒有繪畫偽影。

但是,在您的情況下,您希望在繪制按鈕之前繪制父面板的背景,因此您需要使用:

button.setOpaque(false);

此外,當您單擊按鈕時,矩形仍會出現,表示按鈕處於按下狀態。 要刪除這幅畫,您需要使用:

button.setContentAreaFilled( false );

查看:為圓形按鈕的另一個實現更改 JButton 焦點區域

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM