2017-05-16 4 views
0

Sagen wir, der Benutzer wählte Tankstellen. Der Rahmen ändert sich und der Benutzer kann einen Standort auswählen, und die Liste der Tankstellen an diesem Standort wird unterhalb der Combobox angezeigt.JComboBox-Aktion wird nicht ausgeführt

Also habe ich versucht, einen anderen Action-Listener innerhalb der if-Anweisung, mit einem anderen wenn sonst, und ich habe auch switch-Anweisungen versucht, aber beide zeigen nicht die Ausgabe. Also, wie löse ich das? Danke im Voraus.

Dies ist die gekürzte Version meines Codes vor der zweiten Aktion Hörer Zugabe

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Testing5 { 
private JFrame frame1, frame2; 
private ActionListener action, action2; 
private JButton PetrolStations, back, Foods; 
private JComboBox locationChooser; 
final static String[] location = {"Petaling Jaya", "Port Klang", "Kuala Lumpur"}; 

public void HELPMEGUI() { 

    frame1 = new JFrame("Frame 1"); 
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

    JPanel contentPanel = new JPanel(new GridLayout(5,2)); 

    PetrolStations = new JButton ("PetrolStations"); 
    Foods = new JButton ("Foods");  
    back = new JButton ("Back"); 

    locationChooser = new JComboBox(location); 

    action = new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      JButton button = (JButton) ae.getSource();   

      if (button == PetrolStations) { 
       frame2 = new JFrame("FRAME 2"); 
       frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       frame2.add(locationChooser, BorderLayout.NORTH); 
       frame2.add(back, BorderLayout.SOUTH); 

       frame2.setSize(300, 300); 
       frame2.setLocationRelativeTo(null); 
       frame2.setVisible(true); 
       frame1.setVisible(false); 

      } 

      else if (button == Foods) { 
       frame2 = new JFrame("FRAME 2"); 
       frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       frame2.add(locationChooser, BorderLayout.NORTH); 
       frame2.add(back, BorderLayout.SOUTH); 

       frame2.setSize(300, 300); 
       frame2.setLocationRelativeTo(null); 
       frame2.setVisible(true); 
       frame1.setVisible(false); 

      } 

      else if (button == back) { 
       frame1.setVisible(true); 
       frame2.setVisible(false); 
       frame2.dispose(); 
      } 
     } 
    }; 

    PetrolStations.addActionListener(action); 
    Foods.addActionListener(action); 
    back.addActionListener(action); 
    locationChooser.addActionListener(action2); 

    contentPanel.add(PetrolStations); 
    contentPanel.add(Foods); 

    frame1.getContentPane().add(contentPanel); 
    frame1.setSize(640, 400); 
    frame1.setVisible(true); 
    frame1.setLocationRelativeTo(null); 

} 

public static void main(String...args) { 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      new Testing5().HELPMEGUI(); 
     } 
    }); 
} 

} 

EDIT:

Dies ist, was ich

action2 = new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         JComboBox locationSelected = (JComboBox) e.getSource(); 

         if (locationSelected == Kuala Lumpur) { 
          System.out.println("Address 1"); 
         } 
        } 
       }; 
versucht zu tun

2n versuchen d

action2 = new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
        int temp; 

        if(e.getSource() == locationChooser) { 
         temp = locationChooser.getSelectedIndex(); 

         switch (temp) { 
         case 0: System.out.println("Address 1"); break; 
         case 1: System.out.println("Address 2"); break; 
         } 
        } 
        } 
        };} 
+1

'action2' nie initialisiert wird (es ist' null') – MadProgrammer

+0

@MadProgrammer Dies ist der Code, bevor ich die neue Aktion – Farhan

+1

Nun hinzugefügt, das war unhelp – MadProgrammer

Antwort

0

Sie zeigen nicht den Kontext Ihrer Aktion Hörer Definition, so dass ich glaube, Sie es an der falschen Stelle haben könnten. Hier ist, wie ich es tat, und es scheint zu funktionieren:

PetrolStations.addActionListener(action); 
    Foods.addActionListener(action); 
    back.addActionListener(action); 
    action2 = new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 
      int temp; 

      if (e.getSource() == locationChooser) { 
       temp = locationChooser.getSelectedIndex(); 

       switch (temp) { 
        case 0: 
         System.out.println("Address 1"); 
         break; 
        case 1: 
         System.out.println("Address 2"); 
         break; 
       } 
      } 
     } 
    }; 
    locationChooser.addActionListener(action2); 
+0

Es tut mir leid, wenn ich dies nicht zu der Frage gesagt habe, werde ich bearbeiten. Aber dieser Code ist der Code, bevor ich den 2. Aktion Listener innerhalb der 1. Aktion Listener – Farhan

+0

Zeig mir, dass 2. Aktion Hörer und ich werde sehen, ob ich eine bessere Antwort für Sie habe ... –

+0

Ich habe zwei Sätze von Codes hinzugefügt was ich versucht habe – Farhan