// Programmer: Simbongile and Lesetsa
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 200;
public static final int NUMBER_OF_DIGITS = 20;
private JTextField ioField;
private JTextField ans;
private String result = " ";
public static void main(String[] args)
{
Calculator aCalculator = new Calculator();
aCalculator.setVisible(true);
}
public Calculator()
{
setTitle("Converter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLayout(new GridLayout(4, 1));
// Menu
JMenuBar mb = new JMenuBar();
JMenu mnuFile = new JMenu("File");
JMenuItem mnuItemQuit = new JMenuItem("Quit");
JMenu mnuHelp = new JMenu("Help");
JMenuItem mnuItemAbout = new JMenuItem("About");
setJMenuBar(mb);
mnuFile.add(mnuItemQuit);
mnuHelp.add(mnuItemAbout);
mb.add(mnuFile);
mb.add(mnuHelp);
// Allows the Swing App to be closed
addWindowListener(new ListenCloseWdw());
//Add Menu listener
mnuItemQuit.addActionListener(new ListenMenuQuit());
JPanel jp1 = new JPanel();
//jp1.setBackground(Color.BLUE);
jp1.setLayout(new FlowLayout());
JPanel jp2 = new JPanel();
//jp2.setBackground(Color.BLUE);
jp2.setLayout(new FlowLayout());
JPanel jp3 = new JPanel();
// jp3.setBackground(Color.BLUE);
jp3.setLayout(new FlowLayout());
JPanel jp4 = new JPanel();
//jp4.setBackground(Color.BLUE);
jp4.setLayout(new FlowLayout());
JLabel base = new JLabel("Click Base>>");
JLabel num = new JLabel("Enter number>> ");
ioField = new JTextField("", NUMBER_OF_DIGITS);
ioField.setBackground(Color.WHITE);
ans = new JTextField(" ", 20);
ans.setBackground(Color.WHITE);
JButton b2 = new JButton("2");
JButton b6 = new JButton("6");
JButton b8 = new JButton("8");
JButton b10 = new JButton("10");
JButton b16 = new JButton("16");
JButton conv = new JButton("Convert");
JButton clr = new JButton("Clear");
b2.addActionListener(this);
b6.addActionListener(this);
b8.addActionListener(this);
b10.addActionListener(this);
b16.addActionListener(this);
conv.addActionListener(this);
clr.addActionListener(this);
jp1.add(base);
jp1.add(b2);
jp1.add(b6);
jp1.add(b8);
jp1.add(b10);
//jp1.add(b16);
jp2.add(num);
jp2.add(ioField);
jp3.add(conv);
jp3.add(ans);
jp4.add(clr);
getContentPane().add(jp2);
getContentPane().add(jp1);
getContentPane().add(jp3);
getContentPane().add(jp4);
}
public class ListenMenuQuit implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public class ListenCloseWdw extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
try
{
assumingCorrectNumberFormats(e);
}
catch (NumberFormatException e2)
{
ans.setText("Error: Re-enter Number.");
}
}
public void assumingCorrectNumberFormats(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if(actionCommand.equals("2"))
{
String i = ioField.getText();
int j = Integer.parseInt(i);
result = Integer.toBinaryString((j));
}
else if(actionCommand.equals("6"))
{
String i = ioField.getText();
int j = Integer.parseInt(i);
result = Integer.toHexString(j);
}
else if(actionCommand.equals("8"))
{
String i = ioField.getText();
int j = Integer.parseInt(i);
result = Integer.toOctalString(j);
}
else if(actionCommand.equals("10"))
{
String i = ioField.getText();
result = i;
}
else if(actionCommand.equals("Convert"))
{
ans.setText(result);
}
else if(actionCommand.equals("Clear"))
{
result = " ";
ans.setText("");
}
else
ans.setText("Unexperted error.");
}
private static double stringToInt(String stringObject)
{
return Integer.parseInt(stringObject);
}
}
No comments:
Post a Comment