2017-04-07 8 views
0

私は自動販売機にスロットが6つあり、それぞれに独自のソーダがある自動販売機を作っています。我々は、ソーダのブランドの列挙のarraylistを持っている必要があります。私はそれぞれのcanslotsのボタンを作成しましたが、適切なボタンにスロットを割り当てることができるだけでなく、空になるまでcanslotのボタンを1つクリックすると、arraylistの値を割り当てる方法もわかりません。ボタンにarraylistの値を割り当てる方法

実際の自動販売機のための私のコードはここにある:

public class VendingFrame extends JFrame { 
private ArrayList<CanSlot> CanSlots = new ArrayList<>(); 
public VendingFrame() { 
    CanSlots.add(new CanSlot(Brand.PEPSI)); 
    CanSlots.add(new CanSlot(Brand.COKE)); 
    CanSlots.add(new CanSlot(Brand.SUNKIST)); 
    CanSlots.add(new CanSlot(Brand.DIETPEPSI)); 
    CanSlots.add(new CanSlot(Brand.MTDEW)); 
    CanSlots.add(new CanSlot(Brand.SPRITE)); 
    JPanel panel = new JPanel(); 
    JButton button = new JButton("Pepsi"); 
    button.setPreferredSize(new Dimension(100, 80)); 
    JButton button2 = new JButton("Coke"); 
    button2.setPreferredSize(new Dimension(100, 80)); 
    JButton button3 = new JButton("Diest Pepsi"); 
    button3.setPreferredSize(new Dimension(100, 80)); 
    JButton button4 = new JButton("Sunkist"); 
    button4.setPreferredSize(new Dimension(100, 80)); 
    JButton button5 = new JButton("Mountain Dew"); 
    button5.setPreferredSize(new Dimension(100, 80)); 
    JButton button6 = new JButton("Sprite"); 
    button6.setPreferredSize(new Dimension(100, 80)); 
    JPanel picpanel = new JPanel(); 
    JPanel buttonPanel = new JPanel(); 
    JLabel label = new JLabel(); 
    setLayout(new BorderLayout()); 
    buttonPanel.setLayout(new GridLayout(6, 1)); 
    add(panel); 
    picpanel.add(label); 
    label.setIcon(new javax.swing.ImageIcon("C:\\Users\\iacol\\Desktop\\cans.jpg.jpg")); 
    buttonPanel.add(button); 
    button.addActionListener(new ClickListener()); 
    buttonPanel.add(button2); 
    button2.addActionListener(new ClickListener2()); 
    buttonPanel.add(button3); 
    button3.addActionListener(new ClickListener3()); 
    buttonPanel.add(button4); 
    button4.addActionListener(new ClickListener4()); 
    buttonPanel.add(button5); 
    button5.addActionListener(new ClickListener5()); 
    buttonPanel.add(button6); 
    button6.addActionListener(new ClickListener6()); 
    add(picpanel, BorderLayout.CENTER); 
    add(buttonPanel, BorderLayout.EAST); 
    setSize(700, 700); 
    setTitle("Vending Machine"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 
} 
public static void main(String[] args) { 
} 

答えて

0

あなたは、HashMap<Button, List<Product>>(); はIDにどのようにButtonオブジェクトを使用して作成し、値上のボタンのリストを取得することができます。

+0

これをどのように正確に書きますか?このコードは実際に何をしますか?私はちょっと混乱しています、私たちはこのタイプ/コードの前に –

関連する問題