2010-09-27 25 views
2

我的代碼選擇:需要設置,這取決於項目(S)的雙精度值,到目前爲止,從一個JList

import java.awt.BorderLayout; 
import java.awt.Container; 
import javax.swing.Box; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JScrollPane; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class TestFile2 { 

    public static void main(String args[]) { 
     String size[] = {"Small", "Medium", "Large", "Extra Large"}; 
     String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"}; 
     JFrame f = new JFrame("Pizza"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JList list1 = new JList(size); 
     JList list2 = new JList(toppings); 
     Container c = f.getContentPane(); 
     JScrollPane sp1 = new JScrollPane(list1); 
     sp1.setColumnHeaderView(new JLabel("Select Size")); 
     JScrollPane sp2 = new JScrollPane(list2); 
     sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings")); 
     Box box = Box.createHorizontalBox(); 
     box.add(sp1); 
     box.add(sp2); 
     list1.addListSelectionListener(new ListSelectionListener() { 

      public void valueChanged(ListSelectionEvent e) { 
       JList jListSource = (JList) e.getSource(); 
       Object[] selection = jListSource.getSelectedValues(); 
       if (!e.getValueIsAdjusting()) { 
        System.out.println("----"); 
        for (int i = 0; i < selection.length; i++) { 
         double costSize; 
         if (selection[i].equals("Small")) { 
          costSize = 7.00; 
         } else if (selection[i].equals("Medium")) { 
          costSize = 9.00; 
         } else if (selection[i].equals("Large")) { 
          costSize = 11.00; 
         } else { 
          costSize = 14.00; 
         } 
         System.out.println("selection = " + selection[i]); 
         System.out.println("selection = " + costSize); 
        } 
       } 
      } 
     }); 
     c.add(box, BorderLayout.CENTER); 
     f.setSize(
       300, 200); 
     f.setVisible(
       true); 
    } 
} 

我需要做類似於我list1的做了。

我需要讓用戶從JList中選擇比薩和澆頭的大小。最後,我需要能夠計算總成本。每個頂部是$ 1。任何方向上的任何一點都將不勝感激,因爲在過去的幾個小時裏,我一直拉着我的頭髮嘗試不同的方法解決這個問題。

在此先感謝。

+0

類似的東西?請提供更多細節。你堅持什麼觀點?什麼是預期的輸出? – 2010-09-27 05:55:15

回答

2

我希望這將有助於:) ...還有其他的方法來做到這....這只是其中之一...

import java.awt.BorderLayout; 
import java.awt.Container; 
import javax.swing.Box; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JScrollPane; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class TestFile2 { 

    private static JList list1; 
    private static JList list2; 

    public static void main(String args[]) { 
     String size[] = {"Small", "Medium", "Large", "Extra Large"}; 
     String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"}; 
     JFrame f = new JFrame("Pizza"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     list1 = new JList(size); 
     list2 = new JList(toppings); 
     Container c = f.getContentPane(); 
     JScrollPane sp1 = new JScrollPane(list1); 
     sp1.setColumnHeaderView(new JLabel("Select Size")); 
     JScrollPane sp2 = new JScrollPane(list2); 
     sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings")); 
     Box box = Box.createHorizontalBox(); 
     box.add(sp1); 
     box.add(sp2); 
     list1.addListSelectionListener(new ListSelectionListener() 
     { 

      public void valueChanged(ListSelectionEvent e) 
      { 
       handleEvent(e); 
      } 
     }); 
     list2.addListSelectionListener(new ListSelectionListener() 
     { 

      public void valueChanged(ListSelectionEvent e) 
      { 
       handleEvent(e); 
      } 
     }); 
     c.add(box, BorderLayout.CENTER); 
     f.setSize(
       300, 200); 
     f.setVisible(
       true); 
    } 

    protected static void handleEvent(ListSelectionEvent e) 
    { 

     double cost = 0.0; 
     Object[] selection = list1.getSelectedValues(); 
     Object[] toppings = list2.getSelectedValues(); 

     if(toppings.length == 0) 
      System.out.println("Please select a topping"); 

     if(selection.length == 0) 
      System.out.println("Please select a size"); 

     if (!e.getValueIsAdjusting()) 
     { 
      System.out.println("----"); 
      for (int i = 0; i < selection.length; i++) 
      { 
       double costSize; 
       if (selection[i].equals("Small")) { 
        cost = 7.00; 
       } else if (selection[i].equals("Medium")) { 
        cost = 9.00; 
       } else if (selection[i].equals("Large")) { 
        cost = 11.00; 
       } else { 
        cost = 14.00; 
       } 

      } 


      for (int i = 0; i < toppings.length; i++) 
      { 
       cost++; 
      } 


      System.out.println("Total = " + cost); 
     } 

    } 
}