我目前仍然坚持使用一些程序,我确信有一种方法可以做到,但只是想不到它,所以这就是它。所以我正在使用eclipse窗口查看器,我根据自己的需要制作了一个GUI,我得到了一个Ccombo,有几个选项可供选择,我想要做的是点击一个按钮我希望程序能够看到他们选择了什么,然后根据他们选择的内容将不同的结果放入另一个文本字段
OptionsOptimizer = new Shell();
OptionsOptimizer.setSize(450, 340);
OptionsOptimizer.setText("Options Optimizer");
Label lblSpread = new Label(OptionsOptimizer, SWT.NONE);
lblSpread.setAlignment(SWT.CENTER);
lblSpread.setBounds(10, 10, 213, 15);
lblSpread.setText("Type Of Spread");
CCombo combo = new CCombo(OptionsOptimizer, SWT.BORDER);
combo.setItems(new String[] {"Put Credit Spread", "Short Naked Put", "Put Broken Wing Butterfly", "Custom Naked Put", "Call Debit Spread", "Call Calander", "Call Ratio Backspread", "Put Diagonal", "Short Straddle", "Short Strangle", "Short Iron Condor", "Short Iron Butterfle"});
combo.setBounds(10, 31, 213, 21);
Button Enter = new Button(OptionsOptimizer, SWT.NONE);
Enter.addMouseListener(new MouseAdapter() {
public void mouseDoubleClick(MouseEvent e) {
double profit = Double.parseDouble(Profit.getText());
double margin = Double.parseDouble(Margin.getText());
double roi = profit/margin;
double roundroi = Math.round(roi*10000)/100.0;
ROI.setText("Your return on investment is " + roundroi + "%");
double percent = Double.parseDouble(ITM.getText());
double OTM = ((100 - percent)/100);
int day = Integer.parseInt(Days.getText());
double roc = (roi/day)*OTM;
double roundroc = Math.round(roc*10000)/100.0;
ROC.setText("Your return on capital per day is " + roundroc + "%");
double annual = roc*365*.4;
double roundannual = Math.round(annual*10000)/100.0;
Annual.setText("Your annual return is " + roundannual + "% if you invested 40% of your total equity");
Point spread = combo.getSelection();
if(spread.toString() == "Put Credit Spread")
{
Volitility.setText("A fall in implied volitility will help your position");
}
});
Enter.setBounds(159, 108, 75, 25);
Enter.setText("Evaluate");
}
答案 0 :(得分:1)
我可以看到您的代码存在一个问题:您正在使用==
比较按钮的文字,但您应该使用spread.toString().equals("Put Credit Spread")
。
有关详细信息,请参阅How do I compare strings in Java?。
作为一点建议:CCombo有一个getSelectionIndex
函数,它返回从0
开始的所选项的整数值,如果没有选择项,则返回-1
。使用此功能可以获得更改项目文本的好处,而无需更改逻辑。