2017-04-06 18 views
0

JComboBoxでいくつかの項目を選択して選択すると問題が発生します。アイテムが選択されると、ユーザーは選択されたアイテムの詳細情報を入力できる新しい関連フォームを表示したい(選択ごとに1つのフォームのみ)。私のプログラムでは、間違ったフォームが現れたり、いくつかのフォームが間違った順序で表示されたりします。例えば、私がプログラムを実行し、 "Smycke"(ジュエリー、私のコードの一部がスウェーデン語である)を選択すると、 "Aktie"(Stock)のような別のオプションを選択するまで何も起こらず、 "Smycke" "Aktie"形式で入力します。正しいフォームを表示させるにはどうすればよいですか?私のコードのJComboBox:選択後に間違ったフォームが表示される

パーツ:あなたのBoxLisののリスナーの代わりに、適切な形を示すには

public class Program extends JFrame { 

    private ArrayList<Valueables> allValueables = new ArrayList<>(); 

    private JTextArea display; 
    private JRadioButton sortName; 
    private JRadioButton sortValue; 

    private String[] valueables = { "Smycke", "Aktie", "Apparat" }; 
    private JComboBox<String> box = new JComboBox<String>(valueables); 


    Program() { 
     super("Sakregister"); 
     JPanel top = new JPanel(); 
     top.add(new JLabel("Värdesaker")); 
     add(top, BorderLayout.NORTH); 

     JPanel left = new JPanel(); 
     add(left, BorderLayout.WEST); 

     JPanel right = new JPanel(); 
     right.add(new JLabel("Sortering")); 
     sortName = new JRadioButton("Namn", true); 
     right.add(sortName); 
     sortValue = new JRadioButton("Värde"); 
     right.add(sortValue); 
     ButtonGroup groupb = new ButtonGroup(); 
     groupb.add(sortName); 
     groupb.add(sortValue); 
     add(right, BorderLayout.EAST); 
     right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS)); 

     JPanel bottom = new JPanel(); 
     bottom.add(new JLabel("Nytt:")); 

     bottom.add(box); 
     box.addActionListener(new BoxLis()); 
     add(bottom, BorderLayout.SOUTH); 

     display = new JTextArea(); 
     JScrollPane scroll = new JScrollPane(display); 
     display.setEditable(false); 
     display.setWrapStyleWord(true); 
     add(scroll, BorderLayout.CENTER); 

     setSize(600, 500); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    class BoxLis implements ActionListener { 
     public void actionPerformed(ActionEvent ave) { 

      if (box.getSelectedIndex() == 0) { 
       box.addActionListener(new JewelryLis()); 
      } else if (box.getSelectedIndex() == 1) { 
       box.addActionListener(new StockLis()); 
      } else if (box.getSelectedIndex() == 2) { 
       box.addActionListener(new DeviceLis()); 
      } 
     } 
    } 

    class JewelryLis implements ActionListener { 
     public void actionPerformed(ActionEvent ave) { 

      JewelryForm f = new JewelryForm(); 
      try { 
       int svar = JOptionPane.showConfirmDialog(Program.this, f, "Nytt smycke", JOptionPane.OK_CANCEL_OPTION); 
       if (svar != JOptionPane.OK_OPTION) 
        return; 

       String name = f.getName(); 
       int stones = f.getStones(); 
       boolean isGold = f.getGold(); 

       addJewelry(name, stones, isGold); 

      } catch (NumberFormatException e) { 
       JOptionPane.showMessageDialog(Program.this, "Fel inmatning!"); 
      } 
     } 
    } 

    class StockLis implements ActionListener { 
     public void actionPerformed(ActionEvent ave) { 

      StockForm f = new StockForm(); 
      try { 
       int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny aktie", JOptionPane.OK_CANCEL_OPTION); 
       if (svar != JOptionPane.OK_OPTION) 
        return; 

       String name = f.getName(); 
       int amount = f.getAmount(); 
       int stockPrice = f.getStockPrice(); 

       addStock(name, amount, stockPrice); 

      } catch (NumberFormatException e) { 
       JOptionPane.showMessageDialog(Program.this, "Fel inmatning!"); 
      } 
     } 

    } 

    class DeviceLis implements ActionListener { 
     public void actionPerformed(ActionEvent ave) { 

      DeviceForm f = new DeviceForm(); 
      try { 
       int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny apparat", JOptionPane.OK_CANCEL_OPTION); 
       if (svar != JOptionPane.OK_OPTION) 
        return; 

       String name = f.getName(); 
       int purchasePrice = f.getPurchasePrice(); 
       int wear = f.getWear(); 

       addDevice(name, purchasePrice, wear); 

      } catch (NumberFormatException e) { 
       JOptionPane.showMessageDialog(Program.this, "Fel!"); 
      } 
     } 

    } 

答えて

3

あなたのコンボボックスにactionlistnereを追加しています。

コードを変更すると、問題が解決されます。

box.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (box.getSelectedIndex() == 0) { 
       showJewlryForm(); 
      } else if (box.getSelectedIndex() == 1) { 
       showStockForm(); 
      } else if (box.getSelectedIndex() == 2) { 
       showDeviceForm(); 
      } 

     } 
    });  

    private void showJewlryForm() { 
     JewelryForm f = new JewelryForm(); 
     try { 
      int svar = JOptionPane.showConfirmDialog(Program.this, f, "Nytt smycke", JOptionPane.OK_CANCEL_OPTION); 
      if (svar != JOptionPane.OK_OPTION) 
       return; 

      String name = f.getName(); 
      int stones = f.getStones(); 
      boolean isGold = f.getGold(); 

      addJewelry(name, stones, isGold); 

     } catch (NumberFormatException e) { 
      JOptionPane.showMessageDialog(Program.this, "Fel inmatning!"); 
     } 
    } 

    private void showStockForm() { 

     StockForm f = new StockForm(); 
     try { 
      int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny aktie", JOptionPane.OK_CANCEL_OPTION); 
      if (svar != JOptionPane.OK_OPTION) 
       return; 

      String name = f.getName(); 
      int amount = f.getAmount(); 
      int stockPrice = f.getStockPrice(); 

      addStock(name, amount, stockPrice); 

     } catch (NumberFormatException e) { 
      JOptionPane.showMessageDialog(Program.this, "Fel inmatning!"); 
     } 

    } 

    private void showDeviceForm() { 

     DeviceForm f = new DeviceForm(); 
     try { 
      int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny apparat", JOptionPane.OK_CANCEL_OPTION); 
      if (svar != JOptionPane.OK_OPTION) 
       return; 

      String name = f.getName(); 
      int purchasePrice = f.getPurchasePrice(); 
      int wear = f.getWear(); 

      addDevice(name, purchasePrice, wear); 

     } catch (NumberFormatException e) { 
      JOptionPane.showMessageDialog(Program.this, "Fel!"); 
     } 

    } 
関連する問題