向JFrame添加自定义JButton会更改背景颜色

问题描述 投票:0回答:1

我创建了一个简单的GUI,因为我想创建一个圆形的JButton而不是普通按钮。因此,我创建了一个扩展JButton并覆盖一些方法来实现绘画的类。

但是,一旦我执行代码(使用Java 13),就会用难看的黄色创建框架,之后包括按钮,整个JFrame背景色都会改变。为什么会这样,我该如何预防呢?我只希望创建按钮,而其他都不能更改颜色或类似颜色。

public class RoundedButton extends JButton {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setSize(800, 800);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setSize(new Dimension(200, 200));

    JButton button = new RoundedButton("Text", 40, 2F, Color.BLACK);
    button.setBackground(new Color(116, 10, 10));
    button.setForeground(Color.WHITE);
    button.setSize(new Dimension(200, 120));
    button.setPreferredSize(new Dimension(200, 120));
    button.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            button.setBackground(button.getBackground().brighter().brighter());
        }

        @Override
        public void mouseExited(MouseEvent e) {
            button.setBackground(button.getBackground().darker().darker());
        }
    });


    panel.add(button, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    frame.setBackground(Color.YELLOW);
    //frame.pack();
    frame.setVisible(true);
}




private int arcRadius;
private float borderSize;
private Color borderColor;

public RoundedButton(String label, int arcRadius, float borderSize, Color borderColor) {
    super(label);
    this.setContentAreaFilled(false);
    this.arcRadius = arcRadius;
    this.borderSize = borderSize;
    this.borderColor = borderColor;
}


@Override
public void paint(Graphics g) {
    if(g instanceof Graphics2D) {
        ((Graphics2D) g).setStroke(new BasicStroke(borderSize));
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

    //Draw background / fill button
    g.setColor(this.getBackground());
    g.fillRoundRect(getX(), getY(), this.getWidth(), (int)(this.getHeight()-borderSize), arcRadius, arcRadius);

    //Draw border
    g.setColor(borderColor);
    g.drawRoundRect(getX(), getY(), this.getWidth(), (int)(this.getHeight()-borderSize), arcRadius, arcRadius);

    //Draw font
    if (this.getFont() != null && this.getText() != null) {
        FontMetrics fm = getFontMetrics(getFont());
        g.setColor(this.getForeground());
        g.drawString(this.getText(), ((this.getWidth() / 2) - (fm.stringWidth(this.getText()) / 2)),
                    ((this.getHeight() / 2) + fm.getMaxDescent()));
    }
}

@Override
public boolean contains(int x, int y) {
    return new RoundRectangle2D.Double(getX(), getY(), this.getWidth(), (int)(this.getHeight()-borderSize), arcRadius, arcRadius).contains(x, y);
}

@Override
public void updateUI() {
    super.updateUI();
    this.setContentAreaFilled(false);
    this.setFocusPainted(false);
}

}

如果将其添加到框架中,则颜色的背景会改变。为什么会这样?

java jbutton
1个回答
0
投票

提供给组件的paint方法的图形对象应该已经转换为组件的位置。因此,所有绘画都应在(0,0)和(宽度,高度)之内完成。

我希望剪辑也可以设置为组件的边界,以防止您在其外部进行绘制。但是,也许您的按钮覆盖了框架的很大区域,或者由于某些原因,剪辑为空。

特别是:g.drawRoundRect(0,0,...

© www.soinside.com 2019 - 2024. All rights reserved.