Java 计算价格有困难吗
我目前做的订购系统,我遇到的问题,如显示在我的面板的总价格Java 计算价格有困难吗,java,Java,我目前做的订购系统,我遇到的问题,如显示在我的面板的总价格 public class SummaryPanel extends MasterPanel { private JTable table; public JTable tblList3; protected ArrayList<Food>foodList; protected JComboBox category_CB; protected JComboBox cb_FoodItem; private JComboBox
public class SummaryPanel extends MasterPanel {
private JTable table;
public JTable tblList3;
protected ArrayList<Food>foodList;
protected JComboBox category_CB;
protected JComboBox cb_FoodItem;
private JComboBox combo;
private JTextField ImagePath_TF;
private double total;
private String totalPrice;
private Orders orders;
private JComboBox comboBox;
private JComboBox cb;
private JComboBox cb_tableno;
private double totalAmount;
private ComboBoxModel cb_nopax1;
private JComboBox cbNoPax;
private String numPax;
private String tableNo;
private JLabel txtPrice;
private Double s;
/**
* Create the panel.
*/
public SummaryPanel(JFrame myFrame, ArrayList<Food> listSelected) {
super(myFrame);
this.foodList =listSelected;
setBackground(Color.WHITE);
setForeground(Color.WHITE);;
JLabel lblConfirmOrder = new JLabel("Confirm Order");
lblConfirmOrder.setFont(new Font("Tw Cen MT", Font.PLAIN, 26));
lblConfirmOrder.setBounds(52, 105, 173, 49);
add(lblConfirmOrder);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(27, 342,568, 204);
add(scrollPane);
tblList3 = new JTable();
setTableModel();
scrollPane.setViewportView(tblList3);
JButton btnHome = new JButton("Cancle Order");
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
actionPerformedCancleOrder();
}
});
btnHome.setBounds(93, 554, 119, 43);
add(btnHome);
JLabel lblTableNo = new JLabel("Table No");
lblTableNo.setBounds(337, 241, 67, 39);
add(lblTableNo);
JButton btnOrder = new JButton("Order");
btnOrder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
actionPerformedOrder();
}
});
btnOrder.setBounds(297, 554, 151, 43);
add(btnOrder);
JLabel lblNumberOfPax = new JLabel("Number Of Pax");
lblNumberOfPax.setBounds(337, 174, 106, 27);
add(lblNumberOfPax);
/*JButton btnPrice = new JButton("Price");
btnPrice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedTotalPrice();
}
});
btnPrice.setBounds(71, 176, 89, 23);
add(btnPrice); */
JLabel label = new JLabel("")label.setIcon(newImageIcon(SummaryPanel.class.getResource("/fourFinger/
images/Wall-E6.jpg")));
label.setBounds(636, 327, 253, 258);
add(label);
JLabel lblPleaseIndicateThe = new JLabel("Please indicate the ");
lblPleaseIndicateThe.setFont(new Font("Tw Cen MT", Font.BOLD, 19));
lblPleaseIndicateThe.setBounds(561, 174, 173, 55);
add(lblPleaseIndicateThe);
JLabel lblTheSelectedFood = new JLabel("quantity of the");
lblTheSelectedFood.setFont(new Font("Tw Cen MT", Font.BOLD, 19));
lblTheSelectedFood.setBounds(561, 209, 173, 31);
add(lblTheSelectedFood);
JLabel lblFoodSelected = new JLabel("food selected");
lblFoodSelected.setFont(new Font("Tw Cen MT", Font.BOLD, 19));
lblFoodSelected.setBounds(561, 251, 119, 14);
add(lblFoodSelected);
JLabel lblDsdsds = new JLabel("");
lblDsdsds.setIcon(new ImageIcon(SummaryPanel.class.getResource("/fourFinger/images/chatbox.png")));
lblDsdsds.setBounds(527, 151, 237, 176);
add(lblDsdsds);
txtPrice = new JLabel("");
txtPrice.setBounds(113, 177, 74, 21);
add(txtPrice);
cb_tableno = new JComboBox();
cb_tableno.setBounds(424, 250, 93, 20);
cb_tableno.setModel(new DefaultComboBoxModel(new String[]{ "1","2","3","4","5","6","7","8","9","10"}));
add(cb_tableno);
cbNoPax = new JComboBox();
cbNoPax.setBounds(424, 177, 93, 20);
cbNoPax .setModel(new DefaultComboBoxModel(new String[]{ "1","2","3","4","5","6","7","8","9","10"}));
add(cbNoPax);
}
private void actionPerformedOrder() {
//retrieve user input
String numPax = (String) cbNoPax.getSelectedItem();
String tableNo= (String)cb_tableno.getSelectedItem();
Date orderDate = new Date();
orders=newOrders(Integer.parseInt(tableNo),Integer.parseInt(numPax),orderDate, totalAmount);
int orderID = OrdersDA.createOrders(orders);
}
private double getTotalPrice(){
total = 0;
for(Food fd:foodList){
total += fd.getFoodPrice()*Food.getQuantity();
System.out.printf("Total:$%.2f ", total);
}
return total;
}
private void actionPerformedCancleOrder() {
int resp = JOptionPane.showConfirmDialog(myFrame, "Confirm Cancle Order \n\n Note : Selected food items will be discarded" , "Confirmation", JOptionPane.YES_NO_OPTION);
if(resp == JOptionPane.NO_OPTION){
}
else {
MasterPanel contentPane = new MasterPanel(myFrame);
myFrame.setContentPane(contentPane);
myFrame.setVisible(true);
}
}
private void setTableModel() {
FoodTableModel model = new FoodTableModel(foodList);
totalAmount = getTotalPrice();
s = String.valueOf(totalAmount);
txtPrice.setText(s);
tblList3.setModel(model);
tblList3.removeColumn(tblList3.getColumnModel().getColumn(0));
}
}
公共类摘要面板扩展主面板{
专用JTable表;
公共JTable tblList3;
受保护的ArrayListfoodList;
受保护的JComboBox类别_CB;
受保护的JComboBox cb_FoodItem;
私人JComboBox组合;
私有JTextField ImagePath_TF;
私人双总;
私有字符串总价格;
私人订单;
专用JComboBox组合框;
私人JComboBox cb;
私人JComboBox cb_表号;
私人双总金额;
私人ComboBoxModel cb_nopax1;
私人JComboBox cbNoPax;
私有字符串numPax;
私有字符串表号;
私人JLabel txtPrice;
私人双s;
/**
*创建面板。
*/
公共摘要面板(JFrame myFrame,ArrayList listSelected){
超级(myFrame);
this.foodList=listSelected;
挫折地面(颜色:白色);
设置前景(颜色.白色);;
JLabel lblConfirmOrder=新JLabel(“确认订单”);
lblConfirmOrder.setFont(新字体(“Tw-cent-MT”,Font.PLAIN,26));
lblConfirmOrder.setBounds(5210517349);
添加(lblConfirmOrder);
JScrollPane scrollPane=新的JScrollPane();
滚动窗格.立根(27342568204);
添加(滚动窗格);
tblList3=新的JTable();
setTableModel();
scrollPane.setViewportView(tblList3);
JButton btnHome=新JButton(“取消订单”);
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件arg0){
actionPerformedCancleOrder();
}
});
btnHome.setBounds(93554119,43);
添加(btnHome);
JLabel lblTableNo=新JLabel(“表号”);
lblTableNo.setBounds(337241,67,39);
添加(lblTableNo);
JButton btnOrder=新JButton(“订单”);
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件arg0){
actionPerformedOrder();
}
});
Btnoder.setBounds(297554151,43);
加上(Btnoder);
JLabel lblNumberOfPax=新JLabel(“人数”);
磅磅磅立根数(337174106,27);
加上(磅数);
/*JButton btnPrice=新JButton(“价格”);
btnPrice.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
actionPerformedTotalPrice();
}
});
btnPrice.setBounds(71176,89,23);
添加(btnPrice)*/
JLabel label=newjlabel(“”)label.setIcon(newImageIcon(SummaryPanel.class.getResource(“/fourFinger/
images/Wall-E6.jpg);
标签.立根(6363637253258);
添加(标签);
JLabel lblPleaseIndicateThe=新JLabel(“请注明”);
lblpleasei指示.setFont(新字体(“Tw-Cen-MT”,Font.BOLD,19));
LBLPleaseIndicate.setBounds(56117417355);
添加(lblpleaseIndicate);
JLabel lblTheSelectedFood=新JLabel(“数量”);
lblTheSelectedFood.setFont(新字体(“Tw-cent-MT”,Font.BOLD,19));
lblTheSelectedFood.setBounds(561209173,31);
添加(lblTheSelectedFood);
JLabel lblFoodSelected=新JLabel(“选定食品”);
lblFoodSelected.setFont(新字体(“Tw-cent-MT”,Font.BOLD,19));
lblFoodSelected.setBounds(5612511914);
添加(lblFoodSelected);
JLabel lbldsds=新JLabel(“”);
setIcon(新的ImageIcon(SummaryPanel.class.getResource(“/fourFinger/images/chatbox.png”));
LBLDSDS.立根(527151237176);
添加(LBLDSDS);
txtPrice=新的JLabel(“”);
txtPrice.setBounds(113,177,74,21);
添加(txtPrice);
cb_tableno=新JComboBox();
cb_表号立根(424250,93,20);
cb_tableno.setModel(新的DefaultComboxModel(新字符串[]{“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”);
增加(cb_表号);
cbNoPax=newjcombobox();
cbNoPax.挫折(424177,93,20);
setModel(新的DefaultComboxModel(新字符串[]{“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”);
添加(cbNoPax);
}
私有无效操作PerformedOrder(){
//检索用户输入
字符串numPax=(字符串)cbnomax.getSelectedItem();
String tableNo=(String)cb_tableNo.getSelectedItem();
日期orderDate=新日期();
orders=newOrders(Integer.parseInt(tableNo)、Integer.parseInt(numPax)、orderDate、totalAmount);
int orderID=OrdersDA.createOrders(订单);
}
私有双getTotalPrice(){
总数=0;
食品(食品部:食品清单){
总计+=fd.getFoodPrice()*Food.getQuantity();
System.out.printf(“总计:$%.2f”,总计);
}
返回总数;
}
私有无效操作PerformedCancleOrder(){
int resp=JOptionPane.showConfirmDialog(myFrame,“确认取消订单\n\n注意:所选食品将被丢弃”,“确认”,JOptionPane.YES\u NO\u选项);
if(resp==JOptionPane.NO_选项){
}
否则{
主面板内容窗格=新主面板(myFrame);
setContentPane(contentPane);
myFrame.setVisible(true);
}
}
私有void setTableModel(){
FoodTableModel=新的FoodTableModel(foodList);
totalAmount=getTotalPrice();
s=字符串.valueOf(合计金额);
txtPrice.setText(s);
tblList3.setModel(model);
tblList3.removeColumn(tblList3.getColumnModel().getColumn(0));
}
}
运行问题时,错误发生在:txtPrice.setText和声明的NullPointerexception。有人知道哪里出错了吗?您正在调用指令
txtPrice=new JLabel(“”)之前的方法setTableModel()
代码>。如果需要更精确的答案,请发布所有代码。您正在调用指令txtPrice=new JLabel(“”)之前的方法setTableModel()
代码>。如果您想要更精确的答案,请发布所有代码。如评论中所述,您在初始化之前使用的是txtPrice
。(请参见方法调用的顺序setTableModel
,与txtPrice=new JLabel(“”;
)进行比较,