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.

Results 1 to 3 of 3

Thread: Java Swing

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Java Swing

    I am learning java swing and the following code is not working. What am I doing wrong?

    public void actionPerformed(ActionEvent ae)
    	{
    		if(ae.getActionCommand().equals("Convert"))
    		{
    			if(Cel)
    			{
    			int jml = Integer.parseInt(Cel.getText());
    			int result = jml * 9/5 + 32;
    			Fah.setText(Integer.toString(result));
    			}
    			else
    			{
    				int jml = Integer.parseInt(Fah.getText());
    				int result = (jml - 32) * 5/9;
    				Fah.setText(Integer.toString(result));
    			}
    		}
    		else
    		{
    			Cel.setText("");
    			Fah.setText("");
    		}
    	}


     
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.border.Border;
     
    public class TempConvertor extends JFrame implements ActionListener
    {
    	JButton Convert;
    	JButton Reset;
    	JTextField Cel;
    	JTextField Fah;
    	JLabel CelLbl;
    	JLabel farenheight;
     
    	TempConvertor()
    	{
    		JFrame j = new JFrame("Temp Convertor");
    		j.setSize(200,175);
    		j.setResizable(false);
    		j.setLayout(new FlowLayout());
    		j.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		//create buttons
    		Convert = new JButton("Convert");
    		Reset = new JButton("Reset");
     
    		Convert.addActionListener(this);
    		Reset.addActionListener(this);
     
    		//create labels
    		CelLbl = new JLabel("Celsius");
    		farenheight = new JLabel("Farenheight");
     
    		CelLbl.setVerticalAlignment(SwingConstants.BOTTOM);
    		farenheight.setVerticalAlignment(SwingConstants.BOTTOM);
    		Border border = BorderFactory.createEtchedBorder();
    		CelLbl.setBorder(border);
    		farenheight.setBorder(border);
     
    		JPanel cp = ((JPanel) j.getContentPane());
    		cp.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
     
    		//create text fields
    		Cel = new JTextField(5);
    		Fah = new JTextField(5);
     
    		Cel.setActionCommand("Cel");
    		Fah.setActionCommand("Fah");
     
    		//add TextFields
    		j.add(CelLbl);
    		j.add(Cel);
    		j.add(farenheight);//label
     
    		j.add(Fah);
     
    		//add Buttons
    		j.add(Convert);
    		j.add(Reset);
     
    		j.setVisible(true);	
    	}
     
    	public void actionPerformed(ActionEvent ae)
    	{
    		if(ae.getActionCommand().equals("Convert"))
    		{
    			if(Cel)
    			{
    			int jml = Integer.parseInt(Cel.getText());
    			int result = jml * 9/5 + 32;
    			Fah.setText(Integer.toString(result));
    			}
    			else
    			{
    				int jml = Integer.parseInt(Fah.getText());
    				int result = (jml - 32) * 5/9;
    				Fah.setText(Integer.toString(result));
    			}
    		}
    		else
    		{
    			Cel.setText("");
    			Fah.setText("");
    		}
    	}
     
     
    	public static void main(String[] args)
    	{
    		new TempConvertor();
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Swing

    the following code is not working
    Define 'not working'. Does it compile? Are there exceptions? Does it misbehave, and if so, how? Without supplying more details we're left guessing

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java Swing

    Most likely not your main problem, but I assume this is not what you intend: 9/5 and 5/9
    Those produce 1 and 0, respectfully. You are doing something called "Integer Division". To put it simply: 9/5 should equal 1.8, but an int divided by an int makes another int, not a double (ints have no decimal places, doubles have decimal places). In order to get a double value, at least one of the values need to be a double. To make one of those a double, just add a decimal point to one of them.
    So, in java:
    9/5 = 1
    9.0/5 = 1.8
    9/5.0 = 1.8
    9.0/5.0 = 1.8
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 07:32 PM
  2. Java Swing
    By arvin17 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2013, 09:40 AM
  3. Java Swing
    By Rootntootn in forum AWT / Java Swing
    Replies: 2
    Last Post: April 2nd, 2012, 12:50 PM
  4. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  5. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM