d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Gui
Add Reply New Topic New Poll
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Apr 22 2015 07:19pm
Okay, so I am fairly new to listeners and such.

My goal is to represent a memory GUI calc, and I am having trouble getting it to perform function as well as stretch out the "=" sign on the bottom of the applet. Can someone point out where I failed.


Code




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
// import necessary info for applet


public class JavaCalculator extends Applet {

private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
//imported JPanel for setup

public JavaCalculator(){
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;

// add the display
display = new JButton("0");
display.setEnabled(false);
add(display, BorderLayout.NORTH);

ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();

//INtroduce grid layout
panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));

//Adding buttons to calc
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("+", command);

addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("-", command);

addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("*", command);

addButton("0", insert);
addButton(".", insert);
addButton("C", command);
addButton("/", command);

addButton("\t = \t", command);

add(panel, BorderLayout.CENTER);

}

private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}


private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start);
{
display.setText(" ");
start = false;
}
display.setText(display.getText() + "0.0");
}
}

/**
* This action executes the command that the button action string denotes
*/

private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

if(start)
{
if (command.equals("_"))
{
display.setText("0.0");
start = false;
}

}
}
}



}

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 22 2015 08:43pm
for starters, get rid of the semicolon

if (start);

i'm not clear what this is supposed to do either, considering that you don't have an underscore anywhere else in the app:

if (command.equals("_"))

i also suggest adding log statements to every method so you know what is getting called. if you click a button and it's not creating a log statement, you know your listener isn't getting triggered. you have a lot of if statements, but if you're not sure why it's not working, it might not be executing. it should help you figure out the problem.

design-wise, i'd split up the view from the calculations so that you can test it from the command line without worrying about the applet/swing/etc.

eg:
Calculator c = new Calculator();
c.push("1");
c.push("+");
c.push("2");
c.push("="); // returns 3

test it via your command line, then your swing applet uses the exact same logic, just with buttons. you'll literally just accept input from user, feed it into your calculator, and display the result. dont have to track the last command or any of that inside your applet-specific code.

This post was edited by carteblanche on Apr 22 2015 08:57pm
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Apr 22 2015 08:57pm
Quote (carteblanche @ Apr 22 2015 10:43pm)
for starters, get rid of the semicolon

if (start);

i'm not clear what this is supposed to do either, considering that you don't have an underscore anywhere else in the app:

if (command.equals("_"))

i also suggest adding log statements to every method so you know what is getting called. if you click a button and it's not creating a log statement, you know your listener isn't getting triggered. you have a lot of if statements, but if you're not sure why it's not working, it might not be executing. it should help you figure out the problem.

design-wise, i'd split up the view from the calculations so that you can test it from the command line without worrying about the applet/swing/etc.

eg:
Calculator c = new Calculator();
c.push("1");
c.push("+");
c.push("2");
c.push("="); // returns 3

test it via your command line, then your swing applet uses the exact same logic, just with buttons.



Code
package Calculator;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

// import necessary info for applet


public class JavaCalculator extends Applet {

private boolean start;

private double result;

private JButton display;

private JPanel panel;

private String lastCommand;


//imported JPanel for setup

public JavaCalculator(){
setLayout(new BorderLayout());

result = 0;

lastCommand = "=";

start = true;

// add the display

display = new JButton("0");
display.setEnabled(false);

add(display, BorderLayout.NORTH);

ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();

//INtroduce grid layout

panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));

//Adding buttons to calc

addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("+", command);

addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("-", command);

addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("*", command);

addButton("0", insert);
addButton(".", insert);
addButton("C", command);
addButton("/", command);

addButton("=", command);

add(panel, BorderLayout.CENTER);

}

//Adds Button
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}

//Inserts Action

private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start);
{
display.setText(" ");
start = false;
}
display.setText(display.getText() + input);
}
}

//Executes command

private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

if(start)
{
if (command.equals("_"))
{
display.setText(command);
start = false;
}
else lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}


//Performs Final Calculations

public void calculate(double x)
{
if(lastCommand.equals("+")) result += x;
else if(lastCommand.equals("-")) result -= x;
else if(lastCommand.equals("*")) result *= x;
else if(lastCommand.equals("/")) result /= x;
else if(lastCommand.equals("=")) result = x;
display.setText("" + result);
}
}


Okay, so you were saying I should add logs. That sounds like a good idea.

Also, what am I doing wrong regarding the "=" button. I'd like it to fill up the entire bottom part of the screen if possible.

This post was edited by axid3nt on Apr 22 2015 08:58pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 22 2015 09:01pm
Quote (axid3nt @ Apr 22 2015 10:57pm)
Also, what am I doing wrong regarding the "=" button. I'd like it to fill up the entire bottom part of the screen if possible.


you're using GridLayout for your buttons. did you read the documentation?

http://docs.oracle.com/javase/6/docs/api/java/awt/GridLayout.html

Quote
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. For example, the following is an applet that lays out six buttons into three rows and two columns:


if you dont want them to be equal sizes, you'll want to use something else.

https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

This post was edited by carteblanche on Apr 22 2015 09:02pm
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Apr 22 2015 09:15pm
Quote (carteblanche @ Apr 22 2015 11:01pm)
you're using GridLayout for your buttons. did you read the documentation?

http://docs.oracle.com/javase/6/docs/api/java/awt/GridLayout.html



if you dont want them to be equal sizes, you'll want to use something else.

https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html


No, I hadn't read the whole web site just yet. Still getting the hand of things.

Can you recommend the best way to setup my C button? I changed the grid and everything but the C is set up.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2015 04:23pm
Quote (axid3nt @ Apr 22 2015 11:15pm)
No, I hadn't read the whole web site just yet. Still getting the hand of things.

Can you recommend the best way to setup my C button? I changed the grid and everything but the C is set up.


different ways to do it. you can put it in another border layout. put a panel containing everything except = into north, then put = into the south.

or you can do it a different way.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll