JAVA - 类和它的对象,超级

时间:2015-07-23 08:49:37

标签: java user-interface object interface

这是班贵。

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;

public class Gui extends JFrame{
    Principal obiect;
    public JButton heads = new JButton("Heads");
    public JButton tails = new JButton("Tails");
    public static JTextField display;
    public JTextField comboul;
    public JPanel panel = new JPanel();
    public int predictie;


    public Gui(){
        super("HeadsOrTails");
        panel.setLayout(new BorderLayout(100, 100));
        panel.add(heads,BorderLayout.LINE_START);
        panel.add(tails,BorderLayout.LINE_END);
        panel.add(display,BorderLayout.CENTER); //HERE IS ERROR
        panel.add(comboul,BorderLayout.PAGE_START);
    }


    public void dacaHeads(){
        if(heads.getModel().isPressed()) predictie = 0;
    }
    public void dacaTails(){
        if(tails.getModel().isPressed()) predictie = 1;



        heads.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
               dacaHeads();
               obiect.flip();

               if(predictie == obiect.returnStatus() ){
                   String s = comboul.getText();
                   int combo = Integer.valueOf(s);
                   s = Integer.toString(++combo);
                   comboul.setText(s);}
                   else{
                       String z = "0";
                       comboul.setText(z);
                   }
            }
        });
        tails.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                dacaTails();
                obiect.flip();
                if(predictie == obiect.returnStatus() ){
                    String s = comboul.getText();
                    int combo = Integer.valueOf(s);
                    s = Integer.toString(++combo);
                    comboul.setText(s);}
                else{
                        String z = "0";
                comboul.setText(z);
            }
        }
        });



    }}

这是班主任。

 import javax.swing.*;
import java.util.Random;
    public class Principal extends Gui {
    public int combo;
    public static Random bulion = new Random();
    public static boolean sansa;
    public static boolean input;
    public int status;

    //STATUS 0 = HEADS;
    //STATUS 1 = TAILS;

        public static void main(String[] args) {
            Gui lee = new Gui(); //HERE IS ERROR
            Principal obiect = new Principal();
            lee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lee.setSize(500,500);
            lee.setVisible(true);
        }

        public int flip(){
        boolean sansa2 ;
        sansa2 = bulion.nextBoolean();
        if(sansa2){
           status = 0;
            display.setText("Heads");
        }
        else{
            status = 1;
            display.setText("Tails");
        }
        return status;

    }

     public int returnStatus(){
         return status;
     }
}

这些是错误:     固定:     //线程“main”java.lang.StackOverflowError中的异常     //在Gui。(Gui.java:19)//超级     //在Principal。(Principal.java:3)//公共类Principal扩展Gui     //在Gui。(Gui.java:9)// Principal obiect = new Principal()

NOW:
Exception in thread "main" java.lang.NullPointerException
at Gui.<init>(Gui.java:23)
at Principal.main(Principal.java:14)

这是我的第一篇帖子,如果您不理解,我很抱歉。如果您需要,我会发布更多信息。

1 个答案:

答案 0 :(得分:5)

当您创建Gui实例时,它会创建一个Principal实例(因为Gui类具有Prinicipal实例变量 - Principal obiect = new Principal();),这是也是一个Gui实例,因此你得到一个无限的构造函数调用链,导致StackOverflowError

避免改变

Principal obiect = new Principal(); 

Principal obiect; 

并在创建Gui实例后(即在主方法中调用Gui lee = new Gui();之后)初始化此实例。

例如,添加到Gui类:

public void setPrincipal (Principal object)
{
    this.object = object;
}

在你的主要:

Gui lee = new Gui();
lee.setPrincipal (new Principal());