我没有任何代码可以显示我将JLabel添加到JPanel的问题,并且在鼠标移动时将文本设置为鼠标的当前x / y位置。
如果有人能解释为什么会这样,我会很感激。
PComponent.java
public class PComponent extends JPanel implements Commons {
private static final long serialVersionUID = -5525789712337277886L;
public PComponent(int x, int y, int width, int height) {
this.setLocation(x, y);
this.setSize(width, height);
}
}
PNode
public class PNode extends PComponent {
public PNode(int x, int y, int width, int height) {
super(x, y, width, height);
this.setOpaque(false);
this.setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
PInterface
public abstract class PInterface extends PComponent implements Resizable {
public PInterface(int x, int y, int width, int height, int nodeW, int nodeH) {
super(x, y, width, height);
this.nodeW = nodeW;
this.nodeH = nodeH;
this.node = new PNode[MAX_NODES];
this.addNodes();
this.registerInterface();
this.pack();
}
private void registerInterface() {
for (int i = 0; i < cachedInterface.length; i++) {
if(cachedInterface[i] == null) {
cachedInterface[i] = this;
break;
}
}
}
private void addNodes() {
int index = 0;
for (int i = 0; i < this.getWidth(); i+=nodeW) {
for (int j = 0; j < this.getHeight(); j+=nodeH) {
PNode node = new PNode(i,j,nodeW,nodeH);
this.add(node);
this.node[index] = node;
index++;
}
}
}
public void writeToNode(String s, int index) {
if(node[index] != null) {
if(node[index].getComponent(0) instanceof JLabel) {
((JLabel) node[index].getComponent(0)).setText(s);
}
}
}
public void writeToNode(String s, int index, int child) {
if(node[index] != null) {
if(node[index].getComponent(child) instanceof JLabel) {
((JLabel) node[index].getComponent(child)).setText(s);
}
}
}
@Override
public void resize() {
}
protected abstract void pack();
protected void createTransparentInterface(int r, int g, int b, int o) {
this.setBackground(new Color(r, g, b, o));
}
public static PInterface[] getCachedInterfaces() {
return cachedInterface;
}
public int getNodeW() {
return nodeW;
}
public void setNodeW(int nodeW) {
this.nodeW = nodeW;
}
public int getNodeH() {
return nodeH;
}
public void setNodeH(int nodeH) {
this.nodeH = nodeH;
}
private static PInterface[] cachedInterface = new PInterface[MAX_INTERFACES];
protected int nodeW;
protected int nodeH;
protected PNode[] node;
}
ClientDetailsInterface
public class ClientDetailsInterface extends PInterface {
public ClientDetailsInterface() {
super(0, 0, 200, 100, 100, 50);
this.createTransparentInterface(255, 0, 0, 100);
}
@Override
protected void pack() {
this.node[0].add(new JLabel("X:"));
this.node[0].add(new JLabel("Y:"));
}
}
帆布
公共类Canvas扩展JPanel实现Resizable,Commons,MouseMotionListener {
private static final long serialVersionUID = 8374256772293662258L;
public Canvas() {
this.setBackground(Color.WHITE);
this.setFocusable(true);
this.addMouseMotionListener(this);
this.registerInterfaces();
}
private void registerInterfaces() {
int count = 0;
new ClientDetailsInterface();
for (int i = 0; i < PInterface.getCachedInterfaces().length; i++) {
if (PInterface.getCachedInterfaces()[i] != null) {
this.add(PInterface.getCachedInterfaces()[i]);
count++;
}
}
System.out.println("Loaded: " + count + " interfaces!");
}
@Override
public void resize() {
// TODO add resizing code
}
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent mouse) {
PInterface.getCachedInterfaces()[0].writeToNode("X:" + mouse.getX(), 0,0);
PInterface.getCachedInterfaces()[0].writeToNode("Y:" + mouse.getY(), 0,1);
}
}
答案 0 :(得分:3)
听起来你正在某处做自定义绘画而你忘了调用:
super.paintComponent(g);
确保在进行自定义绘画之前清除背景。
这当然假设您重写了paintComponent()
方法。如果你压倒油漆(...),不要。通过覆盖paintComponent(...)方法来完成自定义绘制。
如果您需要更多帮助,请发布SSCCE,因为您的代码存在问题我们无法通过图片告诉您代码的样子。
编辑:
请勿覆盖getX()
和getY()
。这些是用于在特定位置绘制组件的组件类的方法。
最终编辑:
请查看问题和解决方案Backgrounds With Transparency。