我很喜欢使用swing,我有一个问题。我的代码今天早上停止了工作,但我不知道为什么。我运行调试器,它说2行代码。一个在主类,一个在另一个。他们都在创造彼此的实例。
线条是这样的: 主类:
OnePlayerFrame OnePFrame = new OnePlayerFrame();
OnePlayerFrame类:
MainFrame MainClass = new MainFrame();
我希望你能理解我所说的话,正如我所说,我是一个很擅长摇摆的人。
由于 -Matt
好的,这是完整的事情: 是的,忘了提,我可能已经删除了一行简单的代码而忽略了它,所以请保持冷静,如果它很简单:)
主类:
package rockpaperscissors;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainFrame {
public JFrame frame = new JFrame();
private final ImageIcon rock = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/rock.jpg");
private final ImageIcon paper = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/paper.png");
private final ImageIcon scissors = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/scissors.png");
private final JLabel rockLabel = new JLabel();
private final JLabel paperLabel = new JLabel();
private final JLabel scissorsLabel = new JLabel();
private final JPanel panel1 = new JPanel();
private final JPanel panel2 = new JPanel();
private final JPanel panel3 = new JPanel();
private final JPanel empty1 = new JPanel();
private final JPanel empty2 = new JPanel();
private final JPanel empty3 = new JPanel();
private final JPanel empty4 = new JPanel();
private final JPanel empty5 = new JPanel();
OnePlayerFrame OnePlayerFrame = new OnePlayerFrame();
public JButton btn1 = new JButton("1 Player");
private final JTextField text = new JTextField();
private final Font font = new Font("Showcard Gothic Regular", Font.BOLD, 28);
public MainFrame(){
frame.setSize(360, 300);
frame.setAlwaysOnTop(false);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.setLayout(new GridLayout(3, 1));
panel1.setLayout(new GridLayout(1, 3));
panel1.add(rockLabel);
panel1.add(paperLabel);
panel1.add(scissorsLabel);
panel1.setBackground(Color.WHITE);
panel2.setLayout(new GridLayout(2, 3));
panel2.add(empty1);
panel2.add(empty2);
panel2.add(empty3);
panel2.add(empty4);
panel2.add(btn1);
panel2.add(empty5);
panel3.add(text);
panel3.setBackground(Color.WHITE);
empty1.setBackground(Color.WHITE);
empty2.setBackground(Color.WHITE);
empty3.setBackground(Color.WHITE);
empty4.setBackground(Color.WHITE);
empty5.setBackground(Color.WHITE);
text.setText("Rock Paper Scissors");
text.setFont(font);
text.setPreferredSize(new Dimension(360, 80));
text.setEditable(false);
text.setBorder(null);
text.setHorizontalAlignment(JTextField.CENTER);
text.setBackground(Color.WHITE);
rockLabel.setIcon(rock);
paperLabel.setIcon(paper);
scissorsLabel.setIcon(scissors);
btn1.setFocusPainted(false);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frame.dispose();
OnePlayerFrame.startGame1();
}
});
}
public static void main(String[] args) {
new MainFrame();
}
}
这是第二堂课:
package rockpaperscissors;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class OnePlayerFrame {
MainFrame MainClass = new MainFrame();
JFrame gameFrame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel empty1 = new JPanel();
JPanel empty2 = new JPanel();
JTextField playerText1 = new JTextField();
JTextField playerText2 = new JTextField();
JTextField playerText3 = new JTextField();
JTextField compText1 = new JTextField();
JTextField compText2 = new JTextField();
JTextField compText3 = new JTextField();
JTextField pChoose = new JTextField();
JTextField cChoose = new JTextField();
JButton btn1 = new JButton("Rock");
JButton btn2 = new JButton("Paper");
JButton btn3 = new JButton("Scissors");
JButton btn4 = new JButton("Confirm");
JButton btn5 = new JButton("Fight!");
JButton getB = new JButton();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
boolean confirmed = false;
boolean hasSelected = false;
boolean runThread = true;
String playerMove;
ImageIcon playerIcon = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/question-mark.jpg");
ImageIcon computerIcon = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/question-mark.jpg");
Font font = new Font("Showcard Gothic Regular", Font.BOLD, 18);
public OnePlayerFrame(){
gameFrame.setVisible(true);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setSize(500, 400);
gameFrame.setLocationRelativeTo(MainClass.frame);
gameFrame.setLayout(new GridLayout(3, 1));
gameFrame.add(panel1);
gameFrame.add(panel2);
gameFrame.add(panel3);
MainClass.frame.dispose();
panel1.setBackground(Color.WHITE);
panel1.setLayout(new GridLayout(1, 4));
panel1.add(panel4);
panel1.add(panel5);
panel1.add(panel6);
panel1.add(panel7);
panel2.setBackground(Color.WHITE);
panel2.setLayout(new GridLayout(3, 1));
panel2.add(pChoose);
panel2.add(panel8);
panel2.add(btn4);
panel3.setBackground(Color.WHITE);
panel3.add(cChoose);
panel3.add(panel9);
panel3.setLayout(new GridLayout(2, 1));
panel4.setBackground(Color.WHITE);
panel4.add(playerText1);
panel4.add(playerText2);
panel4.add(playerText3);
panel4.setLayout(new GridLayout(3, 1));
panel5.setBackground(Color.WHITE);
panel5.add(label1);
panel6.setBackground(Color.WHITE);
panel6.add(label2);
panel7.setBackground(Color.WHITE);
panel7.add(compText1);
panel7.add(compText2);
panel7.add(compText3);
panel7.setLayout(new GridLayout(3, 1));
panel8.setLayout(new GridLayout(1, 3));
panel8.add(btn1);
panel8.add(btn2);
panel8.add(btn3);
panel9.setLayout(new GridLayout(1, 3));
panel9.setBackground(Color.WHITE);
panel9.add(empty1);
panel9.add(btn5);
panel9.add(empty2);
empty1.setBackground(Color.WHITE);
empty2.setBackground(Color.WHITE);
playerText1.setEditable(false);
playerText1.setBorder(null);
playerText1.setText("Player");
playerText1.setBackground(Color.WHITE);
playerText1.setFont(font);
playerText1.setHorizontalAlignment(JTextField.CENTER);
playerText2.setEditable(false);
playerText2.setBorder(null);
playerText2.setText("Chose");
playerText2.setBackground(Color.WHITE);
playerText2.setFont(font);
playerText2.setHorizontalAlignment(JTextField.CENTER);
playerText3.setEditable(false);
playerText3.setBorder(null);
playerText3.setText("====>");
playerText3.setBackground(Color.WHITE);
playerText3.setFont(font);
playerText3.setHorizontalAlignment(JTextField.CENTER);
compText1.setEditable(false);
compText1.setBorder(null);
compText1.setText("Computer");
compText1.setBackground(Color.WHITE);
compText1.setFont(font);
compText1.setHorizontalAlignment(JTextField.CENTER);
compText2.setEditable(false);
compText2.setBorder(null);
compText2.setText("Chose");
compText2.setBackground(Color.WHITE);
compText2.setFont(font);
compText2.setHorizontalAlignment(JTextField.CENTER);
compText3.setEditable(false);
compText3.setBorder(null);
compText3.setText("<====");
compText3.setBackground(Color.WHITE);
compText3.setFont(font);
compText3.setHorizontalAlignment(JTextField.CENTER);
pChoose.setEditable(false);
pChoose.setBorder(null);
pChoose.setText("Choose your move");
pChoose.setFont(font);
pChoose.setHorizontalAlignment(JTextField.CENTER);
pChoose.setBackground(Color.WHITE);
cChoose.setEditable(false);
cChoose.setBorder(null);
cChoose.setText("Computer is choosing");
cChoose.setFont(font);
cChoose.setHorizontalAlignment(JTextField.CENTER);
cChoose.setBackground(Color.WHITE);
btn1.setRolloverEnabled(true);
btn1.setFocusPainted(false);
btn2.setFocusPainted(false);
btn2.setRolloverEnabled(true);
btn3.setFocusPainted(false);
btn3.setRolloverEnabled(true);
btn4.setFocusPainted(false);
btn4.setRolloverEnabled(true);
btn5.setFocusPainted(false);
btn5.setRolloverEnabled(true);
btn5.setVisible(false);
btn5.setFont(font);
label1.setIcon(playerIcon);
label2.setIcon(computerIcon);
}
public void startGame1(){
new Thread(new Runnable() {
public void run() {
while(confirmed == true || confirmed == false){
if(confirmed == false){
try{
if(confirmed == false){
cChoose.setText("Computer is choosing");
Thread.sleep(500);
}
if(confirmed == false){
cChoose.setText("Computer is choosing.");
Thread.sleep(500);
}
if(confirmed == false){
cChoose.setText("Computer is choosing..");
Thread.sleep(500);
}
if(confirmed == false){
cChoose.setText("Computer is choosing...");
Thread.sleep(500);
}
}
catch(Exception e){
gameFrame.dispose();
}
}
if(confirmed == true){
cChoose.setText("Computer has chosen!");
fightButtonSet();
}
}
}
}).start();
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(confirmed == false){
playerMove = btn1.getText();
hasSelected = true;
pChoose.setText("You have selected rock");
getB = btn1;
}
else{
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(confirmed == false){
playerMove = btn2.getText();
hasSelected = true;
pChoose.setText("You have selected paper");
getB = btn2;
}
else{
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(confirmed == false){
playerMove = btn3.getText();
hasSelected = true;
pChoose.setText("You have selected scissors");
getB = btn3;
}
else{
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
});
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(hasSelected == true && confirmed == false){
confirmed = true;
pChoose.setText("You have confirmed " + getB.getText().toLowerCase() + "!");
cChoose.setText("Computer has chosen!");
}
else{
if(hasSelected == false){
pChoose.setText("You haven't selected anything yet!");
}
else if (confirmed == true){
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
}
});
}
public void fightButtonSet(){
btn5.setVisible(true);
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
FIGHT();
}
});
}
public void FIGHT(){
if(getB.getText().equalsIgnoreCase("Rock")){
}
if(getB.getText().equalsIgnoreCase("Paper")){
}
if(getB.getText().equalsIgnoreCase("Scissors")){
}
}
}
答案 0 :(得分:1)
我们可以抽象一点你有这个。
public class TestCircularDependency {
static class A{
B b = new B();
}
static class B{
A a = new A();
}
public static void main(String args[]){
A a = new A();
}
}
这将抛出StackOverflowError
。你有一个循环依赖。问题是你是构造函数中的每个实例,然后你有无限递归。
在java中,您可以拥有循环依赖,但不能创建对象。你可以这样做。
public class TestCircularDependency {
static class A{
B b;
}
static class B{
A a;
}
public static void main(String args[]){
A a = new A();
a.b = new B();
b.a= a;
}
}