2012-05-28 48 views
1

我需要将Actionlistener类型转换为整数来调用方法。 任何想法?将Actionlistener类型转换为整数java

import javax.swing.*; 
import java.awt.*; 
/** 
* sold by pound 
*/ 
public class CandyPanel extends JPanel { 
    public CandyPanel() { 
     // Create a GridLayout manager with 
     // two rows and one column. 
     setLayout(new GridLayout(1, 1)); 

     // Create the radio buttons. 
     JPanel gummyBears = new JPanel(new BorderLayout()); 

     // Add a border around the panel. 
     setBorder(BorderFactory.createTitledBorder("Gummy Bears")); 

     // Add the JPanel to the panel. 
     add(gummyBears); 
    } 

    /** 
    * returns the total cost of the item based on the 
    * number of units purchased 
    * @param number the number of units purchased 
    * @return   the total cost of those units 
    */ 
    public double getCost(int number) { 
     double total = number * 2.00; 
     return total; 
    } 

    /** 
    * Prints the ordered item 
    * @param amount the number of units purchased 
    * @param cost  the total cost of those units 
    */ 
    public void printOrderItem(int amount, double cost){ 
     JOptionPane.showMessageDialog(null, amount + " Gummy Bears at $2.00/pound = $" + cost + "."); 
    } 
} 

这是将需要转换的int的方法。

public double getCost(int number) { 
    double total = number * 2.00; 
    return total; 
} 

我试着做parseInt在线研究,但它不与ActionEvent工作。

+0

'e'不是一个字符串。整数值应该来自哪里? –

+0

是的,我意识到它不是一个字符串。 整数值应该是用户输入的内容。 我会问有多少需要和用户会给我输入。问题是,ActionEvent是监听器,所以我希望有一个临时值来存储解析的ActionEvent类型。 会getText()的工作? – user1283885

+0

输入应该去哪里?有没有文本框?目前,您提供的代码中没有任何地方提到用户输入数字的方式。 –

回答

1

您不能将一个ActionEvent转换为一个int。但如果这是你想转换为int用户输入说一个文本框,将是这样的:

String s = jTextBox.getText(); 
int i = Integer.valueOf(s); 

,然后再将整数值到你的方法。 还有一个关于如何在这里使用ActionListener的教程,如果你需要帮助:http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

希望这有助于!