Java 为什么repaint()不起作用?

Java 为什么repaint()不起作用?,java,swing,Java,Swing,我正在尝试将按钮添加到我创建的中心面板,然后将该面板添加到主中心边框布局。由于某种原因,虽然我的标签将不再重新绘制。不久前,当我将DrawFieldsListener类与MagicSquare放在同一个类文件中时,它工作得很好,但代码中的任何内容都没有改变,因为我将它们拆分为两个类文件。所以我真的不知道发生了什么。以前重新粉刷过,也要花很长时间。有什么帮助吗?谢谢 如果在GitHub上更容易阅读和理解,则项目的所有源代码都位于GitHub上: MagicSquare类: package magi

我正在尝试将按钮添加到我创建的中心面板,然后将该面板添加到主中心边框布局。由于某种原因,虽然我的标签将不再重新绘制。不久前,当我将DrawFieldsListener类与MagicSquare放在同一个类文件中时,它工作得很好,但代码中的任何内容都没有改变,因为我将它们拆分为两个类文件。所以我真的不知道发生了什么。以前重新粉刷过,也要花很长时间。有什么帮助吗?谢谢

如果在GitHub上更容易阅读和理解,则项目的所有源代码都位于GitHub上:

MagicSquare类:

package magicSquare;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MagicSquare extends JPanel {
    JLabel sizeLabel = new JLabel("Enter A Square Size: ");
    JButton setSize;
    static JButton calculate;
    static JButton reset;
    static JTextField squareSize;
    static JTextField field;

    public static ArrayList<JTextField> inputFields = new ArrayList<JTextField>();
    public static ArrayList<Integer> inputs = new ArrayList<Integer>();
    public static ArrayList<Integer> totals = new ArrayList<Integer>();

    public static int squared = 0;
    public static int square = 0;

    public static JPanel centerPanel = new JPanel();
    public static JPanel bottomPanel = new JPanel();

    public MagicSquare (){
        setLayout(new BorderLayout());
        JPanel subPanel = new JPanel();

        subPanel.add(sizeLabel);

        squareSize = new JTextField();
        squareSize.setColumns(6);
        subPanel.add(squareSize);

        setSize = new JButton("Enter");
        subPanel.add(setSize);
        setSize.addActionListener(new DrawFieldsListener());

        add(subPanel, BorderLayout.NORTH);
        add(new DrawFieldsListener(), BorderLayout.CENTER);
    }
}
package magicSquare;
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
公共类MagicSquare扩展了JPanel{
JLabel sizeLabel=新JLabel(“输入正方形大小:”);
JButton设置大小;
静态按钮计算;
静态按钮复位;
静态JTextField平方大小;
静态JTextField字段;
公共静态ArrayList inputFields=新ArrayList();
公共静态ArrayList输入=新建ArrayList();
公共静态ArrayList总计=新ArrayList();
公共静态整数平方=0;
公共静态整数平方=0;
公共静态JPanel中心面板=新JPanel();
public static JPanel bottomPanel=new JPanel();
公共魔法广场(){
setLayout(新的BorderLayout());
JPanel subPanel=新的JPanel();
子面板添加(sizeLabel);
squareSize=新的JTextField();
正方形。设置柱(6);
子面板添加(平方大小);
设置大小=新的JButton(“输入”);
子面板添加(设置大小);
setSize.addActionListener(新的DrawFieldsListener());
添加(子面板,BorderLayout.NORTH);
添加(新的DrawFieldsListener(),BorderLayout.CENTER);
}
}
我的DrawFieldsListener课程:

package magicSquare;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

class DrawFieldsListener extends JPanel implements ActionListener {

    int square = MagicSquare.square;
    int squared = MagicSquare.squared;
    JPanel centerPanel = MagicSquare.centerPanel;
    JTextField squareSize = MagicSquare.squareSize;
    JTextField field = MagicSquare.field;
    ArrayList<JTextField> inputFields = MagicSquare.inputFields;
    JButton calculate = MagicSquare.calculate;
    JButton reset = MagicSquare.reset;
    JPanel bottomPanel = MagicSquare.bottomPanel;

    public void actionPerformed(ActionEvent e){
        square = Integer.parseInt(squareSize.getText());
        squared = square*square;

        centerPanel.setLayout(new GridLayout(square, square));

        for(int i = 0; i < squared; i++){
            field = new JTextField();
            field.setColumns(3);
            inputFields.add(field);
            centerPanel.add(inputFields.get(i));
            System.out.println("DRAWING");
        }

        add(centerPanel, BorderLayout.CENTER);
        System.out.println("ADDING ADDITINOAL BUTTONS");
        additionalButtons();
        System.out.println("ADDED ADDITINOAL BUTTONS");
        System.out.println("REPAINTING");
        repaint();
        System.out.println("REPAINTED");
    }
    public void additionalButtons(){
        calculate = new JButton("Calculate");
        reset = new JButton("Reset");

        bottomPanel.setLayout(new GridLayout(2, 2));
        bottomPanel.add(reset);
        bottomPanel.add(calculate);

        add(bottomPanel, BorderLayout.SOUTH);

        calculate.addActionListener(new CalculateListener());
        reset.addActionListener(new ResetListener());
    }
}
package magicSquare;
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
类DrawFieldsListener扩展JPanel实现ActionListener{
int square=MagicSquare.square;
int squared=MagicSquare.squared;
JPanel centerPanel=MagicSquare.centerPanel;
JTextField squareSize=MagicSquare.squareSize;
JTextField=MagicSquare.field;
ArrayList inputFields=MagicSquare.inputFields;
JButton calculate=MagicSquare.calculate;
JButton reset=MagicSquare.reset;
JPanel bottomPanel=MagicSquare.bottomPanel;
已执行的公共无效操作(操作事件e){
square=Integer.parseInt(squareSize.getText());
平方=平方*平方;
setLayout(新的网格布局(正方形,正方形));
对于(int i=0;i
错误#1 接着是

class DrawFieldsListener extends JPanel implements ActionListener {
    //...
    JPanel centerPanel = MagicSquare.centerPanel;
静态
不是一种跨对象通信机制……现在我不知道谁应该负责管理
中心面板

记住,
static
不是你的朋友,小心它的用法

错误2 您正在创建两个
DrawFieldsListener
(这是一个面板)的实例,一个用作
ActionListener
,另一个用作视图,但哪个实例实际包含
MagicSquare.centerPanel
作为一个组件只能有一个父级

错误3 更改容器后不重新验证容器

    public void actionPerformed(ActionEvent e) {
        square = Integer.parseInt(squareSize.getText());
        squared = square * square;

        centerPanel.setLayout(new GridLayout(square, square));

        for (int i = 0; i < squared; i++) {
            field = new JTextField();
            field.setColumns(3);
            inputFields.add(field);
            centerPanel.add(inputFields.get(i));
            System.out.println("DRAWING");
        }

        add(centerPanel, BorderLayout.CENTER);
        System.out.println("ADDING ADDITINOAL BUTTONS");
        additionalButtons();
        System.out.println("ADDED ADDITINOAL BUTTONS");
        System.out.println("REPAINTING");
        revalidate();
        repaint();
        System.out.println("REPAINTED");
    }

为了更快地获得更好的帮助,请发布一个(最小完整可验证示例)或(简短、自包含、正确的示例)。
public static JPanel centerPanel=new JPanel()
吓坏了我…尤其是当你这样做的时候
JPanel centerPanel=MagicSquare.centerPanel?!顺便说一句,
static
通常不是您试图解决的任何问题的解决方案。您正在创建两个
DrawFieldsListener
实例,这将与您使用的
static
有关,一个用作
ActionLIstener
一个在屏幕上…当触发
ActionLIstener
时,谁知道更新了什么…@MadProgrammer我在下面看到了您的评论,对不起。如果我不调用centerPanel static,那么我无法向其添加任何文本字段。此外,如果我没有在MagicSquare类中创建中心面板,我将无法将其添加到当前的显示边界布局中心。我怎样才能消除静电干扰?谢谢你的帮助,谢谢@AndréFecteau为什么
MagicSquare
需要访问这些静态字段?为什么
DrawFieldsListener
需要访问这些字段中的任何一个?专注于将责任限制在真正应该对这些字段负责的对象上。使用方法和回调在ObjectsObject之间传输信息
setSize.addActionListener(new DrawFieldsListener());

add(subPanel, BorderLayout.NORTH);
add(new DrawFieldsListener(), BorderLayout.CENTER);
    public void actionPerformed(ActionEvent e) {
        square = Integer.parseInt(squareSize.getText());
        squared = square * square;

        centerPanel.setLayout(new GridLayout(square, square));

        for (int i = 0; i < squared; i++) {
            field = new JTextField();
            field.setColumns(3);
            inputFields.add(field);
            centerPanel.add(inputFields.get(i));
            System.out.println("DRAWING");
        }

        add(centerPanel, BorderLayout.CENTER);
        System.out.println("ADDING ADDITINOAL BUTTONS");
        additionalButtons();
        System.out.println("ADDED ADDITINOAL BUTTONS");
        System.out.println("REPAINTING");
        revalidate();
        repaint();
        System.out.println("REPAINTED");
    }
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MagicSquare extends JPanel {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MagicSquare());
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    JLabel sizeLabel = new JLabel("Enter A Square Size: ");
    JButton setSize;
    private JSpinner squareSize;
    JTextField field;

    public MagicSquare() {
        setLayout(new BorderLayout());
        JPanel subPanel = new JPanel();

        subPanel.add(sizeLabel);

        squareSize = new JSpinner();
        subPanel.add(squareSize);

        setSize = new JButton("Enter");
        subPanel.add(setSize);
        DrawFieldsListener dfl = new DrawFieldsListener();
        setSize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int gridSize = (int) squareSize.getValue();
                dfl.makeGrid(gridSize);
            }
        });

        add(subPanel, BorderLayout.NORTH);
        add(dfl, BorderLayout.CENTER);
    }

    class DrawFieldsListener extends JPanel {

        private JButton calculate;
        private JButton reset;

        private ArrayList<JTextField> inputFields = new ArrayList<JTextField>();
        private ArrayList<Integer> inputs = new ArrayList<Integer>();
        private ArrayList<Integer> totals = new ArrayList<Integer>();

        private int squared = 0;
        private int square = 0;

        private JPanel centerPanel = new JPanel();
        private JPanel bottomPanel = new JPanel();

        public void makeGrid(int gridSize) {
            square = gridSize;
            squared = square * square;

            centerPanel.setLayout(new GridLayout(square, square));

            for (int i = 0; i < squared; i++) {
                field = new JTextField();
                field.setColumns(3);
                inputFields.add(field);
                centerPanel.add(inputFields.get(i));
                System.out.println("DRAWING");
            }

            add(centerPanel, BorderLayout.CENTER);
            System.out.println("ADDING ADDITINOAL BUTTONS");
            additionalButtons();
            System.out.println("ADDED ADDITINOAL BUTTONS");
            System.out.println("REPAINTING");
            revalidate();
            repaint();
            System.out.println("REPAINTED");
        }

        public void additionalButtons() {
            calculate = new JButton("Calculate");
            reset = new JButton("Reset");

            bottomPanel.setLayout(new GridLayout(2, 2));
            bottomPanel.add(reset);
            bottomPanel.add(calculate);

            add(bottomPanel, BorderLayout.SOUTH);

//          calculate.addActionListener(new CalculateListener());
//          reset.addActionListener(new ResetListener());
        }
    }
}