[英]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实验室。
首先,您的班级名称应以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.