简体   繁体   English

如何将一个JLabel的内容传输到另一个?

[英]How do I transfer the contents of one JLabel to another?

 public void movePiece(JLabel destination){
  JLabel currentPiece = piece[oldIndex[0]][oldIndex[1]];
  destination = currentPiece;
  currentPiece.setVisible(false);
  destination.repaint();
  currentPiece.repaint();
 }

Current method for moving. 当前的移动方法。 It takes the JLabel to which the text is to be "transferred", the JLabel get's a reference to the JLabel from which to take the text. 它需要将文本“传输到”的JLabel,JLabel获得对从中获取文本的JLabel的引用。 Anyone got any idea? 有人知道吗? The method doesn't work, just gives you an idea of how it's going to look. 该方法不起作用,只是让您对它的外观有所了解。

For example if this is the case: 例如,如果是这种情况:

JLabel 1: "Trololo" JLabel 2: "Hello!" JLabel 1:“ Trololo” JLabel 2:“ Hello!”

if destination is 2 and currentPiece is 1, I'd like it to look like this: 如果destination是2,而currentPiece是1,我希望它看起来像这样:

JLabel 1: "Trololo" .setVisibility(false) JLabel 2: "Trololo" JLabel 1:“ Trololo” .setVisibility(false)JLabel 2:“ Trololo”

Effectively making only nr. 有效地仅使nr。 2 visible with the contents of nr. 2可见带有nr的内容。 1. Don't want to remove nr. 1.不想删除nr。 1, just keep it invisible. 1,只是保持它不可见。

(they are not referring to the same object, they just have the same text and font) (它们不是指相同的对象,它们只是具有相同的文本和字体)

Call setText to change the contents of the destination: 调用setText更改目标的内容:

public void movePiece(JLabel destination){
  JLabel currentPiece = piece[oldIndex[0]][oldIndex[1]];
  destination.setText(currentPiece.getText());
  currentPiece.setVisible(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM