- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我刚刚开始使用 Swing 并尝试绘制一个具有自定义形状的按钮,在本例中为三角形。由于其异常行为,我在以下代码中将 JButton 子类称为“ShiftingButton”。当鼠标进入其区域时,它会重新绘制,并从其原始位置偏移。此外,除了原始位置之外还绘制了偏移的偏移版本,以便原始版本和偏移版本一起出现。也就是说,当我运行这段代码时,按钮显示为窗口左边缘的三角形。然后,当我将鼠标移到按钮上时,会绘制一个新三角形(除了旧三角形之外),并向下和向右移动大约 10 个像素。调整窗口大小会更改虚拟按钮相对于原始按钮的偏移量。
用鼠标点击进行的试验表明只有原始的、正确定位的按钮处于 Activity 状态。偏移幻影按钮的区域未激活。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Polygon;
public class ShiftingButton extends JButton implements ActionListener {
private Polygon shape;
public ShiftingButton () {
initialize();
addActionListener(this);
}
protected void initialize() {
shape = new Polygon();
setSize(120, 120);
shape.addPoint(0, 0);
shape.addPoint(0, 60);
shape.addPoint(90, 0);
setMinimumSize(getSize());
setMaximumSize(getSize());
setPreferredSize(getSize());
}
// Hit detection
public boolean contains(int x, int y) {
return shape.contains(x, y);
}
@Override
public void paintComponent (Graphics g) {
System.err.println("paintComponent()");
g.fillPolygon(shape);
}
protected void paintBorder(Graphics g) {
}
@Override
public void actionPerformed (ActionEvent ev) {
System.out.println("ShiftingButton ActionEvent!");
}
public static void main (String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
ShiftingButton button = new ShiftingButton();
panel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
最佳答案
您未能在覆盖的 paintComponent(...)
方法中调用 super.paintComponent(g)
。此外,在覆盖 Base
类的方法时,始终尽量保持方法的访问说明符相同。在这种情况下,它是 protected
而不是 public
:-) 现在函数应该是这样的:
@Override
protected void paintComponent (Graphics g) {
System.err.println("paintComponent()");
super.paintComponent(g);
g.fillPolygon(shape);
}
此外,由于您正在使用要绘制的自定义形状,因此您再次未能为这个有问题的 JButton
指定 ContentAreaFilled
属性,因此在您的构造函数中,你应该写 setContentAreaFilled(false) ,让它工作得很好。尽管如果这不起作用(由于文档中指定的原因),那么您必须使用普通的旧 Opaque
属性并将其设置为 false
为此 JButton
使用 setOpaque(false)
:-)
这是修改后的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Polygon;
public class ShiftingButton extends JButton implements ActionListener {
private Polygon shape;
public ShiftingButton () {
setContentAreaFilled(false);
initialize();
addActionListener(this);
}
protected void initialize() {
shape = new Polygon();
setSize(120, 120);
shape.addPoint(0, 0);
shape.addPoint(0, 60);
shape.addPoint(90, 0);
}
@Override
public Dimension getPreferredSize() {
return (new Dimension(120, 120));
}
// Hit detection
public boolean contains(int x, int y) {
return shape.contains(x, y);
}
@Override
protected void paintComponent(Graphics g) {
System.err.println("paintComponent()");
super.paintComponent(g);
g.fillPolygon(shape);
}
protected void paintBorder(Graphics g) {
}
@Override
public void actionPerformed (ActionEvent ev) {
System.out.println("ShiftingButton ActionEvent!");
}
public static void main (String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
ShiftingButton button = new ShiftingButton();
panel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
关于java - JButton 子类在重绘期间具有自定义形状变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18300056/
我是一名优秀的程序员,十分优秀!