繁体   English   中英

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

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

这只是一个计算器的显示。 我想做的是将所有主要代码放入Calculator方法。 只有离开

new calculator(); 

作为主要在创建框架的对象。 我试图将所有内容都向上移动,但是我会得到一个错误,我从主要方面无法理解。 有人介意帮我吗?

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);
 }

 }

编辑:有条件,因为这是我的Java实验室。

  1. 计算器的大小为250 x 250像素。
  2. 计算器按钮的背景色和前景色必须以棋盘格图案交替显示,如上所示。 您可以选择任何一对颜色作为前景色和背景色。
  3. 按钮之间至少应有5像素的间距。
  4. 按钮上的文字应为SanSerif 16号,并应为粗体。
  5. 您的应用程序应在单个类中实现。 类的主要方法只不过是创建类的对象而已。 该类的构造函数创建并显示GUI。 如果需要,构造函数可以调用该类的其他方法。
  6. 该类必须从JFrame继承,如下所示:public myGUI extended JFrame {…} extends关键字指定继承。 在类内部,您可以直接访问JFrame类的方法,而无需由于继承而指定对象。 因此,当您想向框架中添加内容时,只需说一下add(someComponent);即可。 您只需在构造函数中首先添加以下行,即可在构造函数中指定窗口的标题。 超级(“窗口的标题!”);

首先,您的班级名称应以UpperCase字母Calculator开头,您可以执行以下操作:

像您一样实现类的构造函数。

编写一个方法,该方法将使用您的主代码,并进行一些更改,如下面的代码所示:

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);

}

在您的主体中执行以下操作:

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();

}

它将按预期工作:

在此处输入图片说明

编辑:

引用您的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