swing - Java Example: Enter Key. Java Novice -


thank taking time read i'll make simple. problem have have program changes 1 type of temperature , converts another. thing want add program when person clicks button enter shows the new temperature in box when person clicks "enter" on keyboard. code below:

import javax.swing.*; import javax.swing.event.*;  import java.awt.*; import java.awt.event.*; import java.text.decimalformat; import java.util.inputmismatchexception;   public class testlab {      static string celciusstring = "celcius";     static string fahrenheitstring = "fahrenheit";     static string kelvinstring = "kelvin";      static string celcius1string = "celcius2";     static string fahrenheit1string = "fahrenheit2";     static string kelvin1string = "kelvin2";      private jlabel in = new jlabel("input scale");     private jlabel out = new jlabel("output scale");      private jlabel in1 = new jlabel("input");     private jlabel out1 = new jlabel("output");      private jtextfield input = new jtextfield(7);     private jtextfield output = new jtextfield(7);      private jframe f = new jframe("basicgui"); // create frame     private jpanel pnlnorth = new jpanel(); //north quadrant     private jpanel pnlsouth = new jpanel(); //south quadrant     private jpanel pnleast = new jpanel(); //east quadrant     private jpanel pnlwest = new jpanel(); // west quadrant     private jpanel pnlcenter = new jpanel(); // center quadrant       private jradiobutton celciusbutton = new jradiobutton(celciusstring);     private jradiobutton fahrenheitbutton = new jradiobutton(fahrenheitstring);     private jradiobutton kelvinbutton = new jradiobutton(kelvinstring);      private jradiobutton celcius1button = new jradiobutton(celciusstring);     private jradiobutton fahrenheit1button = new jradiobutton(fahrenheitstring);     private jradiobutton kelvin1button = new jradiobutton(kelvinstring);      //menu     private jmenubar mb = new jmenubar(); // menu bar     private jmenu mnufile = new jmenu("file"); // file entry of menu bar     private jmenuitem mnuitemquit = new jmenuitem("quit"); // quit sub item     private jmenu mnuhelp = new jmenu("help"); //help menu entry     private jmenuitem mnuitemabout = new jmenuitem("about"); //about entry      //constructor gui     public testlab(){         //set menubar         pnleast.setlayout(new gridlayout (4,1));         pnlwest.setlayout(new gridlayout (4,1));          f.setjmenubar(mb);          //radio button --new         celciusbutton.setmnemonic(keyevent.vk_c);         celciusbutton.setactioncommand(celciusstring);         celciusbutton.setselected(true);          fahrenheitbutton.setmnemonic(keyevent.vk_f);         fahrenheitbutton.setactioncommand(fahrenheitstring);          kelvinbutton.setmnemonic(keyevent.vk_k);         kelvinbutton.setactioncommand(kelvinstring);          celcius1button.setmnemonic(keyevent.vk_c);         celcius1button.setactioncommand(celcius1string);         celcius1button.setselected(true);          fahrenheit1button.setmnemonic(keyevent.vk_f);         fahrenheit1button.setactioncommand(fahrenheit1string);          kelvin1button.setmnemonic(keyevent.vk_k);         kelvin1button.setactioncommand(kelvin1string);          //group radio buttons.          buttongroup group = new buttongroup();         group.add(celciusbutton);         group.add(fahrenheitbutton);         group.add(kelvinbutton);          buttongroup group2 = new buttongroup();         group2.add(celcius1button);         group2.add(fahrenheit1button);         group2.add(kelvin1button);           //register listener radio buttons.         celciusbutton.addactionlistener(new radiobuttonevent());         fahrenheitbutton.addactionlistener(new radiobuttonevent());         kelvinbutton.addactionlistener(new radiobuttonevent());          celcius1button.addactionlistener(new radiobuttonevent());         fahrenheit1button.addactionlistener(new radiobuttonevent());         kelvin1button.addactionlistener(new radiobuttonevent());           //build menus         mnufile.add(mnuitemquit); //create quit line         mnuhelp.add(mnuitemabout); //create line         mb.add(mnufile);        // add menu items form         mb.add(mnuhelp);          //add buttons         //pnlnorth.add(btnnorth);          pnlnorth.add(in1);         pnlsouth.add(out1);         pnlnorth.add(input);         pnlsouth.add(output);          pnlwest.add(in);         pnlwest.add(celciusbutton);         pnlwest.add(fahrenheitbutton);         pnlwest.add(kelvinbutton);          pnleast.add(out);         pnleast.add(celcius1button);         pnleast.add(fahrenheit1button);         pnleast.add(kelvin1button);           //setup main frame         f.getcontentpane().setlayout(new borderlayout());         f.getcontentpane().add(pnlnorth, borderlayout.north);         f.getcontentpane().add(pnlsouth,borderlayout.south );         f.getcontentpane().add(pnleast,borderlayout.east );         f.getcontentpane().add(pnlwest,borderlayout.west );         f.getcontentpane().add(pnlcenter,borderlayout.center);          //allows swing app closed         f.addwindowlistener(new listenclosewdw());          // add menu listener         mnuitemquit.addactionlistener(new listenmenuquit());     }      public class listenmenuquit implements actionlistener{         public void actionperformed(actionevent e){             system.exit(0);         }         }        //new stuff add     public class radiobuttonevent implements actionlistener{         public void actionperformed(actionevent e){             string action = e.getactioncommand();              int temperture;             double newtemperture = 0.00;             try {             temperture = integer.parseint(input.gettext());          if (celciusbutton.isselected() & celcius1button.isselected())               newtemperture = temperture;           else if (celciusbutton.isselected() & fahrenheit1button.isselected())              newtemperture = ((temperture * 1.8)) + 32;              else if (celciusbutton.isselected() & kelvin1button.isselected())                  newtemperture = temperture + 273.15;              else if (fahrenheitbutton.isselected() & celcius1button.isselected())                  newtemperture = (temperture - 32) * .55555555555;              else if (fahrenheitbutton.isselected() & fahrenheit1button.isselected())                  newtemperture = temperture;              else if (fahrenheitbutton.isselected() & kelvin1button.isselected())                  newtemperture = (temperture + 459.67) * 5/9;              else if (kelvinbutton.isselected() & celcius1button.isselected())                  newtemperture = (temperture - 273.15);              else if (kelvinbutton.isselected() & fahrenheit1button.isselected())                  newtemperture = (temperture * 9/5) - 459.67;              else if (kelvinbutton.isselected() & kelvin1button.isselected())                  newtemperture = temperture;               } catch (exception a) {                 joptionpane.showmessagedialog(null, "you did not input value");             }             output.settext(new decimalformat("##.##").format(newtemperture));             }     }        public class listenclosewdw extends windowadapter{         public void windowclosing(windowevent e){             system.exit(0);          }     }      public void launchframe(){         //display frame         f.setdefaultcloseoperation(jframe.exit_on_close);         f.pack(); // adjusting panel components display         f.setvisible(true);     }     public static void main(string args[]){          testlab gui = new testlab();         gui.launchframe();     } } 

just add testlab():

input.addactionlistener(new radiobuttonevent()); 

also don't create identical actionlisteners every component. can create 1 , register number of components.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -