Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2013, 12:38 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calculator GUI

Guys Help me please to understand the code. What is my fault in the program? thanks in advance.



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JPanel {
public JTextField tex;

private JButton button0,button1,button2,button3,button4,button5,bu tton6,button7;

private JButton button8,button9,buttonE,buttonD,buttonS,buttonA,bu ttonM,claerBut;
private double n1;
private double n2,ans;
int addclick;
int subclick;
int mclick;
int dclick;
int claerfield;
public static void main(String[] args) {

JFrame frame = new JFrame("Calculator");

frame.setContentPane(new Calculator());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.pack(); // Adjust the size of the window
frame.setSize(200, 300);
frame.setVisible(true);
}

public Calculator() {


JPanel panelCapital = new JPanel();
panelCapital.setBackground(Color.white);
tex = new JTextField("", 16);
tex.setLocation(5, 10);
tex.setSize(154, 40);
panelCapital.add(tex);
tex.setFont(new Font ("Arial",Font.BOLD,12));
add(panelCapital);
// Create the components
button0 = new JButton("0" );
button1 = new JButton("1" );
button2 = new JButton("2" );
button3 = new JButton("3" );
button4 = new JButton("4" );
button5 = new JButton("5" );
button6 = new JButton("6" );
button7 = new JButton("7" );
button8 = new JButton("8" );
button9 = new JButton("9" );
buttonM = new JButton("*" );
buttonS = new JButton("-" );
buttonD = new JButton("/" );
buttonE = new JButton("=" );
buttonA = new JButton("+" );
claerBut=new JButton ("C" );
button0.setVerticalTextPosition(AbstractButton.CEN TER);
setBackground(Color.white);



JPanel panelCapitl = new JPanel();
panelCapitl.add(button0);
panelCapitl.add(button1);
panelCapitl.add(button2);
panelCapitl.add(button3);
panelCapitl.add(button4);
panelCapitl.add(button5);
panelCapitl.add(button6);
panelCapitl.add(button7);
panelCapitl.add(button8);
panelCapitl.add(button9);
panelCapitl.add(buttonE);
panelCapitl.add(buttonD);
panelCapitl.add(buttonS);
panelCapitl.add(buttonA);
panelCapitl.add(buttonM);
panelCapitl.add(claerBut);
setLayout(new GridLayout(2, 9));

add(panelCapitl);
button0.setBackground(new Color(0, 100, 0)); button1.setBackground(new Color(0, 100, 0));
button2.setBackground(new Color(0, 100, 0));button3.setBackground(new Color(0, 100, 0));
button4.setBackground(new Color(0, 100, 0));button5.setBackground(new Color(0, 100, 0));
button6.setBackground(new Color(0, 100, 0)); button7.setBackground(new Color(0, 100, 0));
button8.setBackground(new Color(0, 100, 0)); button9.setBackground(new Color(0, 100, 0));
buttonD.setBackground(new Color(238, 221, 130)); buttonS.setBackground(new Color(238, 221, 130));
buttonM.setBackground(new Color(238, 221, 130)); buttonA.setBackground(new Color(238, 221, 130));
buttonE.setBackground(new Color(0, 191, 255)); claerBut.setBackground(new Color(255, 0, 0));
button0.setForeground(Color.white);
button1.setForeground(Color.white);
button2.setForeground(Color.white);
button3.setForeground(Color.white);
button4.setForeground(Color.white);
button5.setForeground(Color.white);
button6.setForeground(Color.white);
button7.setForeground(Color.white);
button8.setForeground(Color.white);
button9.setForeground(Color.white);
buttonD.setForeground(Color.white);
buttonS.setForeground(Color.white);
buttonA.setForeground(Color.white);
buttonE.setForeground(Color.white);
buttonM.setForeground(Color.white);
claerBut.setForeground(Color.white);


ActionListener listener0 = new Listener01();
button0.addActionListener(listener0);

ActionListener listener1 = new Listener1();
button1.addActionListener(listener1);

ActionListener listener2 = new Listener2();
button2.addActionListener(listener2);

ActionListener listener3 = new Listener3();
button3.addActionListener(listener3);

ActionListener listener4 = new Listener4();
button4.addActionListener(listener4);

ActionListener listener5 = new Listener5();
button5.addActionListener(listener5);

ActionListener listener6 = new Listener6();
button6.addActionListener(listener6);

ActionListener listener7 = new Listener7();
button7.addActionListener(listener7);

ActionListener listener8 = new Listener8();
button8.addActionListener(listener8);

ActionListener listener9 = new Listener9();
button9.addActionListener(listener9);

ActionListener listener10 = new Listener1O();
buttonS.addActionListener(listener10);

ActionListener listener11 = new Listener11();
buttonA.addActionListener(listener11);

ActionListener listener12 = new Listener12();
buttonM.addActionListener(listener12);

ActionListener listener13 = new Listener13();
buttonD.addActionListener(listener13);

ActionListener listener14 = new Listener14();
claerBut.addActionListener(listener14);

ActionListener listener15 = new Listener15();
buttonE.addActionListener(listener15);
}
class Listener01 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"0");
}
}
class Listener1 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"1");
}
}
class Listener2 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"2");
}
}

class Listener3 implements ActionListener {
public void actionPerformed(ActionEvent event) {
tex.setText(tex.getText()+"3");
}
}

class Listener4 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"4");
}
}

class Listener5 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"5");
}
}

class Listener6 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"6");
}
}

class Listener7 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"7");
}
}

class Listener8 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"8");
}
}

class Listener9 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"9");
}
}


class Listener1O implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==buttonS){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
subclick=1;
claerfield=1;

}
}

}
}

class Listener11 implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==buttonA){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
addclick=1;
claerfield=1;

}
}



}
}

class Listener12 implements ActionListener {
public void actionPerformed(ActionEvent event) {

if (event.getSource()==buttonM){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
mclick=1;
claerfield=1;

}
}
}
}

class Listener13 implements ActionListener {
public void actionPerformed(ActionEvent event) {

if (event.getSource()==buttonD){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
dclick=1;
claerfield=1;

}
}
}
}


class Listener14 implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==claerBut){
tex.setText("");
n1=0;
n2=0;
addclick=0;
subclick=0;
mclick=0;
dclick=0;
}

}
}

class Listener15 implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==buttonE){
n2=Double.parseDouble(String.valueOf(tex.getText() ));
tex.setText(tex.getText());

if(addclick==1){
ans=n1 + n2;
tex.setText(String.valueOf(ans));
addclick=0;
}

if(subclick==1){
ans=n1 - n2;
tex.setText(String.valueOf(ans));
subclick=0;
}

if(mclick==1){
ans=n1 * n2;
tex.setText(String.valueOf(ans));
mclick=0;
}

if(dclick==1){
ans=n1 / n2;
tex.setText(String.valueOf(ans));
dclick=0;
}

}
}
}




}
 
Old January 18th, 2014, 11:47 AM
Authorized User
Points: 209, Level: 4
Points: 209, Level: 4 Points: 209, Level: 4 Points: 209, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default From panel to Applet- try

Quote:
Originally Posted by NurasylMaratov View Post
Guys Help me please to understand the code. What is my fault in the program? thanks in advance.



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JPanel {
public JTextField tex;

private JButton button0,button1,button2,button3,button4,button5,bu tton6,button7;

private JButton button8,button9,buttonE,buttonD,buttonS,buttonA,bu ttonM,claerBut;
private double n1;
private double n2,ans;
int addclick;
int subclick;
int mclick;
int dclick;
int claerfield;
public static void main(String[] args) {

JFrame frame = new JFrame("Calculator");

frame.setContentPane(new Calculator());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.pack(); // Adjust the size of the window
frame.setSize(200, 300);
frame.setVisible(true);
}

public Calculator() {


JPanel panelCapital = new JPanel();
panelCapital.setBackground(Color.white);
tex = new JTextField("", 16);
tex.setLocation(5, 10);
tex.setSize(154, 40);
panelCapital.add(tex);
tex.setFont(new Font ("Arial",Font.BOLD,12));
add(panelCapital);
// Create the components
button0 = new JButton("0" );
button1 = new JButton("1" );
button2 = new JButton("2" );
button3 = new JButton("3" );
button4 = new JButton("4" );
button5 = new JButton("5" );
button6 = new JButton("6" );
button7 = new JButton("7" );
button8 = new JButton("8" );
button9 = new JButton("9" );
buttonM = new JButton("*" );
buttonS = new JButton("-" );
buttonD = new JButton("/" );
buttonE = new JButton("=" );
buttonA = new JButton("+" );
claerBut=new JButton ("C" );
button0.setVerticalTextPosition(AbstractButton.CEN TER);
setBackground(Color.white);



JPanel panelCapitl = new JPanel();
panelCapitl.add(button0);
panelCapitl.add(button1);
panelCapitl.add(button2);
panelCapitl.add(button3);
panelCapitl.add(button4);
panelCapitl.add(button5);
panelCapitl.add(button6);
panelCapitl.add(button7);
panelCapitl.add(button8);
panelCapitl.add(button9);
panelCapitl.add(buttonE);
panelCapitl.add(buttonD);
panelCapitl.add(buttonS);
panelCapitl.add(buttonA);
panelCapitl.add(buttonM);
panelCapitl.add(claerBut);
setLayout(new GridLayout(2, 9));

add(panelCapitl);
button0.setBackground(new Color(0, 100, 0)); button1.setBackground(new Color(0, 100, 0));
button2.setBackground(new Color(0, 100, 0));button3.setBackground(new Color(0, 100, 0));
button4.setBackground(new Color(0, 100, 0));button5.setBackground(new Color(0, 100, 0));
button6.setBackground(new Color(0, 100, 0)); button7.setBackground(new Color(0, 100, 0));
button8.setBackground(new Color(0, 100, 0)); button9.setBackground(new Color(0, 100, 0));
buttonD.setBackground(new Color(238, 221, 130)); buttonS.setBackground(new Color(238, 221, 130));
buttonM.setBackground(new Color(238, 221, 130)); buttonA.setBackground(new Color(238, 221, 130));
buttonE.setBackground(new Color(0, 191, 255)); claerBut.setBackground(new Color(255, 0, 0));
button0.setForeground(Color.white);
button1.setForeground(Color.white);
button2.setForeground(Color.white);
button3.setForeground(Color.white);
button4.setForeground(Color.white);
button5.setForeground(Color.white);
button6.setForeground(Color.white);
button7.setForeground(Color.white);
button8.setForeground(Color.white);
button9.setForeground(Color.white);
buttonD.setForeground(Color.white);
buttonS.setForeground(Color.white);
buttonA.setForeground(Color.white);
buttonE.setForeground(Color.white);
buttonM.setForeground(Color.white);
claerBut.setForeground(Color.white);


ActionListener listener0 = new Listener01();
button0.addActionListener(listener0);

ActionListener listener1 = new Listener1();
button1.addActionListener(listener1);

ActionListener listener2 = new Listener2();
button2.addActionListener(listener2);

ActionListener listener3 = new Listener3();
button3.addActionListener(listener3);

ActionListener listener4 = new Listener4();
button4.addActionListener(listener4);

ActionListener listener5 = new Listener5();
button5.addActionListener(listener5);

ActionListener listener6 = new Listener6();
button6.addActionListener(listener6);

ActionListener listener7 = new Listener7();
button7.addActionListener(listener7);

ActionListener listener8 = new Listener8();
button8.addActionListener(listener8);

ActionListener listener9 = new Listener9();
button9.addActionListener(listener9);

ActionListener listener10 = new Listener1O();
buttonS.addActionListener(listener10);

ActionListener listener11 = new Listener11();
buttonA.addActionListener(listener11);

ActionListener listener12 = new Listener12();
buttonM.addActionListener(listener12);

ActionListener listener13 = new Listener13();
buttonD.addActionListener(listener13);

ActionListener listener14 = new Listener14();
claerBut.addActionListener(listener14);

ActionListener listener15 = new Listener15();
buttonE.addActionListener(listener15);
}
class Listener01 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"0");
}
}
class Listener1 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"1");
}
}
class Listener2 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"2");
}
}

class Listener3 implements ActionListener {
public void actionPerformed(ActionEvent event) {
tex.setText(tex.getText()+"3");
}
}

class Listener4 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"4");
}
}

class Listener5 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"5");
}
}

class Listener6 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"6");
}
}

class Listener7 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"7");
}
}

class Listener8 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"8");
}
}

class Listener9 implements ActionListener {
public void actionPerformed(ActionEvent event) {

tex.setText(tex.getText()+"9");
}
}


class Listener1O implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==buttonS){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
subclick=1;
claerfield=1;

}
}

}
}

class Listener11 implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==buttonA){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
addclick=1;
claerfield=1;

}
}



}
}

class Listener12 implements ActionListener {
public void actionPerformed(ActionEvent event) {

if (event.getSource()==buttonM){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
mclick=1;
claerfield=1;

}
}
}
}

class Listener13 implements ActionListener {
public void actionPerformed(ActionEvent event) {

if (event.getSource()==buttonD){
if(tex.getText()!=null){
n1=Double.parseDouble(String.valueOf(tex.getText() ));
dclick=1;
claerfield=1;

}
}
}
}


class Listener14 implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==claerBut){
tex.setText("");
n1=0;
n2=0;
addclick=0;
subclick=0;
mclick=0;
dclick=0;
}

}
}

class Listener15 implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==buttonE){
n2=Double.parseDouble(String.valueOf(tex.getText() ));
tex.setText(tex.getText());

if(addclick==1){
ans=n1 + n2;
tex.setText(String.valueOf(ans));
addclick=0;
}

if(subclick==1){
ans=n1 - n2;
tex.setText(String.valueOf(ans));
subclick=0;
}

if(mclick==1){
ans=n1 * n2;
tex.setText(String.valueOf(ans));
mclick=0;
}

if(dclick==1){
ans=n1 / n2;
tex.setText(String.valueOf(ans));
dclick=0;
}

}
}
}




}


Hi
Please go through this applet code and correct in your prg the following errors ie) M,S,D,etc buttons only do not return values
As
muthukutta1
Quote:
package Calculator;

/*
<applet code="Calculator" width=400 height=400>
</applet>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Calculator extends Applet implements ActionListener
{
GridBagLayout gb = new GridBagLayout();

GridBagConstraints gbc= new GridBagConstraints();

TextField t1= new TextField("",20);
Button b1 = new Button("1");
Button b2 =new Button("2");
Button b3= new Button("3");
Button b4= new Button("4");
Button b5 =new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9= new Button("9");
Button b0=new Button("0");
Button bdo=new Button(".");
Button bp1=new Button("+");
Button bmi=new Button("-");
Button bdi=new Button("/");
Button bmu=new Button("*");
Button beq=new Button("=");

Button bex =new Button("Exit");
float value1,value2;
char oper;
public void addcomp(int row,int col,int wide,int high, Component com)
{
gbc.gridx=col;
gbc.gridy=row;
gbc.gridwidth=wide;
gbc.gridheight=high;
gb.setConstraints(com,gbc);
add(com);
if(com instanceof Button)
{
Button b=(Button) com;
b.addActionListener(this);
}
}
public void init()
{
setLayout(gb);
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(5,5,5,5);
gbc.weightx=1.0;
gbc.weighty=1.0;
addcomp(0,0,3,1,t1);
addcomp(0,3,2,1,bex);
addcomp(1,0,1,1,b7);
addcomp(1,1,1,1,b8);
addcomp(1,2,1,1,b9);
addcomp(1,3,1,1,bmi);
addcomp(1,4,1,1,beq);
addcomp(2,0,1,1,b4);
addcomp(2,1,1,1,b5);
addcomp(2,2,1,1,b6);
addcomp(2,3,1,3,bp1);
addcomp(2,4,1,1,bdi);
addcomp(3,0,1,1,b1);
addcomp(3,1,1,1,b2);
addcomp(3,2,1,1,b3);
addcomp(3,4,1,2,bmu);
addcomp(4,0,1,2,b0);
addcomp(4,2,1,1,bdo);
this.setSize(320,240);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
t1.setText(t1.getText()+"1");
}
if(e.getSource()==bex)
{
setVisible(false);
this.stop();
}
if(e.getSource() == b2)
{
t1.setText(t1.getText()+"2");
}
if(e.getSource()==b3)
{
t1.setText(t1.getText() +"3");
}
if(e.getSource()==b4)
{
t1.setText(t1.getText()+"4");
}
if(e.getSource()==b5)
{
t1.setText(t1.getText()+"5");
}
if(e.getSource()==b6)
{
t1.setText(t1.getText()+"6");
}
if (e.getSource()==b7)
{
t1.setText(t1.getText() +"7");
}
if(e.getSource() ==b8)
{
t1.setText(t1.getText() +"8");
}
if (e.getSource() ==b9)
{
t1.setText(t1.getText()+"9");
}
if(e.getSource() ==b0)
{
t1.setText(t1.getText()+"0");
}
if(e.getSource()==bdo)
{
t1.setText(t1.getText()+".");
}
if(e.getSource()==bmi)
{
value1=(Float.valueOf(t1.getText())).floatValue();
oper='-';
t1.setText(" ");
}
if(e.getSource()==bmu)
{
value1=(Float.valueOf(t1.getText())).floatValue();
oper='*';
t1.setText(" ");
}
if(e.getSource()==bp1)
{
value1=(Float.valueOf(t1.getText())).floatValue();
oper='+';
t1.setText(" ");
}
if(e.getSource() ==bdi)
{
value1=(Float.valueOf(t1.getText())).floatValue();
oper='/';
t1.setText(" ");
}
if(e.getSource()==beq)
{
value2=(Float.valueOf(t1.getText())).floatValue();
switch(oper)
{
case '+':
value1 += value2;
break;
case '-':
value1 -= value2;
break;
case '/':
value1 /= value2;
break;
case '*':
value1 *= value2;
break;
}
value2 =0;
t1.setText(String.valueOf(value1));
}
}

public static void main(String args[])
{


Frame frame = new Frame();
Calculator app=new Calculator();
app.init();

frame.add("Center",app);
frame.setVisible(true);
frame.setSize(200,200);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});






}











}




Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Calculator JustSk8n BOOK: Ivor Horton's Beginning Visual C++ 2010 1 March 28th, 2012 10:27 AM
Calculator AdamPembs BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 January 31st, 2010 02:48 PM
calculator mojtaba rashidi Visual Studio 2005 0 March 17th, 2008 07:29 AM
Calculator DweeLer Other Programming Languages 1 November 18th, 2005 08:13 AM
calculator kale_tushar C++ Programming 1 January 28th, 2004 01:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.