import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class math
{
JFrame jf;
JTextField jt;
JButton jb;
JButton jb1;
math()
{
jf=new JFrame("frame");
jf.setSize(200,200);
jf.setVisible(true);
jt=new JTextField(50);
jf.setLayout(new FlowLayout());
JButton jb=new JButton("30");
JButton jb1=new JButton("sin");
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jt.setText("30");
}
});
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String s=jt.getText();
double x=Double.parseDouble(s);
double s1=Math.sin(x);
jt.setText(s1);
}
});
JButton jb2=new JButton("cos");
jf.add(jt);
jf.add(jb);
jf.add(jb1);
jf.add(jb2);
}
public static void main(String args[])
{
new math();
}
}
//its giving error that double can't be applied to jt.setText
-1
A
回答
2
javax.swing.text.JTextField.setText(java.lang.String)
接受String
。您可以通過編寫此得到s1
一個String
表示:
jt.setText(Double.toString(s1));
現在,可以說是
jt.setText(String.valueOf(s1));
或
jt.setText(s1 + "");
是更好的選擇,因爲既不需要如果y變化ou重構s1
爲其他類型。
就我個人而言,我總是發現第三個選擇,字符串與空字符串連接,有點不雅觀的黑客。
5
setText
該方法採用一個String
,而不是一個double
。
通過致電Double.toString(someDouble)
,您可以從double
中獲得一個字符串。
1
的錯誤是不言自明,我認爲:
double s1=Math.sin(x);
jt.setText(s1);
的「的setText()」方法不希望兩倍的說法,因爲你在這裏給它;它想要一個字符串。因此,轉換你的雙爲字符串,並且它會工作:
double s1=Math.sin(x);
jt.setText(String.valueOf(s1));
0
的TextComponent
類的setText方法希望它的參數是String
類型的這樣的雙重你想它需要被轉換成通過一個字符串。
變化
jt.setText(s1);
到
jt.setText(Double.toString(s1));
相關問題
- 1. 在我的mvc應用程序中刪除自定義錯誤?
- 2. OpenERP的幫我刪除此
- 3. 幫我刪除mysql_insert_id
- 4. 我的iOS應用程序目標被錯誤地刪除
- 5. 這個程序有編譯時錯誤。請幫我將它們刪除
- 6. 我刪除__init__.py錯誤
- 7. 幫助cakephp刪除錯誤
- 8. 如何刪除我的程序中的綁定錯誤的索引?
- 9. 我的程序錯誤
- 10. 我的c程序錯誤
- 11. 我的程序段錯誤
- 12. 我的程序代碼中的錯誤
- 13. 我的NASM程序中的錯誤
- 14. JDBC錯誤。幫我
- 15. 請幫我解決我的Ajax錯誤
- 16. 誰刪除我的iOS應用程序?
- 17. 如何在我的程序中刪除這個錯誤爲什麼它會來?
- 18. 我如何從我的rails應用程序中刪除favicon
- 19. 我的程序中有什麼錯誤?
- 20. 我的程序中有什麼錯誤?
- 21. 我的程序中出現錯誤
- 22. 需要幫助理解我的第一個iPhone應用程序我的錯誤
- 23. 窗體中的錯誤。請幫助我
- 24. 在我的應用程序中排除錯誤處理概括
- 25. 我需要我的Java程序幫助
- 26. 請幫我用java編程錯誤
- 27. 錯誤在我的排序程序
- 28. 我的循環中的錯誤我的程序
- 29. 請幫我找錯誤在我的代碼(分段錯誤)
- 30. 我錯誤地刪除了我的res文件夾
什麼是完整的例外,該計劃拋出? – 2011-03-29 13:12:15
使用'jt.setText(s1 +「」);''或'jt.setText(String.valueOf(s1));' – 2011-03-29 13:12:47
OP發佈了後續問題,但已關閉:http://stackoverflow.com/問題/ 5476258 /程序-IS-未給予期望的輸出 – Pops 2011-03-29 17:15:55