两个textfield t1,t2,一个按钮b.要求在t1内随便输入一个数n,然后按下b,就会在t2内出现一个数m=2n.这要什么样的语句才能转换啊,请好心人教我,感激!

解决方案 »

  1.   

    String t1="3";
    String t2 = "";try{
    t2 = ""+ (Integer.parseInt(t1)*2);
    }catch(Exception e){
    System.out.println("请输入数字");
    }
      

  2.   

    不是一样么,从textfield中取出来的都是String类型的,都是先要转换成int类型的
      

  3.   

    import java.applet.Applet;
    import java.awt.*;public class a extends Applet{

    private Label l1,l2;
    private TextField t1,t2;
    private Button b;

    public void init(){
    l1=new Label("华氏温度:");
    t1=new TextField();
    b=new Button("转换>>");
    l2=new Label("摄氏温度:");
    t2=new TextField();
    add(l1);add(t1);add(b);add(l2);add(t2);


    }
    public boolean action(Event e,Object o)
    {
    String s;
    if(e.target==b){
    ???
    }
    }
    }
    请问,问号里面怎么写?
      

  4.   

    String t1 ="122.122"
    String t2 = "" ;
    BigDecimal t1temp = new BigDecimal(t1);
    BigDecimal t2temp = new BigDecimal("2");
    t2 = t1temp.multiply(t2temp).toString ;
      

  5.   

    问题1:String t1 ="122.122"是什么意思?我是想在t1里面随便输入一个数,然后在转换.
    问题2: new BigDecimal(t1);
          new BigDecimal("2");后面括号里是不是t2?
    问题3:要是t2=(t1-32)*5/9,怎么写公式呢
      

  6.   

    t2.setText(""+2*Double.parseDouble(t1.getText()));
      

  7.   

    答:NO1:t1是你随便输入的数字,我的那个是虚拟了一个数字;
    NO2.后面括号里的是乘以的那个2,要计算精确的话最好用BigDemical;
    NO3.将所有数据转成BigDemical类型,然后进行,加减乘除,用BigDemical类型,分别为:add,subtract,multiply,divide进行运算
      

  8.   

    问题3:要是t2=(t1-32)*5/9,怎么写公式呢double d1=Double.parseDouble(t1.getText());
    double d2=(d1-32)*5/9;t2.setText(""+d2);//或者t2.setText(String.valueOf(d2));
      

  9.   

    把程序改成可执行的不是applet行吗?
    我不知道 TextField();怎么接收数据,谁能说一下吗?就是在事件触发那里不懂
    event.getActionCommand();是什么意思?
    NO1:t1是你随便输入的数字,我的那个是虚拟了一个数字;
    --------------------------------------------------
    不明白~~~啊~~~~!!!
    怎么搞个随便输入接收的TextField();?
      

  10.   

    event.getActionCommand();
    事件中的
    完整的是
    if(event.getActionCommand()=="转换>>");
    意思是说 当监听器接受到 或者触及到 "转换>>" 时 自己调用事件我这里有几个事件的例子
    供给自己参考import java.awt.*;
    import java.awt.event.*;class text extends Frame
    {
    Button b1 = new Button("红色");
    Button b2 = new Button("黄色");
    Button b3 = new Button("蓝色");
    Button b4 = new Button("绿色");
    Button b5 = new Button("紫色");
    boolean l ;
    text(String s)
    {
    super(s);
    setLayout(new FlowLayout());

    l = false;
    add(b1);
    add(b2);
    add(b3);
    add(b4);
    add(b5);

    text1 a1 = new text1(Color.red);
    text1 a2 = new text1(Color.orange);
    text1 a3 = new text1(Color.blue);
    text1 a4 = new text1(Color.green);
    text1 a5 = new text1(Color.cyan);
    b1.addActionListener(a1);
    b2.addActionListener(a2); b3.addActionListener(a3);
    b4.addActionListener(a4);
    b5.addActionListener(a5); addWindowListener(new text2());
    }
    private class text1 implements ActionListener
    { private Color c;
    text1(Color c1) {
    c= c1;
    }

    public void actionPerformed(ActionEvent a)
    {
    setBackground(c);
    }
    }

    private class text2 extends WindowAdapter
    {

    public void windowClosing(WindowEvent a1)
    {
    if(l)
    dispose();
    else
    System.exit(0);
    }

    }

    public static void main(String agrs[])
    {
    text t = new text("坏蛋");
    t.setSize(300,200);
    t.setVisible(true);
    }
    }
    import java.awt.*;import java.awt.event.*;class MyMulticast
    {
    public static void main(String args[])
    {
    MulticastFrame frame = new MulticastFrame();
    frame.show();
    }
    }class MulticastFrame extends Frame
    {
    public MulticastFrame()
    {
    setTitle("nihao");
    setSize(300,200);
    MulticastPanel panel = new MulticastPanel();
    add(panel);
    addWindowListener(new MyWindowAdapter());

    }
    private class MyWindowAdapter extends WindowAdapter
    {
    public void windowClosing(WindowEvent wevent)
    {
    System.exit(0);
    }
    }
    }class MulticastPanel extends Panel
    {
    public MulticastPanel()
    {
    Button newbutton = new Button("新建");
    add(newbutton);
    ActionListener newlistener = new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    makeNewFrame();
    }
    };
    newbutton.addActionListener(newlistener);
    closeAllButton = new Button("关闭全部");
    add(closeAllButton);
    }
    private void makeNewFrame()
    {
    final BlankFrame frame = new BlankFrame();
    frame.show();
    ActionListener closeAllListener = new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    frame.dispose();
    }
    };
    closeAllButton.addActionListener(closeAllListener);
    }
    private Button closeAllButton;
    }
    class BlankFrame extends Frame
    {
    public BlankFrame()
    {
    counter++;
    setTitle("窗体"+counter);
    setSize(200,150);
    setLocation(30*counter,30*counter);

    }

    private static int counter;
    }import java.awt.*;
    import java.awt.event.*;
    class ButtonControl extends Frame implements ActionListener
    {
    Button b = new Button("显示消息") ;
    Label l = new Label("");
    boolean w;
    ButtonControl(String s)
    {
    super(s);
    // setLayout(new FlowLayout());
    add(b,BorderLayout.NORTH);
    add(l,BorderLayout.CENTER);
    b.addActionListener(this);
    this.addWindowListener(new my());
    }
    public void actionPerformed(ActionEvent a)
    {
    if(a.getActionCommand().equals("显示消息"))
    {
    b.setLabel("隐藏消息");
    l.setText("哇!我出现了");
    }
    else if(a.getActionCommand().equals("隐藏消息"))
    {
    b.setLabel("显示消息");
    l.setText("");
    }
    }
    private class my extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    if(w)
    dispose();
    else
    System.exit(0);
    }
    }
    public static void main(String agrs[])
    {
    ButtonControl c =new ButtonControl("练习1");
    c.setSize(300,200);
    c.setVisible(true);
    }
    }