Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

help with basic 4 function calc

setpreferredsize

  • Please log in to reply
5 replies to this topic

#1 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 22 February 2012 - 10:50 PM

I have my calculator made and everything, it's extremely basic. I was just wondering if anyone could help me with how to position the labels and buttons so it looks cleaner, and I was also wondering if there's an easier way than writing 4 different inner classes (AddListener, SubListener, MultListener, DivListener) to listen for 4 different buttons.

Here's the code:

import javax.swing.JFrame;

public class calc {
	public static void main(String args[]) {
		JFrame frame = new JFrame("4 Function Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		calcpanel panel = new calcpanel();
		
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setVisible(true);
	}
}

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

public class calcpanel extends JPanel{
	private JLabel firstInput, secondInput, outputLabel, resultLabel;
	private JButton add, sub, mult, div;
	private JTextField input1, input2;
	
	public calcpanel(){
		firstInput = new JLabel("Enter a number: ");
		secondInput = new JLabel("Enter a number: ");
		outputLabel = new JLabel("Answer: ");
		resultLabel = new JLabel("---");
		add = new JButton("+");
		add.addActionListener(new AddListener());
		sub = new JButton("-");
		sub.addActionListener(new SubListener());
		mult = new JButton("x");
		mult.addActionListener(new MultListener());
		div = new JButton("/");
		div.addActionListener(new DivListener());
		input1 = new JTextField(5);
		input2 = new JTextField(5);
		
		add (firstInput);
		add (input1);
		add (secondInput);
		add (input2);
		add (add);
		add (sub);
		add (mult);
		add (div);
		add (outputLabel);
		add (resultLabel);
		
		setPreferredSize (new Dimension(300, 150));
	}
	
	private class AddListener implements ActionListener{
		public void actionPerformed (ActionEvent event){
			int x, y, total;
			String text = input1.getText();
			x = Integer.parseInt(text);
			
			text = input2.getText();
			y = Integer.parseInt(text);
			
			total = x + y;
			
			resultLabel.setText (Integer.toString(total));
		}
	}
	private class SubListener implements ActionListener{
		public void actionPerformed (ActionEvent event){
			int x, y, total;
			String text = input1.getText();
			x = Integer.parseInt(text);
			
			text = input2.getText();
			y = Integer.parseInt(text);
			
			total = x - y;
			
			resultLabel.setText (Integer.toString(total));
		}
	}
	private class MultListener implements ActionListener{
		public void actionPerformed (ActionEvent event){
			int x, y, total;
			String text = input1.getText();
			x = Integer.parseInt(text);
			
			text = input2.getText();
			y = Integer.parseInt(text);
			
			total = x * y;
			
			resultLabel.setText (Integer.toString(total));
		}
	}
	private class DivListener implements ActionListener{
		public void actionPerformed (ActionEvent event){
			int x, y, total;
			String text = input1.getText();
			x = Integer.parseInt(text);
			
			text = input2.getText();
			y = Integer.parseInt(text);
			
			total = x / y;
			
			resultLabel.setText (Integer.toString(total));
		}
	}
}

thanks for the help anyone and everyone :D

edit: for anyone who tries running the calculator: you have to type in numbers on both fields first, then you hit the button and it will calculate. You can't type one number, hit the button, then type another number. I'll change this soon, I just wanted to finish it first.
  • 0

#2 SirJames

SirJames

    CC Regular

  • Member
  • PipPipPip
  • 27 posts

Posted 23 February 2012 - 04:33 AM

To change the layout you could use "FlowLayout" e.g


import javax.swing.*;

import java.awt.*;

public class flow extends JFrame{

public flow(){

super("Random GUI");
setSize(250,120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

Container pane = getContentPane();
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
JButton Play = new JButton("Play");
JButton Pause = new JButton("Pause");
JButton Stop = new JButton("Stop");

JLabel label = new JLabel("This is how Flow layout works");

pane.add(label);
pane.add(Play);
pane.add(Pause);
pane.add(Stop);
setContentPane(pane);
}
public static void main(String[] args){
flow f = new flow();
}
}

and you could use "BorderLayout" you can set where you want the buttons and labels e.g



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

public class flow extends JFrame {
Container pane = getContentPane();
BorderLayout BorderLayout = new BorderLayout();

JButton button1 = new JButton("We set this one to the Center");
JButton label = new JButton("We set this label to the south");
JButton button = new JButton("We set this button to the north");
public flow(){
super("The GUI or Graphical User Interface :D i made!");
setSize(205,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setLayout(BorderLayout);
pane.add(label, BorderLayout.SOUTH);
pane.add(button, BorderLayout.NORTH);
pane.add(button1, BorderLayout.CENTER);

}
public static void main(String args[]){
flow f = new flow();
}
}



i hope this Helped abit :) mabey just gives you an i dea of what you can use,as for the keylisteners im not to sure :).

Regards James
  • 0

#3 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 23 February 2012 - 04:44 AM

an easier way than writing 4 different inner classes

Write one class that for all the listeners. It would start with the common code and then use a switch statement or if/else if statements to select which operation it was to do with the parsed values. The event object passed to the listener has a reference to the button that was pressed. You'd use that to determine the operation to do.
  • 0

#4 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 23 February 2012 - 07:46 AM

Write one class that for all the listeners. It would start with the common code and then use a switch statement or if/else if statements to select which operation it was to do with the parsed values. The event object passed to the listener has a reference to the button that was pressed. You'd use that to determine the operation to do.


what would be in the if statements exactly though? I don't know what the reference to each button is.


thanks sir james :D
  • 0

#5 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 23 February 2012 - 08:01 AM

I don't know what the reference to each button is.

JButton add, sub, mult, div;
Those are the references to the four buttons
  • 0

#6 SirJames

SirJames

    CC Regular

  • Member
  • PipPipPip
  • 27 posts

Posted 23 February 2012 - 04:03 PM

-_- The post i made was irrelevant ... sorry didn't read it right,i saw it as how to set the layout of your calculator.:pinguin:
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download