Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import javax.swing.border.Border;
- public class problem1 extends JFrame {
- private int l = 20;
- private int w = 70;
- private int h = 40;
- private int g = 5;
- private Container c;
- private JPanel jp;
- private Font font, font2, font3;
- private JMenuBar menubar;
- private JMenu view, edit, help;
- private JTextField tf;
- private Border border;
- private JRadioButton deg, rad, grad;
- private ButtonGroup bg;
- private JPanel panel1;
- private JButton empty, lnt, dms, pi, F_E;
- private JButton lnv, sinh, cosh, tanh, exp;
- private JButton ln, sin, cos, tan, mod;
- private JButton ob, xp2, xpy, xp3, log;
- private JButton cb, nf, xry, xr3, tenpx;
- private JButton mc, la, seven, four, one, zero;
- private JButton mr, ce, eight, five, two;
- private JButton ms, C, nine, six, three, dot;
- private JButton mp, pm, div, mul, min, plus;
- private JButton mm, sq, per, inv, eq;
- problem1()
- {
- this.setSize(10*w+2*l, 9*h+l);
- this.setLocationRelativeTo(null);
- this.setTitle("Calculator");
- this.setResizable(true);
- init();
- }
- void init()
- {
- // creating contentpane
- c = this.getContentPane();
- c.setLayout(null);
- jp = new JPanel();
- jp.setLayout(null);
- c.add(jp);
- jp.setBounds(0, 0, 10*w+2*l, 9*h+l);
- // setting font
- font = new Font("Consolas", Font.BOLD, 32);
- font2 = new Font("Consolas", Font.BOLD, 16);
- font3 = new Font("Consolas", Font.BOLD, 12);
- // setting border
- border = BorderFactory.createLineBorder(Color.black);
- // adding components
- createMenu();
- addTextField();
- addRadioButtons();
- column1Buttons();
- column2Buttons();
- column3Buttons();
- column4Buttons();
- column5Buttons();
- column6Buttons();
- column7Buttons();
- column8Buttons();
- column9Buttons();
- column10Buttons();
- }
- public static void main(String[] args) throws Exception {
- problem1 frame = new problem1();
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- void createMenu()
- {
- menubar = new JMenuBar();
- view = new JMenu("View");
- edit = new JMenu("Edit");
- help = new JMenu("Help");
- menubar.add(view);
- menubar.add(edit);
- menubar.add(help);
- setJMenuBar(menubar);
- }
- void addTextField()
- {
- tf = new JTextField();
- tf.setText("0");
- tf.setBounds(l+g, g, 10*w - 2*g, 2*h - 2*g);
- tf.setFont(font);
- tf.setHorizontalAlignment(JTextField.RIGHT);
- tf.setEditable(false);
- tf.setBorder(border);
- jp.add(tf);
- }
- void addRadioButtons()
- {
- bg = new ButtonGroup();
- panel1 = new JPanel();
- panel1.setBounds(l+g, 2*h + g, 5*w - 2*g, h - 2*g);
- panel1.setBorder(border);
- panel1.setLayout(null);
- deg = new JRadioButton("Degrees");
- rad = new JRadioButton("Radians");
- grad = new JRadioButton("Grads");
- int x = (int) ((5*w - 8*g) / (3.0));
- deg.setBounds(g, 1, x, h - 2*g - 2);
- deg.setFont(font2);
- deg.setVerticalAlignment(JRadioButton.CENTER);
- deg.setFocusable(false);
- bg.add(deg);
- rad.setBounds(x + 3*g, 1, x, h - 2*g - 2);
- rad.setFont(font2);
- rad.setVerticalAlignment(JRadioButton.CENTER);
- rad.setFocusable(false);
- bg.add(rad);
- grad.setBounds(2*x + 5*g, 1, x, h - 2*g - 2);
- grad.setFont(font2);
- grad.setVerticalAlignment(JRadioButton.CENTER);
- grad.setFocusable(false);
- bg.add(grad);
- panel1.add(deg);
- panel1.add(rad);
- panel1.add(grad);
- jp.add(panel1);
- }
- void column1Buttons()
- {
- empty = new JButton();
- empty.setBounds(l+g, 3*h+g, w-2*g, h-2*g);
- empty.setFocusable(false);
- empty.setFont(font2);
- jp.add(empty);
- lnt = new JButton("lnt");
- lnt.setBounds(l+g, 4*h+g, w-2*g, h-2*g);
- lnt.setFont(font2);
- lnt.setFocusable(false);
- jp.add(lnt);
- dms = new JButton("dms");
- dms.setBounds(l+g, 5*h+g, w-2*g, h-2*g);
- dms.setFont(font2);
- dms.setFocusable(false);
- jp.add(dms);
- pi = new JButton("\u03C0");
- pi.setBounds(l+g, 6*h+g, w-2*g, h-2*g);
- pi.setFocusable(false);
- pi.setFont(font2);
- jp.add(pi);
- F_E = new JButton("F-E");
- F_E.setBounds(l+g, 7*h+g, w-2*g, h-2*g);
- F_E.setFocusable(false);
- F_E.setFont(font2);
- jp.add(F_E);
- }
- void column2Buttons()
- {
- lnv = new JButton("lnv");
- lnv.setBounds(l+w+g, 3*h+g, w-2*g, h-2*g);
- lnv.setFocusable(false);
- lnv.setFont(font2);
- jp.add(lnv);
- sinh = new JButton("sinh");
- sinh.setBounds(l+w+g, 4*h+g, w-2*g, h-2*g);
- sinh.setFont(font3);
- sinh.setFocusable(false);
- jp.add(sinh);
- cosh = new JButton("cosh");
- cosh.setBounds(l+w+g, 5*h+g, w-2*g, h-2*g);
- cosh.setFont(font3);
- cosh.setFocusable(false);
- jp.add(cosh);
- tanh = new JButton("tanh");
- tanh.setBounds(l+w+g, 6*h+g, w-2*g, h-2*g);
- tanh.setFocusable(false);
- tanh.setFont(font3);
- jp.add(tanh);
- exp = new JButton("Exp");
- exp.setBounds(l+w+g, 7*h+g, w-2*g, h-2*g);
- exp.setFocusable(false);
- exp.setFont(font2);
- jp.add(exp);
- }
- void column3Buttons()
- {
- ln = new JButton("ln");
- ln.setBounds(l+2*w+g, 3*h+g, w-2*g, h-2*g);
- ln.setFocusable(false);
- ln.setFont(font2);
- jp.add(ln);
- sin = new JButton("sin");
- sin.setBounds(l+2*w+g, 4*h+g, w-2*g, h-2*g);
- sin.setFont(font2);
- sin.setFocusable(false);
- jp.add(sin);
- cos = new JButton("cos");
- cos.setBounds(l+2*w+g, 5*h+g, w-2*g, h-2*g);
- cos.setFont(font2);
- cos.setFocusable(false);
- jp.add(cos);
- tan = new JButton("tan");
- tan.setBounds(l+2*w+g, 6*h+g, w-2*g, h-2*g);
- tan.setFocusable(false);
- tan.setFont(font2);
- jp.add(tan);
- mod = new JButton("Mod");
- mod.setBounds(l+2*w+g, 7*h+g, w-2*g, h-2*g);
- mod.setFocusable(false);
- mod.setFont(font2);
- jp.add(mod);
- }
- void column4Buttons()
- {
- ob = new JButton("(");
- ob.setBounds(l+3*w+g, 3*h+g, w-2*g, h-2*g);
- ob.setFocusable(false);
- ob.setFont(font2);
- jp.add(ob);
- xp2 = new JButton("x\u00B2");
- xp2.setBounds(l+3*w+g, 4*h+g, w-2*g, h-2*g);
- xp2.setFont(font2);
- xp2.setFocusable(false);
- jp.add(xp2);
- xpy = new JButton("x\u02B8");
- xpy.setBounds(l+3*w+g, 5*h+g, w-2*g, h-2*g);
- xpy.setFont(font2);
- xpy.setFocusable(false);
- jp.add(xpy);
- xp3 = new JButton("x\u00B3");
- xp3.setBounds(l+3*w+g, 6*h+g, w-2*g, h-2*g);
- xp3.setFocusable(false);
- xp3.setFont(font2);
- jp.add(xp3);
- log = new JButton("log");
- log.setBounds(l+3*w+g, 7*h+g, w-2*g, h-2*g);
- log.setFocusable(false);
- log.setFont(font2);
- jp.add(log);
- }
- void column5Buttons()
- {
- cb = new JButton(")");
- cb.setBounds(l+4*w+g, 3*h+g, w-2*g, h-2*g);
- cb.setFocusable(false);
- cb.setFont(font2);
- jp.add(cb);
- nf = new JButton("n!");
- nf.setBounds(l+4*w+g, 4*h+g, w-2*g, h-2*g);
- nf.setFont(font2);
- nf.setFocusable(false);
- jp.add(nf);
- xry = new JButton("\u02B8\u221Ax");
- xry.setBounds(l+4*w+g, 5*h+g, w-2*g, h-2*g);
- xry.setFont(font2);
- xry.setFocusable(false);
- jp.add(xry);
- xr3 = new JButton("\u00B3\u221Ax");
- xr3.setBounds(l+4*w+g, 6*h+g, w-2*g, h-2*g);
- xr3.setFocusable(false);
- xr3.setFont(font2);
- jp.add(xr3);
- tenpx = new JButton("10\u00B2");
- tenpx.setBounds(l+4*w+g, 7*h+g, w-2*g, h-2*g);
- tenpx.setFocusable(false);
- tenpx.setFont(font2);
- jp.add(tenpx);
- }
- void column6Buttons()
- {
- mc = new JButton("MC");
- mc.setBounds(l+5*w+g, 2*h+g, w-2*g, h-2*g);
- mc.setFocusable(false);
- mc.setFont(font2);
- jp.add(mc);
- la = new JButton("\u2190");
- la.setBounds(l+5*w+g, 3*h+g, w-2*g, h-2*g);
- la.setFocusable(false);
- la.setFont(font2);
- jp.add(la);
- seven = new JButton("7");
- seven.setBounds(l+5*w+g, 4*h+g, w-2*g, h-2*g);
- seven.setFont(font2);
- seven.setFocusable(false);
- jp.add(seven);
- four = new JButton("4");
- four.setBounds(l+5*w+g, 5*h+g, w-2*g, h-2*g);
- four.setFont(font2);
- four.setFocusable(false);
- jp.add(four);
- one = new JButton("1");
- one.setBounds(l+5*w+g, 6*h+g, w-2*g, h-2*g);
- one.setFocusable(false);
- one.setFont(font2);
- jp.add(one);
- zero = new JButton("0");
- zero.setBounds(l+5*w+g, 7*h+g, 2*w-2*g, h-2*g);
- zero.setFocusable(false);
- zero.setFont(font2);
- jp.add(zero);
- }
- void column7Buttons()
- {
- mr = new JButton("MR");
- mr.setBounds(l+6*w+g, 2*h+g, w-2*g, h-2*g);
- mr.setFocusable(false);
- mr.setFont(font2);
- jp.add(mr);
- ce = new JButton("CE");
- ce.setBounds(l+6*w+g, 3*h+g, w-2*g, h-2*g);
- ce.setFocusable(false);
- ce.setFont(font2);
- jp.add(ce);
- eight = new JButton("8");
- eight.setBounds(l+6*w+g, 4*h+g, w-2*g, h-2*g);
- eight.setFont(font2);
- eight.setFocusable(false);
- jp.add(eight);
- five = new JButton("5");
- five.setBounds(l+6*w+g, 5*h+g, w-2*g, h-2*g);
- five.setFont(font2);
- five.setFocusable(false);
- jp.add(five);
- two = new JButton("2");
- two.setBounds(l+6*w+g, 6*h+g, w-2*g, h-2*g);
- two.setFocusable(false);
- two.setFont(font2);
- jp.add(two);
- }
- void column8Buttons()
- {
- ms = new JButton("MS");
- ms.setBounds(l+7*w+g, 2*h+g, w-2*g, h-2*g);
- ms.setFocusable(false);
- ms.setFont(font2);
- jp.add(ms);
- C = new JButton("C");
- C.setBounds(l+7*w+g, 3*h+g, w-2*g, h-2*g);
- C.setFocusable(false);
- C.setFont(font2);
- jp.add(C);
- nine = new JButton("9");
- nine.setBounds(l+7*w+g, 4*h+g, w-2*g, h-2*g);
- nine.setFont(font2);
- nine.setFocusable(false);
- jp.add(nine);
- six = new JButton("6");
- six.setBounds(l+7*w+g, 5*h+g, w-2*g, h-2*g);
- six.setFont(font2);
- six.setFocusable(false);
- jp.add(six);
- three = new JButton("3");
- three.setBounds(l+7*w+g, 6*h+g, w-2*g, h-2*g);
- three.setFocusable(false);
- three.setFont(font2);
- jp.add(three);
- dot = new JButton(".");
- dot.setBounds(l+7*w+g, 7*h+g, w-2*g, h-2*g);
- dot.setFocusable(false);
- dot.setFont(font2);
- jp.add(dot);
- }
- void column9Buttons()
- {
- mp = new JButton("M+");
- mp.setBounds(l+8*w+g, 2*h+g, w-2*g, h-2*g);
- mp.setFocusable(false);
- ms.setFont(font2);
- jp.add(mp);
- pm = new JButton("\u00B1");
- pm.setBounds(l+8*w+g, 3*h+g, w-2*g, h-2*g);
- pm.setFocusable(false);
- pm.setFont(font2);
- jp.add(pm);
- div = new JButton("/");
- div.setBounds(l+8*w+g, 4*h+g, w-2*g, h-2*g);
- div.setFont(font2);
- div.setFocusable(false);
- jp.add(div);
- mul = new JButton("*");
- mul.setBounds(l+8*w+g, 5*h+g, w-2*g, h-2*g);
- mul.setFont(font2);
- mul.setFocusable(false);
- jp.add(mul);
- min = new JButton("-");
- min.setBounds(l+8*w+g, 6*h+g, w-2*g, h-2*g);
- min.setFocusable(false);
- min.setFont(font2);
- jp.add(min);
- plus = new JButton("+");
- plus.setBounds(l+8*w+g, 7*h+g, w-2*g, h-2*g);
- plus.setFocusable(false);
- plus.setFont(font2);
- jp.add(plus);
- }
- void column10Buttons()
- {
- mm = new JButton("M-");
- mm.setBounds(l+9*w+g, 2*h+g, w-2*g, h-2*g);
- mm.setFocusable(false);
- mm.setFont(font2);
- jp.add(mm);
- sq = new JButton("\u221A");
- sq.setBounds(l+9*w+g, 3*h+g, w-2*g, h-2*g);
- sq.setFocusable(false);
- sq.setFont(font2);
- jp.add(sq);
- per = new JButton("%");
- per.setBounds(l+9*w+g, 4*h+g, w-2*g, h-2*g);
- per.setFont(font2);
- per.setFocusable(false);
- jp.add(per);
- inv = new JButton("1/x");
- inv.setBounds(l+9*w+g, 5*h+g, w-2*g, h-2*g);
- inv.setFont(font2);
- inv.setFocusable(false);
- jp.add(inv);
- eq = new JButton("=");
- eq.setBounds(l+9*w+g, 6*h+g, w-2*g, 2*h-2*g);
- eq.setFocusable(false);
- eq.setFont(font);
- jp.add(eq);
- }
- }
Add Comment
Please, Sign In to add comment