Java 在JTextField中有一个整数

Java 在JTextField中有一个整数,java,swing,jtextfield,Java,Swing,Jtextfield,如何在JTextField中显示整数。我创建了一个for循环,以检查我在JList中选择的字符串是否与我在类中创建的另一个数组匹配。然后我会检查它们是否相等,然后使用我在另一个类中为每个菜创建的GetPrice方法,并在JTextField(totalText字段)中以整数而不是字符串的形式显示该价格 // NamesFrame.java // // Informatics 45 Spring 2010 // Code Example: GridBagLayouts, JLists, and L

如何在JTextField中显示整数。我创建了一个for循环,以检查我在JList中选择的字符串是否与我在类中创建的另一个数组匹配。然后我会检查它们是否相等,然后使用我在另一个类中为每个菜创建的GetPrice方法,并在JTextField(totalText字段)中以整数而不是字符串的形式显示该价格

// NamesFrame.java
//
// Informatics 45 Spring 2010
// Code Example: GridBagLayouts, JLists, and ListModels
// Version 2
//
// This version of NamesFrame employs model/view separation.  In order to
// do it, it stores a NameCollection (our model) instead of a DefaultListModel,
// then implements its own custom ListModel that knows how to handle event
// notifications from NameCollection and turn them into events that JLists
// can handle.

package inf45.spring2010.examples.gui3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MenuFrame extends JFrame
{
    private MenuCollection menu;
    private JList menuList;
    private JTextField newNameField;
    private JList orderList; 
    private JButton orderButton, cancelButton; 
    private JTextField totalText, inputText; 
    DefaultListModel dm = new DefaultListModel();
    public menuItem[] mi; 
    private double getPrice; 


    menuItem [] menuArray = new menuItem [13]; 








    public MenuFrame()
    {


        menu = new MenuCollection();

        setTitle("Senor Club");
        setSize(1000, 1000);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        menuArray[0] = new menuItem("Cheese Enchilada", 2.95, true); 
        menuArray[1]= new menuItem ("Chicken Enchilada", 3.59, true);
        menuArray[2]= new menuItem ("Beef Taco", 1.69, true);
        menuArray [3]= new menuItem ("Chicken Taco", 1.89, true);
        menuArray [4] = new menuItem ("Fish Taco", 2.39, true);
        menuArray [5] = new menuItem ("Bean and Cheese Burrito", 3.19, true);
        menuArray [6] = new menuItem ("Chicken Burrito", 5.49, true);
        menuArray [7] = new menuItem ("Steak Burrito", 6.49, true);
        menuArray [8] = new menuItem ("Carnitas Burrito", 6.79, true);
        menuArray [9] = new menuItem ("Chips and Salsa", .99, true);
        menuArray [10] = new menuItem ("Guacamole", 2.49, false);
        menuArray [11] = new menuItem ("Small Drink", 1.45, false);
        menuArray [12] = new menuItem ("Large Drink", 1.95, false);

        mi = menuArray; 

        buildUI();
    }


    // All of the layout code has stayed the same.  The only difference in
    // this method is where we create the list model; instead of creating a
    // DefaultListModel (which stores its own data), we create a NamesListModel,
    // our own custom list model that knows how to interact with the
    // NameCollection.
    private void buildUI()
    {

        GridBagLayout layout = new GridBagLayout();
        getContentPane().setLayout(layout);







        NamesListModel listModel = new NamesListModel(menu);
        menuList = new JList(menuArray);

        menuList.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {

                Object selected = menuList.getSelectedValue();
                System.out.println("menuList.addMouseListener.mousePressed selected=" + selected);
                DefaultListModel dm = (DefaultListModel) orderList.getModel();
                dm.add(orderList.getModel().getSize(), selected);

                for (int i = 0; i < menuArray.length; i++){
                    if (selected.equals(mi[i].getItemName())) {
                        getPrice =      mi[i].getItemPrice();



            }
                    totalText.add(comp, index)
        }}
        });




        orderList = new JList(dm);



        JScrollPane orderListScrollPane = new JScrollPane(orderList);

        getContentPane().add(orderListScrollPane);
        layout.setConstraints(
            orderListScrollPane,
            new GridBagConstraints(
                1, 1, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 10, 5, 10), 0, 0));


        JScrollPane menuListScrollPane = new JScrollPane(menuList);
        getContentPane().add(menuListScrollPane);
        layout.setConstraints(
            menuListScrollPane,
            new GridBagConstraints(
                0, 1, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 10, 5, 10), 0, 0));

        JScrollPane recieptListScrollPane = new JScrollPane();
        getContentPane().add(recieptListScrollPane);
        layout.setConstraints(
            recieptListScrollPane,
            new GridBagConstraints(
                3, 1, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 10, 5, 10), 0, 0));

        totalText = new JTextField(); 
        Font font = new Font("Verdana", Font.BOLD, 30);
        totalText.setFont(font);
        totalText.setForeground(Color.RED);
        getContentPane().add(totalText);
        layout.setConstraints(
                totalText,
                new GridBagConstraints(0, 3, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 10), 0, 0));

        inputText = new JTextField();
        Font inputFont = new Font("Verdana", Font.BOLD, 30);
        totalText.setFont(inputFont);
        totalText.setForeground(Color.RED);
        getContentPane().add(inputText);
        layout.setConstraints(
            inputText,
            new GridBagConstraints(
                1, 3, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 10, 5, 10), 0, 0));

        JButton payLabel = new JButton("Pay");
        getContentPane().add(payLabel);
        layout.setConstraints(
            payLabel,
            new GridBagConstraints(
                3, 3, 2, 1, 1.0, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 10, 5, 10), 0, 0));



        orderButton = new JButton("Order");
        new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {



            }
        };
        getContentPane().add(orderButton);
        layout.setConstraints(
            orderButton,
            new GridBagConstraints(
                0, 2, 1, 1, 0.5, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                new Insets(5, 10, 5, 5), 0, 0));



        cancelButton = new JButton("Cancel");
        //addButton.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    cancelPressed(); 

                }
            };
        getContentPane().add(cancelButton);
        layout.setConstraints(
            cancelButton,
            new GridBagConstraints(
                1, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.NONE,
                new Insets(5, 5, 5, 10), 0, 0));
    }


    // In this method, we add the new name to the NameCollection instead of
    // adding it directly to the list model.  This sets off a chain of events:
    // 
    // * NameCollection notifies its listener, the NamesListModel, that a name
    //   has been added.
    // * NamesListModel notifies its listener, the JList, that an "interval"
    //   (a subsequence of the elements) has changed.
    // * JList redraws the affected elements (if they're visible).
    //
    private void addName()
    {
        String newName = newNameField.getText().trim();

        if (newName.isEmpty())
        {
            JOptionPane.showMessageDialog(
                this, "No name was specified.  Please enter a name.", "No Name Specified",
                JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            menu.add(newName);
            newNameField.setText("");
        }
    }


    // In this method, we remove the name from the NameCollection instead of the
    // list model, which sets off the same chain of events that addName() does.


    // This is a custom list model.  It extends AbstractListModel, which
    // provides a basic implementation of some of the ListModel functionality,
    // while leaving the important parts of it (namely, how to figure out how
    // large the collection is and how to get an element from the collection)
    // unimplemented; this class implements those two elements, which are
    // called getSize() and getElementAt().
    //
    // Our list model also implements the NameCollectionChangedListener
    // interface, so that it can receive change events from the NameCollection.
    // It's legal in Java for a class to extend another class and implement
    // an interface.  (In fact, it's legal for a class to extend a class and
    // implement multiple interfaces, if you're so inclined.)  It's not legal,
    // however, for a class to extend multiple other classes.
    private class NamesListModel
    extends AbstractListModel
    implements NameCollectionChangedListener
    {
        // The list model will operate on the collection, so we'll need to
        // store a reference to the collection inside the list model.
        private MenuCollection collection;


        // When we create the list model, we need to store the collection
        // and register the list model as a listener on the collection,
        // so that whenever the collection is changed, the list model
        // will be notified.
        public NamesListModel(MenuCollection collection)
        {
            this.collection = collection;
            collection.addNameCollectionChangedListener(this);
        }


        // This is the method that will be called whenever the NameCollection
        // has a name added to it.  What it does is fire an "interval added"
        // event, which is how it tells its JList that some subsequence of
        // elements has changed.  The three parameters are (1) who sent the
        // event, (2) where the sequence of changed elements begins, and
        // (3) where the sequence of changed elements ends.  In our case, the
        // sequence of changed elements is just the one element, so we say
        // "index" in both places; why the method specifies the beginning and
        // end separately is so that you can cheaply add, say, 10 elements
        // without having to fire 10 events.
        public void nameAdded(int index)
        {
            fireIntervalAdded(this, index, index);
        }


        // This is the method that will be called whenever the NameCollection
        // has a name removed from it.  It does something similar to nameAdded(),
        // except that it notifies its JList about the removal of an element,
        // appropriately, instead of the addition of an element.
        public void nameRemoved(int index)
        {
            fireIntervalRemoved(this, index, index);
        }


        // Whenever the JList asks the list model how many elements there are
        // in the list, the list model will just ask the collection "How many
        // names have you got?"
        public int getSize()
        {
            return collection.getSize();
        }


        // Whenever the JList asks the list model what element is at a
        // particular index, the list model will just ask the collection
        // "What name is at this index?"
        public Object getElementAt(int index)
        {
            return collection.get(index);
        }
    }


    private void cancelPressed()
    {
        dispose();
    }
    public void addMenu ()
    {

    }
}
//NamesFrame.java
//
//2010年春季信息学45
//代码示例:GridBagLayouts、JLists和ListModels
//版本2
//
//此版本的NameFrame采用模型/视图分离。为了
//这样做,它将存储一个名称集合(我们的模型),而不是DefaultListModel,
//然后实现自己的定制ListModel,它知道如何处理事件
//来自NameCollection的通知,并将其转换为JLists的事件
//我能应付。
包inf45.spring2010.examples.gui3;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类MenuFrame扩展了JFrame
{
私有菜单采集菜单;
私家侦探;
私有JTextField newNameField;
私人JList订单列表;
私有JButton orderButton、cancelButton;
私有JTextField totalText,inputText;
DefaultListModel dm=新的DefaultListModel();
公共菜单项[]mi;
私人双价;
menuItem[]menuArray=新的menuItem[13];
公共菜单框架()
{
菜单=新建菜单集合();
setTitle(“Senor俱乐部”);
设置大小(10001000);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
menuArray[0]=新menuItem(“奶酪Enchilada”,2.95,真);
menuArray[1]=新menuItem(“鸡肉卷”,3.59,真);
menuArray[2]=新menuItem(“牛肉塔可”,1.69,正确);
menuArray[3]=新menuItem(“鸡肉塔可”,1.89,正确);
menuArray[4]=新menuItem(“鱼塔可”,2.39,真);
menuArray[5]=新menuItem(“豆和奶酪煎饼”,3.19,正确);
menuArray[6]=新menuItem(“鸡肉煎饼”,5.49,正确);
menuArray[7]=新menuItem(“牛排玉米煎饼”,6.49,正确);
menuArray[8]=新menuItem(“Carnitas玉米煎饼”,6.79,真);
menuArray[9]=新menuItem(“薯片和莎莎酱”,.99,正确);
menuArray[10]=新menuItem(“鳄梨酱”,2.49,假);
menuArray[11]=新menuItem(“小饮料”,1.45,假);
menuArray[12]=新menuItem(“大饮料”,1.95,假);
mi=月经量;
buildUI();
}
//所有的布局代码都保持不变。唯一的区别是
//这个方法是我们创建列表模型的地方;而不是创建
//DefaultListModel(存储自己的数据),我们创建一个NameListModel,
//我们自己的自定义列表模型知道如何与
//名字收藏。
私有void buildUI()
{
GridBagLayout=新的GridBagLayout();
getContentPane().setLayout(布局);
NamesListModel listModel=新建NamesListModel(菜单);
menuList=新JList(menuArray);
menuList.addMouseListener(新的MouseAdapter()
{
@凌驾
公共无效鼠标按下(MouseEvent e)
{
所选对象=menuList.getSelectedValue();
System.out.println(“menuList.addMouseListener.mousePressed selected=“+selected”);
DefaultListModel dm=(DefaultListModel)orderList.getModel();
dm.add(orderList.getModel().getSize(),选中);
对于(int i=0;i