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 7 of 7

Thread: Calculator GUI Help

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calculator GUI Help

    I am diving into my first major GUI project and am stumped from the start. I have made a diagram and would like someone to point me into the correct direction so that I may start my task with a good foundation. I am making a calculator that I would like to use as a stand alone application. Here is my diagram:
    Drawing1.jpg
    Any advice to help me get started?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Calculator GUI Help

    Which part of this is giving you trouble? What have you tried? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator GUI Help

    I'm not too sure where to begin. I know the base for an application is the JFrame (or at least so I believe). Then after that I would add JPanels to group the different components together? I am just having difficulties with the layouts and how I would add or nest the different pieces into the the JFrame. I'm just beginning to learn the GUI basics so any information is greatly helpful. Thanks again

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Calculator GUI Help

    I'd start here: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    But you have the basic idea down- you have a JFrame, which is the main window. That JFrame has a content pane, which is itself a JPanel. That JPanel can contain other JPanels, which can all have different layouts (and include even more JPanels).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator GUI Help

    That is exactly the advice I was looking for. Much appreciated! I'm sure I will have more questions pertaining to this project as I progress through.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Location
    Florida
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator GUI Help

    I finally decided on using a GridLayout layour manager. However, with no experience on the subject at all, I decided to tailor an existing source code to try and meet my needs. That didn't go so well unfortunately. Now I am stuck with useless code that I am hoping someone can help me fix. At the moment I am just trying to get the different components to display; later, I will make the methods that accompany them.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TEMPERATURE extends JFrame {
    	static final String units[] = {"Celcius", "Farenheit", "Kelvin"};
    	JComboBox convertFrom, convertTo;
    	JTextField initialValue, calculatedValue;
    	JButton convertButton;
    	GridLayout calculator = new GridLayout(2,2);
     
    		public TEMPERATURE (String name) {
    			super(name);
    			setResizable(false);
    		}
    	public void initUnits() {
    		convertFrom = new JComboBox(units);
    		convertTo = new JComboBox(units);	
    	}
    	public void addComponentsToPane(final Container pane) {
    		initUnits();
    		final JPanel aCalculator = new JPanel();
    		aCalculator.setLayout(calculator);
    		JPanel converter = new JPanel();
    		converter.setLayout(new GridLayout(1,1));
     
    		aCalculator.add(initialValue);
    		aCalculator.add(convertFrom);
    		aCalculator.add(calculatedValue);
    		aCalculator.add(convertTo);
     
    		converter.add(convertButton);
    		//converter.addActionListener needed here
    		//**missing section**
     
    		pane.add(aCalculator, BorderLayout.NORTH);
    		pane.add(new JSeparator(), BorderLayout.CENTER);
    		pane.add(converter, BorderLayout.SOUTH);
     
    	}
     
    	private static void makeCalculator () {
    		TEMPERATURE frame = new TEMPERATURE("Temperature Calculator");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.addComponentsToPane(frame.getContentPane());
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args) {
    				makeCalculator();
    	}		
    }

    I know this is extremely far off from the end result, but any directioning would boost my morale at this points lol

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Calculator GUI Help

    What does this code do? Does it give you any errors? When do you initialize each of the components you're using?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help with a calculator
    By Joshroark10 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2012, 03:59 PM
  2. Age Calculator
    By Sagittarian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 1st, 2012, 08:06 PM
  3. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM