jlabel 不能连续两次set_Java JLabel.setFocusable方法代码示例

这篇博客探讨了在Java Swing中,JLabel组件不能连续两次调用setFocusable方法的问题。通过代码示例展示了如何创建一个不可聚焦的JLabel,并在鼠标点击时触发特定操作,例如改变游戏单位的方向。同时,还提到了MiniMapPanel的布局和位置设置,以及在资源存在时如何加载和使用MiniMap皮肤。
摘要由CSDN通过智能技术生成

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

/**

* The basic constructor.

*

* @param freeColClient The {@code FreeColClient} for the game.

*/

public CornerMapControls(final FreeColClient freeColClient) {

super(freeColClient, true);

compassRose = new JLabel(new ImageIcon(ResourceManager.getImage("image.skin.compass")));

compassRose.setFocusable(false);

compassRose.setSize(compassRose.getPreferredSize());

compassRose.addMouseListener(new MouseAdapter() {

/**

* {@inheritDoc}

*/

@Override

public void mouseClicked(MouseEvent e) {

Unit unit = getGUI().getActiveUnit();

if (unit == null) return;

int x = e.getX() - compassRose.getWidth()/2;

int y = e.getY() - compassRose.getHeight()/2;

double theta = Math.atan2(y, x) + Math.PI/2 + Math.PI/8;

if (theta < 0) {

theta += 2*Math.PI;

}

igc().moveUnit(unit, Direction.angleToDirection(theta));

}

});

miniMapPanel = new MiniMapPanel();

miniMapPanel.setFocusable(false);

/**

* In order to make the setLocation setup work, we need to set

* the layout to null first, then set the size of the minimap,

* and then its location.

*/

miniMapPanel.setLayout(null);

miniMap.setSize(MAP_WIDTH, MAP_HEIGHT);

// Add buttons:

miniMapPanel.add(miniMapToggleBorders);

miniMapPanel.add(miniMapToggleFogOfWarButton);

miniMapPanel.add(miniMapZoomInButton);

miniMapPanel.add(miniMapZoomOutButton);

miniMapPanel.add(miniMap);

String miniMapSkinKey = "image.skin.MiniMap";

if (ResourceManager.hasImageResource(miniMapSkinKey)) {

miniMapSkin = ResourceManager.getImage(miniMapSkinKey);

miniMapPanel.setBorder(null);

miniMapPanel.setSize(miniMapSkin.getWidth(null),

miniMapSkin.getHeight(null));

miniMapPanel.setOpaque(false);

// FIXME: LATER: The values below should be specified by a

// skin-configuration-file.

miniMap.setLocation(38, 75);

miniMapToggleBorders.setLocation(4,114);

miniMapToggleFogOfWarButton.setLocation(4, 144);

miniMapZoomInButton.setLocation(4, 174);

miniMapZoomOutButton.setLocation(264, 174);

} else {

miniMapSkin = null;

int width = miniMapZoomOutButton.getWidth()

+ miniMapZoomInButton.getWidth() + 4 * GAP;

miniMapPanel.setOpaque(true);

miniMap.setBorder(new BevelBorder(BevelBorder.RAISED));

miniMap.setLocation(width/2, GAP);

miniMapZoomInButton.setLocation(GAP,

MAP_HEIGHT + GAP - miniMapZoomInButton.getHeight());

miniMapZoomOutButton.setLocation(

miniMapZoomInButton.getWidth() + MAP_WIDTH + 3 * GAP,

MAP_HEIGHT + GAP - miniMapZoomOutButton.getHeight());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值