2017-02-22 1 views
0

Je travaille actuellement sur un programme utilisant JSwing.JSwing: Action Effectué en créant une nouvelle fenêtre.

J'ai le programme qui fonctionne avec la bonne logique, mais pour une raison quelconque, lorsque j'entre ma première entrée, il crée une nouvelle fenêtre avec les changements corrects.

Il fait seulement une nouvelle fenêtre une fois.

Ci-dessous mon code pour mon convertisseur

public class Converter extends JFrame{ 
     //Creating text fields to be put into the "Distance" Tab 
     JTextField metersTxt = new JTextField(), feetTxt = new JTextField(),milesTxt = new JTextField(), 
     inchesTxt = new JTextField(), cmTxt = new JTextField(), kmTxt = new JTextField(); 




public Converter(){ 
       //Creating the GUI object for frame 
       JFrame frame = new JFrame("Distance/Energy Converter"); 
       frame.setSize(600, 600); 
       frame.setVisible(true); 

       //Assigning a name to JTextFile 
       metersTxt.setName("meters"); 
       feetTxt.setName("feet"); 
       milesTxt.setName("miles"); 
       inchesTxt.setName("inches"); 
       cmTxt.setName("cm"); 
       kmTxt.setName("km"); 

       //Making an instance of a tabbedPane to be able to add 2 panes. 
       JTabbedPane tabbedPane = new JTabbedPane(); 

       //Making the first tab to convert distance values 
       JPanel panel1 = new JPanel(true); 
       tabbedPane.addTab("Distance", panel1); 
       panel1.setLayout(new GridLayout(0,6)); 

       //A second tab to switch to energy conversions 
       JPanel panel2 = new JPanel(true); 
       tabbedPane.addTab("Energy", panel2); 
       panel2.setLayout(new GridLayout(0,6)); 

       //Add the tabbedPane to this panel. 
       frame.add(tabbedPane); 

       //Creates labels for the "Distance" tab 
       JLabel meters = new JLabel("Meters"); 
       JLabel feet = new JLabel("Feet"); 
       JLabel miles = new JLabel("Miles");   
       JLabel inches = new JLabel("Inches");   
       JLabel cm = new JLabel("Centimeters");   
       JLabel km = new JLabel ("Kilometers"); 

       //Create labels for the "Energy" tab 
       JLabel joule = new JLabel("Joule"); 
       JLabel cal = new JLabel("Calorie"); 
       JLabel volt = new JLabel("Volt"); 
       JLabel watt = new JLabel("Watt"); 
       JLabel thermal = new JLabel("Thermal Units"); 

       //Adding labels and text boxes to distance panel in order to be formatted right 
       panel1.add(meters); 
       panel1.add(metersTxt); 
       panel1.add(feet); 
       panel1.add(feetTxt); 
       panel1.add(miles); 
       panel1.add(milesTxt); 
       panel1.add(inches); 
       panel1.add(inchesTxt); 
       panel1.add(cm); 
       panel1.add(cmTxt); 
       panel1.add(km); 
       panel1.add(kmTxt); 

       //Allowing the user to edit the text boxes 
       metersTxt.setEditable(true); 
       feetTxt.setEditable(true); 
       milesTxt.setEditable(true); 
       inchesTxt.setEditable(true); 
       cmTxt.setEditable(true); 
       kmTxt.setEditable(true); 

       //Attaching the metric listener to each JTextField 
       metersTxt.addActionListener(new MetricListener()); 
       feetTxt.addActionListener(new MetricListener()); 
       milesTxt.addActionListener(new MetricListener()); 
       inchesTxt.addActionListener(new MetricListener()); 
       cmTxt.addActionListener(new MetricListener()); 
       kmTxt.addActionListener(new MetricListener()); 

       //To clean up interface and close it when close the window 
       frame.pack(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     } 


     public static void main(String args[]){ 
      Converter mf = new Converter(); 
     } 



} 

Et puis ma classe d'auditeur

public class MetricListener implements ActionListener 
{ 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     Converter c = null; 
     c = new Converter(); 

     //Getting where the action takes place 
     JTextField txt = new JTextField(); 
     txt = (JTextField) e.getSource(); 

     //Extracting input from GUI 
     String input = null; 
     input = e.getActionCommand(); 

     //Turing the taken in value into type double 
     double value = 0; 
     value = Double.parseDouble(input); 

     //Variables to hold values for conversions 
     double meters = 0, feet = 0, miles = 0, inches = 0, cm = 0, km = 0; 

     String txtName = null; 
     txtName = txt.getName(); 

     //The following if statements are seeing which JTextField is being changed 
     if(txtName.equals("meters")){ 

      //Creating conversions from meters 
      meters = value; 
      feet = value * .3048; 
      miles = value * 0.000621371; 
      inches = value * 39.3701; 
      cm = value * 100; 
      km = value * .001; 

      //Applying the converted values to respected places 
      c.metersTxt.setText(Double.toString(meters)); 
      c.feetTxt.setText(Double.toString(feet)); 
      c.milesTxt.setText(Double.toString(miles)); 
      c.inchesTxt.setText(Double.toString(inches)); 
      c.cmTxt.setText(Double.toString(cm)); 
      c.kmTxt.setText(Double.toString(km)); 


     } 

     else if(txtName.equals("feet")){ 
      //Creating conversions from feet 
      meters = value * .3048; 
      feet = value ; 
      miles = value * 0.000189394; 
      inches = value * 12; 
      cm = value * 30.48; 
      km = value * 0.0003048; 

      //Applying the converted values to respected places 
      c.metersTxt.setText(Double.toString(meters)); 
      c.feetTxt.setText(Double.toString(feet)); 
      c.milesTxt.setText(Double.toString(miles)); 
      c.inchesTxt.setText(Double.toString(inches)); 
      c.cmTxt.setText(Double.toString(cm)); 
      c.kmTxt.setText(Double.toString(km)); 
     } 
     else if(txtName.equals("miles")){ 
      //Creating conversions from feet 
      meters = value * 1609.34; 
      feet = value * 5280; 
      miles = value; 
      inches = value * 63360; 
      cm = value * 160934; 
      km = value * 1.60934; 

      //Applying the converted values to respected places 
      c.metersTxt.setText(Double.toString(meters)); 
      c.feetTxt.setText(Double.toString(feet)); 
      c.milesTxt.setText(Double.toString(miles)); 
      c.inchesTxt.setText(Double.toString(inches)); 
      c.cmTxt.setText(Double.toString(cm)); 
      c.kmTxt.setText(Double.toString(km)); 
     } 
     else if(txtName.equals("inches")){ 
      //Creating conversions from feet 
      meters = value * 0.0254; 
      feet = value * 0.0833333; 
      miles = value * .0000157828; 
      inches = value; 
      cm = value * 2.54; 
      km = value * .0000254; 

      //Applying the converted values to respected places 
      c.metersTxt.setText(Double.toString(meters)); 
      c.feetTxt.setText(Double.toString(feet)); 
      c.milesTxt.setText(Double.toString(miles)); 
      c.inchesTxt.setText(Double.toString(inches)); 
      c.cmTxt.setText(Double.toString(cm)); 
      c.kmTxt.setText(Double.toString(km)); 
     } 
     else if(txtName.equals("cm")){ 
      //Creating conversions from feet 
      meters = value * 0.01; 
      feet = value * 0.0328084; 
      miles = value * .00000621371; 
      inches = value * 0.393701; 
      cm = value; 
      km = value * .00001; 

      //Applying the converted values to respected places 
      c.metersTxt.setText(Double.toString(meters)); 
      c.feetTxt.setText(Double.toString(feet)); 
      c.milesTxt.setText(Double.toString(miles)); 
      c.inchesTxt.setText(Double.toString(inches)); 
      c.cmTxt.setText(Double.toString(cm)); 
      c.kmTxt.setText(Double.toString(km)); 
     } 
     else if(txtName.equals("km")){ 
      //Creating conversions from feet 
      meters = value * 1000f; 
      feet = value * 3280.84; 
      miles = value * 0.621371; 
      inches = value * 39370.1; 
      cm = value * 100000; 
      km = value; 

      //Applying the converted values to respected places 
      c.metersTxt.setText(Double.toString(meters)); 
      c.feetTxt.setText(Double.toString(feet)); 
      c.milesTxt.setText(Double.toString(miles)); 
      c.inchesTxt.setText(Double.toString(inches)); 
      c.cmTxt.setText(Double.toString(cm)); 
      c.kmTxt.setText(Double.toString(km)); 
     } 

    } 

} 

Toute aide serait appréciée! Je vous remercie!

Répondre

1

Votre premier problème est la création d'une nouvelle JFrame dans Converter(). Le convertisseur est déjà un JFrame donc vous pouvez utiliser "this" au lieu de "frame".

Je pense que l'ouverture d'une nouvelle fenêtre n'est pas prévue, donc ne créez pas un nouveau convertisseur en actionPerformed. Alors donnez plutôt une référence de votre convertisseur à MetricListener. Si vous faites de MetricListener une classe interne de Converter, c'est encore plus facile.

J'essaie de montrer les points importants ici:

public class Converter extends JFrame{ 
    //Creating text fields to be put into the "Distance" Tab 
    JTextField metersTxt = new JTextField(), feetTxt = new JTextField(),milesTxt = new JTextField(), inchesTxt = new JTextField(), cmTxt = new JTextField(), kmTxt = new JTextField(); 

    //Creating the GUI object for frame 
    public Converter() { 
     this.setTitle("Distance/Energy Converter"); 
     this.setSize(600, 600); 
     this.setVisible(true); 

... 

     //Add the tabbedPane to this panel. 
     this.add(tabbedPane); 

... 

     //To clean up interface and close it when close the window 
     this.pack(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    public class MetricListener implements ActionListener 
{ 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
       /* DO NOT CREATE A NEW Converter */ 

       ... 

       //The following if statements are seeing which JTextField is being changed 
     if(txtName.equals("meters")){ 

         .... 

         /* Since it is an inner class you can use these variables directly */ 

      //Applying the converted values to respected places 
      metersTxt.setText(Double.toString(meters)); 
      feetTxt.setText(Double.toString(feet)); 
      milesTxt.setText(Double.toString(miles)); 
      inchesTxt.setText(Double.toString(inches)); 
      cmTxt.setText(Double.toString(cm)); 
      kmTxt.setText(Double.toString(km)); 


     } 

     else if(txtName.equals("feet")){ 
        /* and so on */ 
     } 

    } 

    } 

    public static void main(String args[]){ 
     Converter mf = new Converter(); 
    } 

} 
1

votre programme crée un nouveau cadre à chaque fois, parce que dans la méthode actionPerformed de MetricListener, vous appelez chaque fois

c = new Converter(); 

qui crée et affiche un nouveau JFrame chaque fois.

La solution est d'éviter l'instanciation d'un nouveau convertisseur à chaque fois, et ont comme champ local, par exemple comme suit

MetricListener

public class MetricListener implements ActionListener 
{ 
     //add field c of class Converter 
    private Converter c; 

     //create COntructor with Converter parameter 
    public MetricListener(Converter c){ 
     this.c=c; 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 

     //Comment converte creation 
     //Converter c = null; 
     //c = new Converter(); 
..... 
//All the rest of the Class as before 

Converter

public Converter(){ 
.... 

//Attaching the metric listener to each JTextField 
//you pass this to the MetricListener 
      metersTxt.addActionListener(new MetricListener(this)); 
      feetTxt.addActionListener(new MetricListener(this)); 
      milesTxt.addActionListener(new MetricListener(this)); 
      inchesTxt.addActionListener(new MetricListener(this)); 
      cmTxt.addActionListener(new MetricListener(this)); 
      kmTxt.addActionListener(new MetricListener(this));