Posted by : Unknown
Friday, 19 June 2015
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PopupCalculatorApplet extends JApplet implements ActionListener
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PopupCalculatorApplet extends JApplet implements ActionListener
{
public void init()
public void init()
{
Button calcButton = new Button("Calculator");
calcButton.addActionListener(this);
Container contentPane = getContentPane();
contentPane.add(calcButton);
}
public void actionPerformed(ActionEvent evt)
Button calcButton = new Button("Calculator");
calcButton.addActionListener(this);
Container contentPane = getContentPane();
contentPane.add(calcButton);
}
public void actionPerformed(ActionEvent evt)
{
if (calc.isVisible())
calc.setVisible(false);
else
calc.show();
}
private JFrame calc = new CalculatorFrame();
}
class CalculatorPanel extends JPanel implements ActionListener
if (calc.isVisible())
calc.setVisible(false);
else
calc.show();
}
private JFrame calc = new CalculatorFrame();
}
class CalculatorPanel extends JPanel implements ActionListener
{
public CalculatorPanel()
public CalculatorPanel()
{
setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}
private void addButton(Container c, String s)
setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}
private void addButton(Container c, String s)
{
JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9' || s.equals("."))
String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9' || s.equals("."))
{
if (start)
display.setText(s);
else
display.setText(display.getText() + s);
start = false;
}
if (start)
display.setText(s);
else
display.setText(display.getText() + s);
start = false;
}
else
{
if (start)
if (start)
{
if (s.equals("-"))
if (s.equals("-"))
{
display.setText(s);
start = false;
} else
op = s;
}
display.setText(s);
start = false;
} else
op = s;
}
else
{
calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
}
}
}
public void calculate(double n)
calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
}
}
}
public void calculate(double n)
{
if (op.equals("+"))
arg += n;
else if (op.equals("-"))
arg -= n;
else if (op.equals("*"))
arg *= n;
else if (op.equals("/"))
arg /= n;
else if (op.equals("="))
arg = n;
display.setText("" + arg);
}
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
class CalculatorFrame extends JFrame
if (op.equals("+"))
arg += n;
else if (op.equals("-"))
arg -= n;
else if (op.equals("*"))
arg *= n;
else if (op.equals("/"))
arg /= n;
else if (op.equals("="))
arg = n;
display.setText("" + arg);
}
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
public CalculatorFrame()
{
setTitle("Calculator");
setSize(200, 200);
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
setTitle("Calculator");
setSize(200, 200);
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
Output:
Related Posts :
- Back to Home »
- Simple Application Program »
- Popup Calculator Applet