为什么字符串“ getPriceDisplay”不会出现在我的JTextField“ totalTextField”中?

如何解决为什么字符串“ getPriceDisplay”不会出现在我的JTextField“ totalTextField”中?

| 为什么在我的
JTextField
totalTextField
中不会显示字符串\“ getPriceDisplay \”?
// 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;
    private JTextField inputText; 
    DefaultListModel dm = new DefaultListModel();
    public menuItem[] mi; 
    private double getPrice; 
    private String getPriceDisplay; 

    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(); 

            }

        }
                getPriceDisplay = Double.toString(getPrice);
                getPriceDisplay = totalText.getText();
                totalText = new JTextField(getPriceDisplay); 

                }
        });

        orderList = new JList(dm);

        JScrollPane orderListScrollPane = new JScrollPane(orderList);

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


        JScrollPane menuListScrollPane = new JScrollPane(menuList);
        getContentPane().add(menuListScrollPane);
        layout.setConstraints(
            menuListScrollPane,new GridBagConstraints(
                0,0));

        JScrollPane recieptListScrollPane = new JScrollPane();
        getContentPane().add(recieptListScrollPane);
        layout.setConstraints(
            recieptListScrollPane,new GridBagConstraints(
                3,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,0));

        inputText = new JTextField();
        Font inputFont = new Font(\"Verdana\",30);
        totalText.setFont(inputFont);
        totalText.setForeground(Color.RED);
        getContentPane().add(inputText);
        layout.setConstraints(
            inputText,0));

        JButton payLabel = new JButton(\"Pay\");
        getContentPane().add(payLabel);
        layout.setConstraints(
            payLabel,0));



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



            }
        };
        getContentPane().add(orderButton);
        layout.setConstraints(
            orderButton,0.5,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,5),0));



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

                }
            };
        getContentPane().add(cancelButton);
        layout.setConstraints(
            cancelButton,GridBagConstraints.EAST,GridBagConstraints.NONE,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);
        }


        // 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 ()
    {

    }
}
    

解决方法

在您的鼠标侦听器中,将一个新的“ 0”实例分配给“ 4”。
...
totalText = new JTextField(getPriceDisplay); 
...
相反,您应该更改文本
...
totalText.setText(\"the new value\");
...
顺便说一句:您的程序中还有另一个错误。由于这是家庭作业,因此我将其保留为练习供您查找。 :-)     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-