【问题标题】:Unwanted Images Appearing on Java ButtonJava 按钮上出现不需要的图像
【发布时间】:2021-05-19 17:25:27
【问题描述】:

我正在尝试在 Java 中使用多边形按钮。我为此目的找到了这段代码:

package testklassen;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PolygonButton extends JButton {

    private Polygon shape;

    public PolygonButton(int[] x, int[] y) {
        this.shape = new Polygon();
        
        this.initialize(x, y);
    }

    protected void initialize(int[] x, int[] y) {
        Point p1, p2, p3, p4, p5;

        this.setSize(90, 120);

        p1 = new Point(x[0], y[0]);
        p2 = new Point(x[1], y[1]);
        p3 = new Point(x[2], y[2]);
        p4 = new Point(x[3], y[3]);
        p5 = new Point(x[4], y[4]);

        this.shape.addPoint((int) Math.round(p1.getX()),
                (int) Math.round(p1.getY()));
        this.shape.addPoint((int) Math.round(p2.getX()),
                (int) Math.round(p2.getY()));
        this.shape.addPoint((int) Math.round(p3.getX()),
                (int) Math.round(p3.getY()));
        this.shape.addPoint((int) Math.round(p4.getX()),
                (int) Math.round(p4.getY()));
        this.shape.addPoint((int) Math.round(p5.getX()),
                (int) Math.round(p5.getY()));

        this.setMinimumSize(this.getSize());
        this.setMaximumSize(this.getSize());
        this.setPreferredSize(this.getSize());
    }

    // Hit detection
    public boolean contains(int x, int y) {
        return this.shape.contains(x, y);
    }

    // Draw Button
    protected void paintComponent(Graphics g) {
        Graphics2D gCopy = (Graphics2D) g.create();
        gCopy.fillPolygon(this.shape);

    }

}

为了测试这一点,我构建了一个简单的框架,其中包含一个 PolygonButton 和一个普通按钮:

package testklassen;

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

public class FrameForPolygonButton extends JFrame {
  
    
    int[] x = {0, 50, 100, 100, 0};
    int[] y = {0, 50, 0, 100, 100};
  PolygonButton polygonButton = new PolygonButton(x, y);
  JButton button = new JButton();
  
  public FrameForPolygonButton() { 
    
    super();
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 530; 
    int frameHeight = 400;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);

    
    setVisible(true);

    button.setBounds(200, 200, 50, 50);
    cp.add(button);
    
    //polygonButton.setBorder(null);
    
    cp.add(polygonButton);
    
  } 
  
  public static void main(String[] args) {
    new FrameForPolygonButton();    
  }
} 

That's how it looks like when starting. And that is exactly what I want.

但是当我将鼠标悬停在另一个 JButton 上,然后将鼠标悬停在 PolygonButton 上时

a picture of the JButton appears on the PolygonButton

有谁知道,我该如何避免这种情况?

【问题讨论】:

    标签: image button graphics hover


    【解决方案1】:

    我找到了解决这个问题的方法。但我仍然不知道,为什么会发生这种情况。如果有人知道,我会很感兴趣。

    对我有用的解决方案是添加以下行:

    polygonButton.setOpaque(false);
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多