Getter Setter不会给出变量

问题描述 投票:-2回答:1

所以我想编写一个tictactoe,当我创建吸气剂和吸气剂时,他们没有给我变量。我查了一下它们可能有什么问题,但没有找到错误。我希望有人能向我解释什么地方错了。它只是关于设置面板并将其取回并设置字符串并将其取回。我是编程新手,如果有人可以帮助我解决这个问题,我将不胜感激。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * @author Anid Memisi
 *
 * @date 21.02.2020
 *
 * @version 1.0
 */

public class TicTacToe {

    public void tictactoe() {
        //basic constructor
            TicTacToe t = new TicTacToe();
            t.GridLayout();
        }

    public void Entscheid() {
        TicTacToe t = new TicTacToe();
        t.Update();
    }

    public void Update() {
        Setter_and_Getter s = new Setter_and_Getter();
        String feldI=s.getFeldi();
        JPanel panel=s.getPanel();
        System.out.print(feldI);
        System.out.print(panel);
        String[] feld = new String[] {" "," "," "," "," "," "," "," "," "};
        feld[0] = feldI;
        System.out.print(feldI);
        if(!feldI.equals(" ")){
            if(panel != null) {
                panel.removeAll();
                panel.invalidate();
                panel.repaint();
            }
            panel.setLayout(new GridLayout(3, 3));
            //creates the tiles for the tictactoe
            JButton button1 = new JButton(feld[0]);
            JButton button2 = new JButton(feld[1]);
            JButton button3 = new JButton(feld[2]);
            JButton button4 = new JButton(feld[3]);
            JButton  button5 = new JButton(feld[4]);
            JButton button6 = new JButton(feld[5]);
            JButton button7 = new JButton(feld[6]);
            JButton  button8 = new JButton(feld[7]);
            JButton button9 = new JButton(feld[8]);
            panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            panel.add(button1);
            panel.add(button2);
            panel.add(button3);
            panel.add(button4);
            panel.add(button5);
            panel.add(button6);
            panel.add(button7);
            panel.add(button8);
            panel.add(button9);
            panel.revalidate();
            panel.repaint();
            panel.requestFocus();
        }
    }

    public void GridLayout() {
        TicTacToe t = new TicTacToe();
        Setter_and_Getter s = new Setter_and_Getter();

        String[] feld = new String[] {" "," "," "," "," "," "," "," "," "};

        JFrame frame = new JFrame("Grid Layout");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        JPanel panel = new JPanel();
        //creates the tiles for the tictactoe
        panel.setLayout(new GridLayout(3,3));
        JButton button1 = new JButton(feld[0]);
        JButton button2 = new JButton(feld[1]);
        JButton button3 = new JButton(feld[2]);
        JButton button4 = new JButton(feld[3]);
        JButton button5 = new JButton(feld[4]);
        JButton button6 = new JButton(feld[5]);
        JButton button7 = new JButton(feld[6]);
        JButton button8 = new JButton(feld[7]);
        JButton button9 = new JButton(feld[8]);
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        frame.add(panel);

        s.setPanel(panel);


        //sends the input to the listener
        DerHandler handler = new DerHandler();
        button1.addActionListener(handler);

        DerHandler2 handler2 = new DerHandler2();
        button2.addActionListener(handler2);

        DerHandler3 handler3 = new DerHandler3();
        button3.addActionListener(handler3);

        DerHandler4 handler4 = new DerHandler4();
        button4.addActionListener(handler4);

        DerHandler5 handler5 = new DerHandler5();
        button5.addActionListener(handler5);

        DerHandler6 handler6 = new DerHandler6();
        button6.addActionListener(handler6);

        DerHandler7 handler7 = new DerHandler7();
        button7.addActionListener(handler7);

        DerHandler8 handler8 = new DerHandler8();
        button8.addActionListener(handler8);

        DerHandler9 handler9 = new DerHandler9();
        button9.addActionListener(handler9);
        }
 private class DerHandler implements ActionListener {

     @Override
     public void actionPerformed(ActionEvent e) {
         TicTacToe t = new TicTacToe();
         Setter_and_Getter s = new Setter_and_Getter();
         String feldI =" ";
         //gets the text of the tile
         Object source = e.getSource();
         //compares it
         if (source instanceof JButton) {
             feldI = "1";
         }
         s.setFeldI(feldI);
         System.out.print(feldI);
         t.Entscheid();
     }
 }

    private class DerHandler2 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "2";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler3 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "3";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler4 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "4";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler5 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "5";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler6 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "6";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler7 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "7";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler8 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "8";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }
    private class DerHandler9 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            TicTacToe t = new TicTacToe();
            Setter_and_Getter s = new Setter_and_Getter();
            String feldI =" ";
            //gets the text of the tile
            Object source = e.getSource();
            //compares it
            if (source instanceof JButton) {
                feldI = "9";
            }
            s.setFeldI(feldI);
            System.out.print(feldI);
            t.Entscheid();
        }
    }

public static void main(String[] args) {
    TicTacToe t = new TicTacToe();
        t.tictactoe();
        }
}
import javax.swing.*;

public class Setter_and_Getter {

    private String feldI;
    private JPanel panel;

    public void setFeldI(String feldI){
        this.feldI=feldI;
    }

    public void setPanel(JPanel panel){
        this.panel=panel;
    }

    public String getFeldi(){
        return this.feldI;
    }

    public JPanel getPanel(){
        return this.panel;
    }
}
java swing getter-setter
1个回答
0
投票

在每个actionPerformed()方法中,创建Setter_and_Getter的新实例并设置一个值。在这些方法的最后,方法的作用域结束了,因此Setter_and_Getter对象被垃圾收集器删除。

因此,以后没有办法访问这些对象实例。

在您的Update()方法中,再次创建一个Setter_and_Getter的新实例,并读取两个重视。吸气剂返回null,因为这些值实际上为null。您之前没有调用该对象实例的任何设置方法。

您需要将对象实例存储在方法之外的某个位置,然后从以前填充的实例中读取,而不是读取一个新的空实例。

顺便说一句:您应该对TicTacToe类的所有方法使用小写字母名称。大写名称按照惯例指示类名称。

© www.soinside.com 2019 - 2024. All rights reserved.