Lunarblaz3 Lunarblaz3 - 24 days ago 4
Java Question

Creating just an object in main from using the constructor and method that is provided

This is just an display for a calculator. What im trying to do is place all the main code into the calculator method. which only leaving

new calculator();


as the object of the frame being created in the main.
I tried to move everything up but i would get an error that i would not understand from the main. Anyone mind helping me out?

import java.awt.*;
import javax.swing.*;


public class calculator extends JFrame {



public calculator() {

super("Calculator");

setLayout(new BorderLayout());
setSize(new Dimension(250,250));

}

public static void main(String[] args) {


calculator c = new calculator();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
GridLayout gl = new GridLayout(4,4,5,5);


JButton b1 = new JButton("7");
JButton b2 = new JButton("8");
JButton b3 = new JButton("9");
JButton b4 = new JButton("/");

JButton b5 = new JButton("4");
JButton b6 = new JButton("5");
JButton b7 = new JButton("6");
JButton b8 = new JButton("*");

JButton b9 = new JButton("1");
JButton b10 = new JButton("2");
JButton b11 = new JButton("3");
JButton b12 = new JButton("-");

JButton b13 = new JButton("0");
JButton b14 = new JButton(".");
JButton b15 = new JButton("=");
JButton b16 = new JButton("+");


panel2.add(b1);
b1.setBackground(Color.white);
b1.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b2);
b2.setBackground(Color.BLACK);
b2.setFont(new Font("Sans Serif", Font.BOLD, 16));
b2.setForeground(Color.WHITE);
panel2.add(b3);
b3.setBackground(Color.white);
b3.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b4);
b4.setBackground(Color.BLACK);
b4.setFont(new Font("Sans Serif", Font.BOLD, 16));
b4.setForeground(Color.WHITE);


panel2.add(b5);
b5.setBackground(Color.BLACK);
b5.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b6);
b5.setForeground(Color.WHITE);
b6.setBackground(Color.white);
b6.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b7);
b7.setBackground(Color.BLACK);
b7.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b8);
b7.setForeground(Color.WHITE);
b8.setBackground(Color.white);
b8.setFont(new Font("Sans Serif", Font.BOLD, 16));

panel2.add(b9);
b9.setBackground(Color.white);
b9.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b10);
b10.setBackground(Color.BLACK);
b10.setFont(new Font("Sans Serif", Font.BOLD, 16));
b10.setForeground(Color.WHITE);
panel2.add(b11);
b11.setBackground(Color.white);
b11.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b12);
b12.setBackground(Color.BLACK);
b12.setFont(new Font("Sans Serif", Font.BOLD, 16));
b12.setForeground(Color.WHITE);

panel2.add(b13);
b13.setBackground(Color.BLACK);
b13.setFont(new Font("Sans Serif", Font.BOLD, 16));
b13.setForeground(Color.WHITE);
panel2.add(b14);
b14.setBackground(Color.white);
b14.setFont(new Font("Sans Serif", Font.BOLD, 16));
panel2.add(b15);
b15.setBackground(Color.BLACK);
b15.setFont(new Font("Sans Serif", Font.BOLD, 16));
b15.setForeground(Color.WHITE);
panel2.add(b16);
b16.setBackground(Color.white);
b16.setFont(new Font("Sans Serif", Font.BOLD, 16));




panel1.add(new JTextField(20));
panel2.setLayout(gl);

c.add(panel1,BorderLayout.NORTH);
c.add(panel2,BorderLayout.CENTER);
c. setVisible(true);
}

}


EDIT: There are conditions since this is for my java lab.


  1. The size of the calculator is 250 x 250 pixels.

  2. The background and foreground color of the calculator buttons must alternate in a checker board pattern as shown above. You can choose any pair of colors for your foreground and background colors.

  3. The buttons should have at least 5 pixels of space between them.

  4. The text on the buttons should be SanSerif size 16 and be bold.

  5. Your application should be implemented in a single class. The main method of the class does nothing more than create an object of the class. The constructor of the class creates and displays the GUI. The constructor may call other methods of the class if needed.

  6. The class must inherit from JFrame as the following demonstrates:
    public myGUI extends JFrame { … }
    The extends keyword specifies inheritance. Inside the class you can directly access methods of the JFrame class without specifying an object due to inheritance. So when you want to add something to the frame, simply say
    add(someComponent);
    You can specify the title of the window in your constructor by simply adding the following line as the first thing in your constructor.
    super(“Title of your window!”);


Answer

First of all you class name should begin with an UpperCase letter Calculator and you can do the following:

Implement the constructor of your class like you did.

Write a method that takes all the code you have on your main and make some changes like in the code below:

public void init() {

 //Don't instantiate your class here
 JPanel panel1 = new JPanel();
 JPanel panel2 = new JPanel();
 GridLayout gl = new GridLayout(4,4,5,5);


 JButton b1 = new JButton("7");
 JButton b2 = new JButton("8");
 JButton b3 = new JButton("9");
 JButton b4 = new JButton("/");

 JButton b5 = new JButton("4");
 JButton b6 = new JButton("5");
 JButton b7 = new JButton("6");
 JButton b8 = new JButton("*");

 JButton b9 = new JButton("1");
 JButton b10 = new JButton("2");
 JButton b11 = new JButton("3");
 JButton b12 = new JButton("-");

 JButton b13 = new JButton("0");
 JButton b14 = new JButton(".");
 JButton b15 = new JButton("=");
 JButton b16 = new JButton("+");


 panel2.add(b1);
 b1.setBackground(Color.white);
 b1.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b2);
 b2.setBackground(Color.BLACK);
 b2.setFont(new Font("Sans Serif", Font.BOLD, 16));
 b2.setForeground(Color.WHITE);
 panel2.add(b3);
 b3.setBackground(Color.white);
 b3.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b4);
 b4.setBackground(Color.BLACK);
 b4.setFont(new Font("Sans Serif", Font.BOLD, 16));
 b4.setForeground(Color.WHITE);


 panel2.add(b5);
 b5.setBackground(Color.BLACK);
 b5.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b6);
 b5.setForeground(Color.WHITE);
 b6.setBackground(Color.white);
 b6.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b7);
 b7.setBackground(Color.BLACK);
 b7.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b8);
 b7.setForeground(Color.WHITE);
 b8.setBackground(Color.white);
 b8.setFont(new Font("Sans Serif", Font.BOLD, 16));

 panel2.add(b9);
 b9.setBackground(Color.white);
 b9.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b10);
 b10.setBackground(Color.BLACK);
 b10.setFont(new Font("Sans Serif", Font.BOLD, 16));     
 b10.setForeground(Color.WHITE);
 panel2.add(b11);
 b11.setBackground(Color.white);
 b11.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b12);
 b12.setBackground(Color.BLACK);
 b12.setFont(new Font("Sans Serif", Font.BOLD, 16));
 b12.setForeground(Color.WHITE);

 panel2.add(b13);
 b13.setBackground(Color.BLACK);
 b13.setFont(new Font("Sans Serif", Font.BOLD, 16));
 b13.setForeground(Color.WHITE);
 panel2.add(b14);
 b14.setBackground(Color.white);
 b14.setFont(new Font("Sans Serif", Font.BOLD, 16));
 panel2.add(b15);
 b15.setBackground(Color.BLACK);
 b15.setFont(new Font("Sans Serif", Font.BOLD, 16));
 b15.setForeground(Color.WHITE);
 panel2.add(b16);
 b16.setBackground(Color.white);
 b16.setFont(new Font("Sans Serif", Font.BOLD, 16));

 panel1.add(new JTextField(20));
 panel2.setLayout(gl);

 //Replace the c instance with the keyword this
 this.add(panel1,BorderLayout.NORTH);
 this.add(panel2,BorderLayout.CENTER);
 this. setVisible(true);

}

And in your main do the following :

public static void main(String[] args) {
    //Instantiate your class and the constructor code will be executed
       Calculator c=new Calculator();
    //Then call your implemented init() method 
       c.init();

}

And it will work as Expected:

enter image description here

EDIT:

Referring to your EDIT just put the code of the method init in the constructor and in your main just instantiate your class, and it will work using the this keyword.

 public static void main(String[] args) {

    Calculator c=new Calculator();

 }