Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA GUI赢得';我不能计算价格_Java_Eclipse_User Interface - Fatal编程技术网

JAVA GUI赢得';我不能计算价格

JAVA GUI赢得';我不能计算价格,java,eclipse,user-interface,Java,Eclipse,User Interface,我试图让这个应用程序计算,但它不会。花很多时间在它上面。我只想在按下按钮时计算一次。我需要它显示正确的价格时,点击正确的组合框。我想价格已经定好了 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; public class retailsalescalcu extends JFrame { //create the objects J

我试图让这个应用程序计算,但它不会。花很多时间在它上面。我只想在按下按钮时计算一次。我需要它显示正确的价格时,点击正确的组合框。我想价格已经定好了

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

public class retailsalescalcu extends JFrame {
    //create the objects
    JLabel department;
    JLabel number;
    JLabel name;
    JLabel price;
    JLabel discount;
    JLabel sale;
    JComboBox<String> dept;
    JTextField itemNum;
    JTextField itemName;
    JTextField itemPrice;
    JTextField itemDisc;
    JTextField salePrice;
    JButton calculate;
    JButton clear;

    public retailsalescalcu() {
        //set object variables
        super("Retail Sales Calculator");  //set window bar title
        setSize(300, 300);  //set window size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //set window              close
        GridLayout grid = new GridLayout(7, 2);
        setLayout(grid);
        department = new JLabel("Department");
        dept = new JComboBox<String>();
            dept.addItem("Select");
            dept.addItem("Men's Clothing");
            dept.addItem("Women's Clothing");
            dept.addItem("Shoes");
            dept.addItem("Belts");
            dept.addItem("Electronics");
            dept.addItem("Hats");
            //add ItemListener - JTextField & ComboBox
            dept.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent ie) {
                    String str = (String)dept.getSelectedItem();
                    itemNum.setText(str);
                }  //end public void
            });  //end item listener
        number = new JLabel("Item Number");
        itemNum = new JTextField(10);
        name = new JLabel("Item Name");
        itemName = new JTextField(10);
        price = new JLabel("Original Price");
        itemPrice = new JTextField(10);
        discount = new JLabel("Discount");
        itemDisc = new JTextField(10);
        sale = new JLabel("Sale Price");
        salePrice = new JTextField(10);
        salePrice.setEditable(false);
        calculate = new JButton("Calculate");
        clear = new JButton("Clear");

        //add objects to JFrame
        add(department);
        add(dept);
        add(number);
        add(itemNum);
        add(name);
        add(itemName);
        add(price);
        add(itemPrice);
        add(discount);
        add(itemDisc);
        add(sale);
        add(salePrice);
        add(calculate);
        add(clear);    

        //add event listener to calculate sale price
        calculate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent retail) {
                String input1;
                String input2;
                double origPrice;
                double percOff;
                double clearance;   

                input1 = itemPrice.getText();
                input2 = itemDisc.getText();
                origPrice = Double.parseDouble(input1);
                percOff = Double.parseDouble(input2)/100;
                clearance = origPrice - (origPrice * percOff); 
                DecimalFormat df = new DecimalFormat("$#,###.##");  
                df.format(clearance);                        
                salePrice.setText(df.format(clearance));                                         
                salePrice.setText(df.toString());  //output to JTextField    
            }
        });


        //add event listener to reset fields
        clear.addActionListener (new ActionListener() {
            public void actionPerformed(ActionEvent event) {  //JButton event if clicked
                dept.setSelectedIndex(0);  //Combo Box will be empty and can be reset
                itemNum.setText(null);  //Item Number will be empty and can be reset
                itemName.setText(null);  //Item Name will be empty and can be reset
                itemPrice.setText(null);  //Item Price will be empty and can be reset
                itemDisc.setText(null);  //Item Discount will be empty and can be reset
                salePrice.setText(null);  //Item SalePrice will be empty and can be reset
            }
        });

        setVisible(true);
    }  //end public retailsalescalcu

    public static void main(String[] args) {
        new retailsalescalcu();

    }  //end public static void

  }  //end public class retailsalescalcu
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.text.DecimalFormat;
公共类retailsalescalcu扩展了JFrame{
//创建对象
JLabel部门;
JLabel数;
JLabel名称;
JLabel价格;
JLabel折扣;
JLabel销售;
JComboBox部门;
JTextField itemNum;
JTextField itemName;
JTextField项目价格;
JTextField-itemDisc;
JTextField-salePrice;
JButton计算;
按钮清晰;
公共零售业{
//设置对象变量
super(“零售额计算器”);//设置窗口栏标题
设置大小(300300);//设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭
GridLayout grid=新的GridLayout(7,2);
设置布局(网格);
部门=新的JLabel(“部门”);
dept=新的JComboBox();
部门补充(“选择”);
附加部(“男装”);
附加部门(“女装”);
附加部门(“鞋”);
附加部门(“皮带”);
电子部;
附加部门(“Hats”);
//添加ItemListener-JTextField和ComboBox
dept.addItemListener(新的ItemListener(){
公共无效itemStateChanged(ItemEvent ie){
String str=(String)部门getSelectedItem();
itemNum.setText(str);
}//结束公共无效
});//结束项侦听器
编号=新JLabel(“项目编号”);
itemNum=新的JTextField(10);
名称=新JLabel(“项目名称”);
itemName=新的JTextField(10);
价格=新JLabel(“原价”);
itemPrice=新的JTextField(10);
折扣=新的JLabel(“折扣”);
itemDisc=新的JTextField(10);
销售=新JLabel(“销售价格”);
salePrice=新的JTextField(10);
salePrice.setEditable(false);
计算=新按钮(“计算”);
清除=新的按钮(“清除”);
//将对象添加到JFrame
增加(部门);
增加(部门);
添加(编号);
添加(itemNum);
加上(姓名);
添加(项目名称);
加上(价格);
添加(项目价格);
增加(折扣);
添加(项目光盘);
添加(销售);
加上(售价);
加(算);
添加(清除);
//添加事件侦听器以计算销售价格
calculate.addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件零售){
字符串输入1;
字符串输入2;
双孔;
双percOff;
双间隙;
input1=itemPrice.getText();
input2=itemDisc.getText();
origPrice=Double.parseDouble(input1);
percOff=Double.parseDouble(input2)/100;
间隙=初始价格-(初始价格*percOff);
DecimalFormat df=新的DecimalFormat($#,####.##“);
格式(清除);
salePrice.setText(df.format(clearance));
salePrice.setText(df.toString());//输出到JTextField
}
});
//添加事件侦听器以重置字段
clear.addActionListener(新ActionListener(){
public void actionPerformed(ActionEvent事件){//JButton事件(如果单击)
dept.setSelectedIndex(0);//组合框将为空,可以重置
itemNum.setText(null);//项目编号将为空并且可以重置
itemName.setText(null);//项名称将为空并且可以重置
itemPrice.setText(null);//项目价格将为空,可以重置
itemDisc.setText(null);//商品折扣将为空并且可以重置
salePrice.setText(null);//商品salePrice将为空,可以重置
}
});
setVisible(真);
}//结束公共零售业务
公共静态void main(字符串[]args){
新零售销售计算器();
}//结束公共静态无效
}//结束公共类RetailSalesCalcuc
这里有一个问题:

//salePrice.setText(df.toString()); 
将其注释掉,单击“计算”后您将看到您的价格。您已经在此处设置了
saleprice

salePrice.setText(df.format(clearance)); 

toString()
无法满足您的需要。

调试代码后,我发现您应该从代码中删除/comment行#100

编辑:


非常感谢您的回复,不过我已经尝试过了,但仍然不起作用@威士忌皮谢谢你的回答,还是不起作用。试着像你说的那样评论这句话@Lalit RaoI也附上了O/P图像。你现在还想要什么?它在我这边起作用了,很抱歉,我又试了一次,没有用。这可能是我的程序,我使用的是eclipse?IDE与此无关。你有什么例外?最好在Sale Price字段中说出您收到的文本?线程“AWT-EventQueue-0”java.lang.NumberFormatException中的异常:sun.misc.FloatingDecimal.readJavaFormatString(未知源)位于RetailSalesCalcuc的java.lang.Double.parseDouble(未知源)处$2.actionPerformed(RetailSalesCalcuc.java:87)javax.swing.AbstractButton.fireActionPerformed(未知源)javax.swing.AbstractButton$Handler.actionPerformed(未知源)javax.swing.DefaultButtonModel.fireActionPerformed(未知源)javax.swing.DefaultButtonModel.setPressed(未知源)
 DecimalFormat df = new DecimalFormat("$#,###.##");  
                        df.format(clearance);                        
                        salePrice.setText(df.format(clearance));                                         
                        salePrice.setText(df.toString());  //output to JTextField    
                        System.err.println(df.toString());
                    }
                }); 
 DecimalFormat df = new DecimalFormat("$#,###.##");  
                        df.format(clearance);                        
                        salePrice.setText(df.format(clearance));                                         
                        //salePrice.setText(df.toString());  //output to JTextField    
                        System.err.println(df.toString());
                    }
                });