简体   繁体   English

使用提供的构造函数和方法仅在main中创建一个对象

[英]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. 我想做的是将所有主要代码放入Calculator方法。 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. 编辑:有条件,因为这是我的Java实验室。

  1. The size of the calculator is 250 x 250 pixels. 计算器的大小为250 x 250像素。
  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. 按钮之间至少应有5像素的间距。
  4. The text on the buttons should be SanSerif size 16 and be bold. 按钮上的文字应为SanSerif 16号,并应为粗体。
  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. 该类的构造函数创建并显示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. 该类必须从JFrame继承,如下所示:public myGUI extended JFrame {…} extends关键字指定继承。 Inside the class you can directly access methods of the JFrame class without specifying an object due to inheritance. 在类内部,您可以直接访问JFrame类的方法,而无需由于继承而指定对象。 So when you want to add something to the frame, simply say add(someComponent); 因此,当您想向框架中添加内容时,只需说一下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!”); 超级(“窗口的标题!”);

First of all you class name should begin with an UpperCase letter Calculator and you can do the following: 首先,您的班级名称应以UpperCase字母Calculator开头,您可以执行以下操作:

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: 它将按预期工作:

在此处输入图片说明

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. 引用您的EDIT只是将方法init的代码放入构造函数中,而在您的main中只是实例化您的类,它将使用this关键字起作用。

 public static void main(String[] args) {

    Calculator c=new Calculator();

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM