Please check my code.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please check my code.
Next time please post your code and surround it in [highlight=java][/highlight] tags. What problem are you experiencing, are there any errors?
For anyone that is too lazy to open the link:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; public class unitConversion { public static void main (String[] args) { JFrameWithPanel panel = new JFrameWithPanel(); panel.setSize(305,250); panel.setVisible(true); panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class JFrameWithPanel extends JFrame implements ActionListener{ JLabel lbl = new JLabel("Enter choice (i.e. One, Two, Three, or Four)"); JLabel lbl2 = new JLabel("Enter value to convert:"); JTextField txt = new JTextField(); JTextField txt2 = new JTextField(); JLabel lbl3 = new JLabel(); JButton btn = new JButton("CLEAR"); JButton btn2 = new JButton("CONVERT"); JButton btnok = new JButton("OK"); JButton btncan = new JButton("CANCEL"); public static String u; public static double i, mn; public JFrameWithPanel(){ setTitle("Unit Conversion"); setLayout(null); txt.setBounds(10,48,120,31); lbl.setBounds(10,23,300,20); txt2.setBounds(160,92,110,30); lbl2.setBounds(10,96,130,20); lbl3.setBounds(40,120,300,30); lbl3.setHorizontalAlignment( SwingConstants.CENTER ); btnok.setBounds(135,48,60,30); btnok.addActionListener(this); btnok.setActionCommand("h"); btncan.setBounds(200,48,80,30); btncan.addActionListener(this); btncan.setActionCommand("r"); btn.setBounds(40,160,80,30); btn.addActionListener(this); btn.setActionCommand("g"); btn2.setBounds(132,160,120,30); btn2.addActionListener(this); btn2.setActionCommand("x"); add(btn); add(btn2); add(btnok); add(btncan); add(lbl); add(txt); add(lbl2); add(txt2); add(lbl3); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("h")){ u = txt.getText(); if(u.equalsIgnoreCase("one")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i/1000; lbl3.setText(""+i+" meter/s is equivalent to "+mn+" kilometer/s."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("two")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i*0.0003048; lbl3.setText(""+i+" foot/feet is equivalent to "+mn+" kilometer/s."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("three")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i*(-457.87); lbl3.setText(""+i+" kelvin is equivalent to "+mn+" fahrenheit."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("four")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i*33.8; lbl3.setText(""+i+" celsius is equivalent to "+mn+" fahrenheit."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("")){ txt.setText("default: one"); lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i/1000; lbl3.setText(""+i+" meter/s is equivalent to "+mn+" kilometer/s."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else{ txt.setText("Enter another choice."); }if(e.getActionCommand().equals("r")){ txt.setText(""); } } } }
Last edited by Brt93yoda; September 2nd, 2010 at 02:03 PM.
khms (September 2nd, 2010)
What is the correct input for the "convert value" textField?
the input can be any number. the program is a unit converter. if you type 'one', that will enable you to convert meters to kilometers, typing 'two' will make you convert feet to kilometers, 'three' for kelvin to fahrenheit and 'four' for celsius to fahrenheit. if you choose one, and placed 10 as the convert value, that will be .01 kilometers.
but i can't seem to make the buttons to work. the output/answer is supposed to appear to lbl3 which is located between the convert textfield and the clear and convert buttons. i've been trying to figure out the problem with my code, but i can't seem to find any.
Your code tries to capture those events in the action listener implementation within another caught event, which means they will never be seen. Add another else if along with the other 'outer' ones to implement the actions of those buttons.
I found your problem. On the beginning of your action listener you have
Which meant that the action performed had to magically be H AND X. Here is my fixed code (works perfectly fine for me):if(e.getActionCommand().equals("h")){
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; public class unitConversion { public static void main (String[] args) { JFrameWithPanel panel = new JFrameWithPanel(); panel.setSize(305,250); panel.setVisible(true); panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class JFrameWithPanel extends JFrame implements ActionListener{ JLabel lbl = new JLabel("Enter choice (i.e. One, Two, Three, or Four)"); JLabel lbl2 = new JLabel("Enter value to convert:"); JTextField txt = new JTextField(); JTextField txt2 = new JTextField(); JLabel lbl3 = new JLabel(); JButton btn = new JButton("CLEAR"); JButton btn2 = new JButton("CONVERT"); JButton btnok = new JButton("OK"); JButton btncan = new JButton("CANCEL"); public static String u; public static double i, mn; public JFrameWithPanel(){ setTitle("Unit Conversion"); setLayout(null); txt.setBounds(10,48,120,31); lbl.setBounds(10,23,300,20); txt2.setBounds(160,92,110,30); lbl2.setBounds(10,96,130,20); lbl3.setBounds(40,120,300,30); lbl3.setHorizontalAlignment( SwingConstants.CENTER ); btnok.setBounds(135,48,60,30); btnok.addActionListener(this); btnok.setActionCommand("h"); btncan.setBounds(200,48,80,30); btncan.addActionListener(this); btncan.setActionCommand("r"); btn.setBounds(40,160,80,30); btn.addActionListener(this); btn.setActionCommand("g"); btn2.setBounds(132,160,120,30); btn2.addActionListener(this); btn2.setActionCommand("x"); add(btn); add(btn2); add(btnok); add(btncan); add(lbl); add(txt); add(lbl2); add(txt2); add(lbl3); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } public void actionPerformed(ActionEvent e){ u = txt.getText(); if(u.equalsIgnoreCase("one")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i/1000; lbl3.setText(""+i+" meter/s is equivalent to "+mn+" kilometer/s."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("two")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i*0.0003048; lbl3.setText(""+i+" foot/feet is equivalent to "+mn+" kilometer/s."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("three")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i*(-457.87); lbl3.setText(""+i+" kelvin is equivalent to "+mn+" fahrenheit."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("four")){ lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i*33.8; lbl3.setText(""+i+" celsius is equivalent to "+mn+" fahrenheit."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else if(u.equalsIgnoreCase("")){ txt.setText("default: one"); lbl2.setVisible(true); lbl3.setVisible(true); txt2.setVisible(true); btn.setVisible(true); btn2.setVisible(true); if(e.getActionCommand().equals("x")){ try{ i = Double.parseDouble(txt2.getText()); mn = i/1000; lbl3.setText(""+i+" meter/s is equivalent to "+mn+" kilometer/s."); }catch(NumberFormatException t){ lbl3.setText("Invalid Input. Please try again."); } } if(e.getActionCommand().equals("g")){ txt.setText(""); txt2.setText(""); lbl3.setText(""); lbl2.setVisible(false); lbl3.setVisible(false); txt2.setVisible(false); btn.setVisible(false); btn2.setVisible(false); } }else{ txt.setText("Enter another choice."); }if(e.getActionCommand().equals("r")){ txt.setText(""); } } }
PS: java.lang.* and java.awt.* are never used in your program.
Last edited by Brt93yoda; September 2nd, 2010 at 03:05 PM.
khms (September 2nd, 2010)
You're welcome!
java.lang is actually being used, but it's always imported so you never need to explicitly tell Java you want to import it
Thanks for the info.
How do you get they get it to make a new Rectangle with the Rectangle class in Java? I can't figure that out for the life of me!
I've tried trying for a long time.
I need to somehow find the the upper-right and lower left point of the new Rectangle formed by the intersection of the two Rectangles. But how? I've tried and tried. Nothing works.
javapenguin (September 2nd, 2010)
Dang! why ask the question here *and* start a new thread?