gpt4 book ai didi

JAVA - 类及其对象, super

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:10:29 25 4
gpt4 key购买 nike

这是 Gui 类。

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 //at Gui.(Gui.java:19)// super //at Principal.(Principal.java:3)//public class Principal extends Gui //at 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)

这是我的第一篇文章,如果您不明白,我很抱歉。如果您需要,我会发布更多信息。

最佳答案

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

避免改变

Principal obiect = new Principal(); 

Principal obiect; 

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

例如,添加到Gui类:

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

在你的主要部分:

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

关于JAVA - 类及其对象, super ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31582463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com