这是我的计划: 该语言显然是 Java 。感谢您抽出宝贵的时间来帮助您。我非常感谢您的服务。
该代码用于创建一个计算器,如果我觉得将来有必要,它应具有平方,生根,减法,加法,乘法,除法等功能。
此程序纯粹供个人使用,不可用于商业用途。
//04-11-15
//This program is a JFrame Calculator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calc extends JFrame{
public static void main(String[] acalc){
Calc cal = new Calc();
}
public Calc(){
super("Calculator");
setSize(350,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(10,50);
setResizable(false);
Clanel z = new Clanel();
setContentPane(z);
setVisible(true);
}
}
class Clanel extends JPanel{
Bon bons;
public Clanel(){
setLayout(new BorderLayout());
bons = new Bon();
add(bons,BorderLayout.CENTER);
}
}
class Bon extends JPanel{
Disp dis = new Disp();
Funk fun = new Funk();
Numbers nus = new Numbers();
public Bon(){
setLayout(new BorderLayout());
add(dis,BorderLayout.NORTH);
add(fun,BorderLayout.EAST);
add(nus,BorderLayout.CENTER);
}
class Funk extends JPanel implements ActionListener{
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton square;
private JButton r;
public Funk(){
setLayout(new GridLayout(5,1));
JButton plus = new JButton("+");
plus.addActionListener(this);
JButton minus = new JButton("-");
minus.addActionListener(this);
JButton divide = new JButton("/");
divide.addActionListener(this);
JButton square = new JButton("Square");
square.addActionListener(this);
JButton r = new JButton("Root");
r.addActionListener(this);
add(plus);
add(minus);
add(divide);
add(square);
add(r);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("+")){
}
}
}
class Numbers extends JPanel implements ActionListener{
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton square;
private JButton r;
private JLabel t;
private String number=" ";
public Numbers(){
setLayout(new GridLayout(4,3));
JButton plus = new JButton("0");
plus.addActionListener(this);
JButton minus = new JButton("1");
minus.addActionListener(this);
JButton divide = new JButton("2");
divide.addActionListener(this);
JButton square = new JButton("3");
square.addActionListener(this);
JButton r = new JButton("4");
r.addActionListener(this);
JButton fiv = new JButton("5");
fiv.addActionListener(this);
JButton si = new JButton("6");
si.addActionListener(this);
JButton se = new JButton("7");
se.addActionListener(this);
JButton ei = new JButton("8");
ei.addActionListener(this);
JButton ni = new JButton("9");
ni.addActionListener(this);
JButton clear = new JButton("Clear");
clear.addActionListener(this);
add(plus);
add(minus);
add(divide);
add(square);
add(r);
add(fiv);
add(si);
add(se);
add(ei);
add(ni);
add(clear);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("0")){
number=number + "0";
}
if(e.getActionCommand().equals("Clear")){
number="0";
}
if(e.getActionCommand().equals("1")){
number=number+"1";
}
if(e.getActionCommand().equals("2")){
number=number+"2";
}
if(e.getActionCommand().equals("3")){
number=number+"3";
}
if(e.getActionCommand().equals("4")){
number=number+"4";
}
if(e.getActionCommand().equals("5")){
number=number+"5";
}
if(e.getActionCommand().equals("6")){
number=number+"6";
}
if(e.getActionCommand().equals("7")){
number=number+"7";
}
if(e.getActionCommand().equals("8")){
number=number+"8";
}
if(e.getActionCommand().equals("9")){
number=number+"9"; }
}
}
class Disp extends JPanel implements ActionListener{
private JLabel t;
private String numb=nus.number;
public Disp(){
t = new JLabel(numb);
add(t);
}
public void actionPerformed(ActionEvent e){
}
}
}

答案 0 :(得分:1)
nus
班级尝试访问时, Disp
尚未初始化
定义字段的顺序还定义了它们的初始化顺序
// dis is been initialised BEFORE nus
Disp dis = new Disp();
Funk fun = new Funk();
Numbers nus = new Numbers();
如果您将订单更改为更像......
Numbers nus = new Numbers();
Disp dis = new Disp();
Funk fun = new Funk();
它应该有用。
你必须像对待任何常规变量初始化一样对待它,如果变量A
依赖变量B
中的某些内容,B
必须在A
之前初始化