将Actionlistener类型转换为整数java

时间:2012-05-28 15:11:46

标签: java swing user-interface actionlistener

我需要将Actionlistener类型转换为整数来调用方法。 有什么想法吗?

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

1 个答案:

答案 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

希望这有帮助!