java jlabel如何继承,Java JLabel.getParent方法代码示例

这段代码展示了在Java Swing环境中实现数据导入和组件交互的逻辑。主要涉及JLabel对象的转换、数据格式检查、组件判断以及不同类型的标签(如GoodsLabel、MarketLabel、UnitLabel)的导入处理。在处理过程中,确保了数据不会被导入到自身,并考虑了单位与货物在港口的装载情况,以及单位装备的可能性。
摘要由CSDN通过智能技术生成

import javax.swing.JLabel; //导入方法依赖的package包/类

/**

* {@inheritDoc}

*/

@Override

public boolean importData(JComponent comp, Transferable t) {

if (!t.isDataFlavorSupported(DefaultTransferHandler.flavor)) {

return importFail(comp, "data flavor");

}

boolean ret;

// This variable is used to temporarily keep the old

// selected unit, while moving cargo from one carrier to another:

UnitLabel oldSelectedUnit = null;

try {

// Get the data to transfer.

JLabel data = (JLabel)t.getTransferData(DefaultTransferHandler.flavor);

// Do not allow a transferable to be dropped upon itself:

if (comp == data) return false;

// Make sure we don't drop onto other Labels.

if (comp instanceof AbstractGoodsLabel) {

comp = getDropTarget(comp);

} else if (comp instanceof UnitLabel) {

UnitLabel unitLabel = (UnitLabel)comp;

/**

* If the unit/cargo is dropped on a carrier in port

* then the ship is selected and the unit is added to

* its cargo. If the unit is not a carrier, but can

* be equipped, and the goods can be converted to

* equipment, equip the unit.

*

* If not, assume that the user wished to drop the

* unit/cargo on the panel below.

*/

if (unitLabel.getUnit().isCarrier()

&& unitLabel.getParent() instanceof InPortPanel

&& parentPanel instanceof PortPanel) {

PortPanel portPanel = (PortPanel) parentPanel;

if (data instanceof Draggable

&& ((Draggable)data).isOnCarrier()) {

oldSelectedUnit = portPanel.getSelectedUnitLabel();

}

portPanel.setSelectedUnitLabel(unitLabel);

comp = portPanel.getCargoPanel();

} else if (unitLabel.canUnitBeEquippedWith(data)) {

// don't do anything before partial amount has been checked

} else {

comp = getDropTarget(comp);

}

}

ret = (data.getParent() == comp)

? importFail(comp, "data-already-present")

: (data instanceof GoodsLabel)

? importGoods(comp, (GoodsLabel)data, oldSelectedUnit)

: (data instanceof MarketLabel)

? importMarket(comp, (MarketLabel)data)

: (data instanceof UnitLabel)

? importUnit(comp, (UnitLabel)data, oldSelectedUnit)

: importFail(comp, data.toString());

} catch (Exception e) { // FIXME: Suggest a reconnect?

logger.log(Level.WARNING, "Import fail", e);

ret = importFail(comp, "crash: " + e.toString());

}

return ret;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值