Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 String怎样转成double
laziliyi





发贴: 2
积分: 0
于 2006-06-02 13:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因为需要从JTextField中取值计算,但一输入带小数点的就会出问题


作者 Re:String怎样转成double [Re:laziliyi]
laziliyi





发贴: 2
积分: 0
于 2006-06-02 13:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test5 extends JFrame implements WindowListener,ActionListener{
JLabel jl1=new JLabel("本金");
JLabel jl2=new JLabel("利率");
JLabel jl3=new JLabel("存款年数");
JLabel jl4=new JLabel("每年年金额总计");
JTextField jt1=new JTextField(10);
JTextField jt2=new JTextField(10);
JTextField jt3=new JTextField(10);
JTextArea ja1=new JTextArea(4,6);
JButton jbtnAdd=new JButton("计算");
//GridBagLayout gb;
;
/** Creates a new instance of Test5 */
public Test5(String s) {
superMoon;
//GridBagLayout gd=new GridBagLayout();

Container jfrm=getContentPane();

//jfrm.setLayout(new GridLayout());
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(3,2));
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(2,1));
p1.add(jl1);
p1.add(jt1);
p1.add(jl2);
p1.add(jt2);
p1.add(jl3);
p1.add(jt3);
jfrm.add(p1,BorderLayout.WEST);
p2.add(jl4);
p2.add(ja1);
jfrm.add(p2,BorderLayout.SOUTH);
jfrm.add(jbtnAdd,BorderLayout.EAST);
addWindowListener(this);
jbtnAdd.addActionListener(this);
}
public static void main(String[] args) {
Test5 t=new Test5("计算");
t.setSize(400,300);
t.setVisible(true);
}

public void windowOpened(WindowEvent e) {
}

public void windowClosing(WindowEvent e) {
System.exit(0);
}

public void windowClosed(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowActivated(WindowEvent e) {
}

public void windowDeactivated(WindowEvent e) {
}

public void actionPerformed(ActionEvent e) {
double sum=1,nu1,nu2,nu3;

nu1=(double)Integer.parseInt(jt1.getText());
nu2=(double)Integer.parseInt(jt2.getText());
nu3=(double)Integer.parseInt(jt3.getText());
sum=sum*nu1*(1.0f+nu2);
for(int i=1;i<Integer.parseInt(jt3.getText());i++)
sum=sum*(1.0f+nu2);
ja1.setText(String.valueOf(sum));

//getWrapStyleWord() 获取换行方式(如果文本区要换行)。
}



}
以上是代码部分



作者 Re:String怎样转成double [Re:laziliyi]
lisliefor





发贴: 287
积分: 7
于 2006-06-03 11:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
String 转DOuble: String s
double b = Double.ValueOfMoon.doubleValue();
该语句需要放入try{}catch(NumberFormatException e){}块中,当S包含非数字字符的时候,会抛出该异常。



作者 Re:String怎样转成double [Re:lisliefor]
cqx2004





发贴: 5
积分: 0
于 2006-06-03 13:39 user profilesend a private message to usersend email to cqx2004search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
double b = Double.ValueOfMoon.doubleValue();


作者 Re:String怎样转成double [Re:laziliyi]
大山之鹰





发贴: 4
积分: 0
于 2006-06-08 14:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以用
nu1=Double.parseDouble(jt1.getText());
nu2=Double.parseDouble(jt2.getText());
nu3=Double.parseDouble(jt3.getText());




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923