gpt4 book ai didi

java - 使用 NiftyGUI 的自定义 Controller

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:14 25 4
gpt4 key购买 nike

我有一个自定义按钮,单击时会切换图像,因此我需要处理 mouseEvent 单击。但是,它只对函数“inputEvent(NiftyInputEvent inputEvent)”上的 keyEvent 使用react...

我必须传递 NiftyMousePrimaryClickedEvent 但随后它不会实现 Controller 类...我这样做对吗?

这是我的自定义图像按钮:

   public class ImageButtonControl implements Controller{
private Nifty nifty;
private Screen screen;
private Element img1;
private Element img2;
private Properties properties;
private boolean isPressed = false;
private ImageRenderer render1;
private NiftyImage imageNifty1;
private NiftyImage imageNifty2;
private FocusHandler focusHandler;

@Override
public void bind(
final Nifty nifty,
final Screen screenParam,
final Element element,
final Properties parameter,
final Attributes controlDefinitionAttributes) {
this.nifty = nifty;
this.screen = screenParam;
this.properties = parameter;
img1 = element.findElementByName("image-1");
img2 = element.findElementByName("image-2");
initImages(this.nifty, this.screen, properties.getProperty("img-1"),properties.getProperty("img-2"));
//focusHandler = screenParam.getFocusHandler();
System.out.println("Initialisation completed.");}
@Override
public void init(Properties prprts, Attributes atrbts) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void onStartScreen() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void onFocus(final boolean getFocus) {
//super.onFocus(getFocus);
System.out.println("---Clicked event focus---");
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean inputEvent(NiftyInputEvent inputEvent) {
System.out.println("---Clicked event focus---");
return false;
}


public void switchImage(){
//ImageRenderer imageRenderer = img1.getRenderer(ImageRenderer.class);
if(isPressed){
isPressed = false;
//img1.show();
//img2.hide();
System.out.println("Clicked event");
}else{
isPressed = true;
//img1.hide();
//img2.show();
System.out.println("Clicked event");
}
}

private void initImages(Nifty nifty, Screen screen, String imgPath1, String imgPath2) {
NiftyRenderEngine renderEngine = nifty.getRenderEngine();
imageNifty1 = renderEngine.createImage(screen,imgPath1,false);
imageNifty2 = renderEngine.createImage(screen,imgPath2,false);
}}

这是我的 XML:

  <controlDefinition name="imagebutton" style="nifty-panel-style" controller="de.lessvoid.nifty.customs.imagebutton.ImageButtonControl">
<panel style="#panel" childLayout="overlay" focusable="true" visibleToMouse="true" width="$width" heigth="$height">
<image id="img-1" name="image-1" style="#select" filename="$img1" visibleToMouse="true"/>
<image id="img-2" name="image-2" style="#select" filename="$img2" visibleToMouse="true"/>
</panel>
</controlDefinition>

最佳答案

最好的方法是向面板添加“交互”元素:

<controlDefinition name="imagebutton" style="nifty-panel-style" controller="de.lessvoid.nifty.customs.imagebutton.ImageButtonControl">
<panel style="#panel" childLayout="overlay" focusable="true" visibleToMouse="true" width="$width" heigth="$height">

<!-- add this line to the panel -->
<interact onClick="onClick()" />

<!-- ... your other stuff -->

并将相应的“onClick()”处理程序方法添加到 Controller 实现中:

public class ImageButtonControl implements Controller {
...

public boolean onClick() {
// TODO add mouse click handling here
}

Nifty 将从下到上解析交互方法,从最里面的 Controller 开始向上到 ScreenController,第一个匹配获胜并被调用。

关于java - 使用 NiftyGUI 的自定义 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com