FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

[Java] Calculator with GUI Pt. 2




Code:

        //Instantiate variables
        display = new JLabel();
        one = new JButton();
        two = new JButton();
          three = new JButton();
        four = new JButton();
        five = new JButton();
        six = new JButton();
        seven = new JButton();
        eight = new JButton();
        nine = new JButton();
        zero = new JButton();

        point = new JButton();
        add = new JButton();
        subtract = new JButton();
        multiply = new JButton();
        divide = new JButton();
        sqrt = new JButton();
        modulus = new JButton();
        equals = new JButton();
        reset = new JButton();

        Border border = LineBorder.createBlackLineBorder();

        //Add north
        display.setText("0");
        display.setFont(new Font("Sans-Serif", Font.BOLD, 36));
        Color color = new Color(191, 187, 126);
        display.setBackground(color);
        display.setOpaque(true);
        display.setPreferredSize(new Dimension(275, 36));
        display.setHorizontalAlignment(JLabel.RIGHT);
        display.setBorder(border);
       
        result.add(display);
        frame.add(result, BorderLayout.NORTH);

        //Add center
        seven.setText("7");
        seven.addActionListener(this);
        fields.add(seven);

        eight.setText("8");
        eight.addActionListener(this);
        fields.add(eight);

        nine.setText("9");
        nine.addActionListener(this);
        fields.add(nine);

        add.setText("+");
        add.addActionListener(this);
        fields.add(add);

        four.setText("4");
        four.addActionListener(this);
        fields.add(four);




1 blog comments below

In stead of doing this
Quote:
Code:
one = new JButton();
one.setText("1");

for every button, why not just do:
Code:
one = new JButton("1");


Saves some lines of code. Smile
adri on Sat Apr 21, 2012 1:59 pm



FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.