import java.awt.event.*;
import javax.swing.*;
public class MiniCalculator extends JFrame
{
private int WIDTH = 300;
private int HEIGHT = 75;
private JButton calculate;
private JComboBox ops;
private JTextField num1, num2;
private JLabel output;
private JPanel top, mid, bot;
private final String[] OPERATIONS = {"Add", "Subtract", "Divide", "Multiply"};
public MiniCalculator()
{
setTitle("Calculator");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
top = new JPanel();
num1 = new JTextField("Enter number");
num2 = new JTextField("Enter number");
top.add(num1);
top.add(num2);
add(top);
mid = new JPanel();
ops = new JComboBox(OPERATIONS);
mid.add(ops);
add(mid);
bot = new JPanel();
calculate = new JButton("Go!");
calculate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == calculate)
{
int one = Integer.parseInt(num1.getText());
int two = Integer.parseInt(num2.getText());
if(ops.getSelectedIndex() == 0)
output.setText((one + two) + "");
else if(ops.getSelectedIndex() == 1)
output.setText((one - two) + "");
else if(ops.getSelectedIndex() == 2)
output.setText((one * two) + "");
else if(ops.getSelectedIndex() == 3)
output.setText((one / two) + "");
}
}
});
output = new JTextField("Result...");
bot.add(calculate);
bot.add(output);
add(bot);
}
public static void main(String[] args)
{
new MiniCalculator();
}
}
This post has been edited by zim1985: 12 January 2011 - 12:38 PM

New Topic/Question
Reply




MultiQuote




|