3 Replies - 444 Views - Last Post: 12 January 2011 - 01:04 PM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

JComponents Don't Add to JFrame

Posted 12 January 2011 - 12:37 PM

Trying to make a simple calculator, but I can't seem to make this work correctly. I added all the JPanels and JTextfields, but I obviously did something wrong.


import java.awt.event.*;
import javax.swing.*;

public class MiniCalculator extends JFrame
{
	private int WIDTH = 300;
	private int HEIGHT = 75;
	
	private JButton calculate;
	private JComboBox ops;
	private JTextField num1, num2;
        private JLabel output;
	private JPanel top, mid, bot;
	
	private final String[] OPERATIONS = {"Add", "Subtract", "Divide", "Multiply"};
	
	public MiniCalculator()
	{
		setTitle("Calculator");
		setSize(300,300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setVisible(true);
		
		top = new JPanel();
		num1 = new JTextField("Enter number");
		num2 = new JTextField("Enter number");
		top.add(num1);
		top.add(num2);
		add(top);
		
		mid = new JPanel();
		ops = new JComboBox(OPERATIONS);
		mid.add(ops);
		add(mid);
		
		bot = new JPanel();
		calculate = new JButton("Go!");
		calculate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				if(ae.getSource() == calculate)
				{
					int one = Integer.parseInt(num1.getText());
					int two = Integer.parseInt(num2.getText());
					if(ops.getSelectedIndex() == 0)
						output.setText((one + two) + "");
					else if(ops.getSelectedIndex() == 1)
						output.setText((one - two) + "");
					else if(ops.getSelectedIndex() == 2)
						output.setText((one * two) + "");
					else if(ops.getSelectedIndex() == 3)
						output.setText((one / two) + "");
				}
			}
		});
		output = new JTextField("Result...");
		bot.add(calculate);
		bot.add(output);
		add(bot);
	}
	
	public static void main(String[] args)
	{
		new MiniCalculator();
	}
}


This post has been edited by zim1985: 12 January 2011 - 12:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: JComponents Don't Add to JFrame

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JComponents Don't Add to JFrame

Posted 12 January 2011 - 12:53 PM

When creating JFrame, the call to setVisible(true) should be made the last call, (actually after you have already added all the components to the GUI)
Was This Post Helpful? 1
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: JComponents Don't Add to JFrame

Posted 12 January 2011 - 12:57 PM

View Postjapanir, on 12 January 2011 - 10:53 AM, said:

When creating JFrame, the call to setVisible(true) should be made the last call, (actually after you have already added all the components to the GUI)

That worked perfectly. Wow, I must have forgotten that. Thanks :D
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JComponents Don't Add to JFrame

Posted 12 January 2011 - 01:04 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1