2011-11-11 26 views
0

我试图建立一个百分比程序,发现一个数字的百分比从另一个数字。我想为jtextfield输入两个用户输入的数字,并将结果显示在另一个作为结果列出的jtextfield中。任何想法如何做到这一点我知道它需要动作监听器,但我不知道从那里去哪里。这是我得到它打印出我想要的只是不知道必须让actionlistener工作。图形用户界面的Java - 如何使用2个用户输入的数字来获得答案

package GUI; 

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


@SuppressWarnings("serial") 
public class WeightGUI extends JFrame{ 
private JLabel label1; 
private JTextField field1; 
private JLabel label2; 
private JTextField field2; 
private JLabel label3; 
private JTextField field3; 
private JLabel label4; 
private JTextField field4; 
private JButton button1; 
private JButton button2; 
private JButton button3; 
public WeightGUI() { 
    super("Percentage Weight Loss Calculator"); 
    setLayout(new FlowLayout()); 

    label1 = new JLabel ("Starting Weight "); 
    field1 = new JTextField ("Type"); 
    label2 = new JLabel ("Last Week Weight"); 
    field2 = new JTextField ("Type"); 
    label3 = new JLabel ("Current Weight"); 
    field3 = new JTextField ("Type"); 
    label4 = new JLabel ("Percentage Lost"); 
    field4 = new JTextField ("Results"); 
    button1 = new JButton (" Calculate Percent Total"); 
    button2 = new JButton (" Calculate Percent Week"); 
    button3 = new JButton (" Calculate Percent from last weight"); 
    label1 = new JLabel("Starting Weight"); 
    add(label1); 

    field1 = new JTextField("Type"); 
    add (field1); 

    label2 = new JLabel("Last Week Weight"); 
    add(label2); 

    field3 = new JTextField(10); 
    add (field2); 

    label3 = new JLabel("Current Weight"); 
    add(label3); 

    field3 = new JTextField("Type"); 
    add (field3); 

    label4 = new JLabel("Percentage Lost"); 
    add(label4); 

    field4 = new JTextField("Results"); 
    field4.setEditable(false); 
    add (field4); 

    button1 = new JButton ("Calculate Total Percent"); 
    add(button1); 

    button2 = new JButton ("Calculate Percent from last Weight"); 
    add(button2); 

    button3 = new JButton ("Calculate Weekly Percent"); 
    add(button3); 

    thehandler handler = new thehandler(); 
    button1.addActionListener(handler); 

    thehandler handler1 = new thehandler(); 
    button2.addActionListener(handler1); 

    thehandler handler2 = new thehandler(); 
    button3.addActionListener(handler2); 
} 

private class thehandler implements ActionListener { 

    public void actionPerformed (ActionEvent e){ 

     int sw; 
     int lw; 
     int cw; 
     double weight = 0 ; 
     String inputString; 
     inputString = field1.getText(); 
     inputString = field2.getText(); 
     inputString = field3.getText(); 
     sw = Integer.parseInt(inputString); 
     lw = Integer.parseInt(inputString); 
     cw = Integer.parseInt(inputString); 
      if(e.getSource() == button1) { 
       weight = (sw - cw)/sw; 
      } 
      else if(e.getSource() == button2) { 
       weight = (lw - cw)/sw; 
      } 
      else if(e.getSource() == button3){ 
       weight = (lw - cw)/ lw; 



    } 



    } 
} 


} 
+0

请告诉我们它是如何工作的。详细信息*很重要。祝你好运。 –

+0

此外,你会想要重命名你的变量,给他们的名字是有道理的。从现在开始,更容易调试一个名为“field4”的变量或名为“resultsField”的变量? –

回答

1

你的代码的主要问题是你没有设置保存结果的JTextField的文本。只需在此JTextField上调用setText(...)并传入表示结果的字符串即可完成此操作。您还需要删除JTextField中的无用文本“Type”,因为它们可能会搞砸了,除非要将FocusListener添加到所有文本字段,以便在focusGained上选择该字段的所有内容。

您还需要重新命名您的变量,如上面我的评论中所述。

0

您应该使用setText()方法在适当的字段中设置结果。但我有一个问题要问你:你说两个用户在同一时间设置了他们的输入,我的意思是你的应用程序是分发的?换句话说,用户在网络中并使用它!?

+0

那么,他只是想摆弄从多个JTextField中提取的文本。 –