Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 添加多个选择列表的值_Java_User Interface_Jlist - Fatal编程技术网

Java 添加多个选择列表的值

Java 添加多个选择列表的值,java,user-interface,jlist,Java,User Interface,Jlist,我试图添加所选项目的价格,然后将其返回到主类,在主类中,我将把它们添加到其他值中。然而,当我运行程序时,我得到了非常大的数字,与我所拥有的相比 到目前为止我一直到这里 import java.util.Random; import javax.swing.*; import java.util.Arrays; import java.text.DecimalFormat; import javax.swing.event.*; import java.awt.event.*; i

我试图添加所选项目的价格,然后将其返回到主类,在主类中,我将把它们添加到其他值中。然而,当我运行程序时,我得到了非常大的数字,与我所拥有的相比

到目前为止我一直到这里

import java.util.Random;
import javax.swing.*;
import java.util.Arrays;
import java.text.DecimalFormat;
import javax.swing.event.*;
import java.awt.event.*;        
import java.awt.*;

    public class OtherPrdctPanel extends JPanel implements ListSelectionListener
    {
       private JPanel otherPrdctPanel;
       private JList otherPrdctList;
       public int selectedOtherService;

       private String[] miscellaneousProd = {"Grip tape: $10",
                                             "Bearings: $30", "Riser pads: $2",
                                             "Nuts & bolts kit: $3"};
       private int[] miscellaneousProdPri = {10, 30, 2, 3};


       public OtherPrdctPanel()
       {
          setBorder(BorderFactory.createTitledBorder("Other Products"));

          otherPrdctList = new JList(miscellaneousProd);
          add(otherPrdctList);

          otherPrdctList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
          otherPrdctList.addListSelectionListener(this);

          setLayout(new GridLayout(3, 1));

       }

       public void valueChanged (ListSelectionEvent e)
       {
          int selection;
          selectedOtherService = 0;
          selection = (int)otherPrdctList.getSelectedIndex();

          for(int i = 0; i < 4; i++)
          {
             selectedOtherService = selectedOtherService + miscellaneousProdPri[selection];
          }


       }
    }
import java.util.Random;
导入javax.swing.*;
导入java.util.array;
导入java.text.DecimalFormat;
导入javax.swing.event.*;
导入java.awt.event.*;
导入java.awt.*;
公共类OtherPrdctPanel扩展JPanel实现ListSelectionListener
{
私人JPanel其他面板;
私有JList-otherPrdctList;
公共互联网选择其他服务;
私有字符串[]miscellaneousProd={“夹持带:$10”,
“轴承:$30”,“立管垫:$2”,
“螺母和螺栓套件:$3”};
私有int[]杂项USPRODPRI={10,30,2,3};
公共面板()
{
setBorder(BorderFactory.createTitledBorder(“其他产品”);
其他PRDCTLIST=新JList(杂项USPROD);
添加(其他PRDCT列表);
otherPrdctList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
otherPrdctList.addListSelectionListener(此);
setLayout(新的GridLayout(3,1));
}
public void值已更改(ListSelectionEvent e)
{
int选择;
选择其他服务=0;
selection=(int)otherPrdctList.getSelectedIndex();
对于(int i=0;i<4;i++)
{
selectedOtherService=selectedOtherService+杂项USPRODPRI[选择];
}
}
}
请帮忙


谢谢。

ListSelectionListener会触发多个事件,因为您需要取消选择一行,然后选择另一行。因此,您可能不希望每次选择更改时都计算总数


相反,您可能需要一个“计算总数”按钮。单击该按钮后,您可以使用JList API获取所有选定项目,然后计算总数。

ListSelectionListener会触发多个事件,因为您需要取消选择一行,然后选择另一行。因此,您可能不希望每次选择更改时都计算总数

public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

相反,您可能需要一个“计算总数”按钮。单击该按钮后,您可以使用JList API获取所有选定项目,然后计算总数。

ListSelectionListener会触发多个事件,因为您需要取消选择一行,然后选择另一行。因此,您可能不希望每次选择更改时都计算总数

public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

相反,您可能需要一个“计算总数”按钮。单击该按钮后,您可以使用JList API获取所有选定项目,然后计算总数。

ListSelectionListener会触发多个事件,因为您需要取消选择一行,然后选择另一行。因此,您可能不希望每次选择更改时都计算总数

public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

相反,您可能需要一个“计算总数”按钮。单击该按钮后,您将使用JList API获取所有选定项目,然后计算总数。

由于您使用的是
多间隔选择的选择模式,因此您必须考虑所有选择。考虑使用这样的方法来计算总数。< /P>
public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

然后你可以通过按下“计算”按钮来调用它。这种方法也不要求您实现
ListSelectionListener
,您可以删除
valueChanged
方法。

由于您使用的是
多间隔选择的选择模式,您必须考虑所有选择。考虑使用这样的方法来计算总数。< /P>
public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

然后你可以通过按下“计算”按钮来调用它。这种方法也不要求您实现
ListSelectionListener
,您可以删除
valueChanged
方法。

由于您使用的是
多间隔选择的选择模式,您必须考虑所有选择。考虑使用这样的方法来计算总数。< /P>
public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

然后你可以通过按下“计算”按钮来调用它。这种方法也不要求您实现
ListSelectionListener
,您可以删除
valueChanged
方法。

由于您使用的是
多间隔选择的选择模式,您必须考虑所有选择。考虑使用这样的方法来计算总数。< /P>
public int calculateTotalPrice() {
    int[] selections = otherPrdctList.getSelectedIndices();
    int total = 0;
    for (int i : selections) {
        total += miscellaneousProdPri[i];
    }
    return total;
}

然后你可以通过按下“计算”按钮来调用它。这种方法也不需要您实现
ListSelectionListener
,您可以删除
valueChanged
方法。

谢谢您的回答。但是,我有一个calculate(计算)按钮,它将添加来自其他JList(单选)的所有值。我的问题就是在这个类中,我试图通过使用Ctrl键同时选择多行来添加多个值。谢谢您的回答。但是,我有一个calculate(计算)按钮,它将添加来自其他JList(单选)的所有值。我的问题就是在这个类中,我试图通过使用Ctrl键同时选择多行来添加多个值。谢谢您的回答。但是,我有一个calculate(计算)按钮,它将添加来自其他JList(单选)的所有值。我的问题就是在这个类中,我试图通过使用Ctrl键同时选择多行来添加多个值。谢谢您的回答。但是,我有一个calculate(计算)按钮,它将添加来自其他JList(单选)的所有值。我的问题就是在这个类中,我试图通过使用Ctrl键同时选择多行来添加多个值。谢谢。非常感谢。@user3080461,这就是我建议的