2017-04-20 50 views
0

我正在处理一个压缩图像的插件。在“魔术发生”之前,用户应该决定,如果他想用三个或四个邻居的方法。 为此,我创建了一个带有RadioButtonGroup的通用对话框。ImageJ:获取特定的Radiobox

这工作正常,我的问题是我如何得到用户的选择?方法getRadioButton返回一个Vector。但我不知道如何处理这个问题。我的计划是使用按钮索引作为我的主类选择参数,但我没有看到一种方法来管理这个。

你有什么想法吗?

编辑:我的代码(不含进口)

public class FrameDemo_ extends PlugInFrame { 
    public FrameDemo_(){ 
     super("FrameDemo"); 
    } 

    public void run (String arg){ 
     String[] items = {"Option A", "Option B"}; 
     GenericDialog gd = new GenericDialog("FrameDemo settings"); 
     gd.addRadioButtonGroup("Test",items,2,1,"0"); 
     gd.showDialog(); 
     if (gd.wasCanceled()){ 
      IJ.error("PlugIn canceled!"); 
      return; 
     } 
     String input; 
     Vector vec = gd.getRadioButtonGroups(); 
     Object obj = vec.elementAt(0); 
     input = obj.toString(); 

     this.setSize(250, 250); 
     this.add(new Label(input, Label.CENTER)); 
     this.setVisible(true); 
    } 
} 

我的目标是做这样的事情:

input = obj.Somefunction; // Input contains for exampe "A" for Option A 

class RealPlugin (parameter input){ 
    if(input == A) { do something } // Pseudocode...not the real if for a string 

else if {input == B) {do something else } 

我的问题是,当我将对象转换为字符串,它是:

java.awt.CheckboxGroup [selectedCheckbox = java.awt.Checkbox [checkbox0,0,0,66x23,invalid,label = Option A,state = true}}

我敢肯定,有一种字符串操作的方式,但我不认为这是做到这一点的正确方法。我的意思是这必须是一个非常典型的工作(使用RadioButtonGroup中获得用户的选择),必须有一个聪明的办法或功能...

+0

请出示,你做了什么 –

回答

1

注意:类似这样的问题通常回答快得多的ImageJ forum,更多ImageJ专家将会阅读它们。


要检索从无线电按钮组的结果,使用的GenericDialoggetNextRadioButton方法。下面是可以直接从ImageJ中的script editor运行一个小的Groovy脚本:

import ij.gui.GenericDialog 

gd = new GenericDialog("FrameDemo Settings") 

items = ["Option A", "Option B"] 
gd.addRadioButtonGroup("Test", (String[]) items, 2, 1, "0") 

gd.showDialog() 

if (gd.wasOKed()) { 
    answer = gd.getNextRadioButton() 
    println answer 
} 

随着ImageJ2(附带附带的ImageJ的Fiji分布),它更容易得到这样的选择,使用SciJava script parameters

// @String(label="Choice", choices={"Option A", "Option B"}, style="radioButtonVertical") choice 

println choice 

在Java中,同样是这样的:

@Plugin(type = Command.class, menuPath = "Plugins>My New Plugin") 
public class MyNewPlugin implements Command { 

    @Parameter (label="Choice", choices={"Option A", "Option B"}, style="radioButtonVertical") 
    private String choice; 

    // your code here 
} 

对于一个完整的示例,请参阅https://github.com/imagej/example-imagej-command

+0

对不起,我迟到的回应。我试着在最近两天使用你的代码片段,但是我得到的只是IDE中的错误代码(IntelliJ)或消息“Unable to load plugin(ins)”。 我认为我的代码应该在“你的代码在这里”的地方工作,我做了一些改变,看看这是否有帮助,但总而言之,这是行不通的。 我看你的例子,但它并没有真正帮助我。 你可以给我一个简单的例子,其中,在单选按钮选择后,出现一个窗口中出现一个新窗口,其中A表示选项A的消息“A”,B表示窗口中央的字符串B的选项B。 –