分配新变量 (setter) 并调用 repaint() 后无法更改 paintComponent 的输出

Can't change the output of paintComponent after assigned new variable (setter) and called repaint()

我被困在同一个地方将近 1 天了,但我仍然找不到我的代码错误在哪里。需要你们的帮助,这是我的代码。 (有点长)

我的 JFrame class:

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

public class CircleProject extends JFrame implements ActionListener{
    JTextField xTextField1;
    JTextField yTextField1;
    JTextField textFieldRadiusLeft;
    JTextField xTextField2;
    JTextField yTextField2;
    JTextField textFieldRadiusRight;
    JButton redraw;

public static void main(String[] args) {
    CircleProject frame = new CircleProject();
    frame.setResizable(true);
    frame.setVisible(true);
    frame.setSize(580, 700);
    frame.setTitle("YONG JING PING FINAL PROJECT");
}

public CircleProject() {
    JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JLabel theTitle = new JLabel("Two Circles Intersect?");
    JLabel intersection = new JLabel("");
    titlePanel.add(theTitle);
    titlePanel.add(intersection);

    JPanel contentPanel = new JPanel(new GridLayout(2, 1));
    JPanel userInputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JPanel gridPanel = new JPanel(new GridLayout(1, 2, 8, 0));

    JPanel leftInputPanel = new JPanel(new GridLayout(4, 1));
    JLabel inputTitle1 = new JLabel("Enter circle 1 info: ");
    inputTitle1.setHorizontalAlignment(JLabel.CENTER);

    JPanel xInputPanelLeft = new JPanel();
    xTextField1 = new JTextField(5);
    xInputPanelLeft.add(new JLabel("Center X: "));
    xInputPanelLeft.add(xTextField1);

    JPanel yInputPanelLeft = new JPanel();
    yTextField1 = new JTextField(5);
    yInputPanelLeft.add(new JLabel("Center Y: "));
    yInputPanelLeft.add(yTextField1);

    JPanel radiusLeft = new JPanel();
    textFieldRadiusLeft = new JTextField(5);
    radiusLeft.add(new JLabel("Radius: "));
    radiusLeft.add(textFieldRadiusLeft);

    leftInputPanel.add(inputTitle1);
    leftInputPanel.add(xInputPanelLeft);
    leftInputPanel.add(yInputPanelLeft);
    leftInputPanel.add(radiusLeft);
    leftInputPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    JPanel rightInputPanel = new JPanel(new GridLayout(4, 1));
    JLabel inputTitle2 = new JLabel("Enter circle 2 info: ");
    inputTitle2.setHorizontalAlignment(JLabel.CENTER);

    JPanel xInputPanelRight = new JPanel();
    xTextField2 = new JTextField(5);

    xInputPanelRight.add(new JLabel("Center X: "));
    xInputPanelRight.add(xTextField2);

    JPanel yInputPanelRight = new JPanel();
    yTextField2 = new JTextField(5);
    yInputPanelRight.add(new JLabel("Center Y: "));
    yInputPanelRight.add(yTextField2);

    JPanel radiusRight = new JPanel();
    textFieldRadiusRight = new JTextField(5);
    radiusRight.add(new JLabel("Radius: "));
    radiusRight.add(textFieldRadiusRight);

    rightInputPanel.add(inputTitle2);
    rightInputPanel.add(xInputPanelRight);
    rightInputPanel.add(yInputPanelRight);
    rightInputPanel.add(radiusRight);
    rightInputPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    gridPanel.add(leftInputPanel);
    gridPanel.add(rightInputPanel);
    userInputPanel.add(gridPanel);
    contentPanel.add(new DrawPanel());
    contentPanel.add(userInputPanel);

    redraw = new JButton("Redraw Circles");
    redraw.setEnabled(true);
    redraw.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(redraw);

    setLayout(new BorderLayout());
    add(titlePanel, BorderLayout.NORTH);
    add(contentPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

}

DrawPanel drawCircle = new DrawPanel();

@Override
public void actionPerformed(ActionEvent e) {
    this.setVisible(false);
    if (e.getSource() == redraw) {
        CircleProject frame2 = new CircleProject();
        frame2.setResizable(false);
        frame2.setVisible(true);
        frame2.setSize(580,700);
        frame2.setTitle("YONG JING PING FINAL PROJECT");


        String text = xTextField1.getText();
        int x1 = Integer.parseInt(text);

        String text1 = xTextField2.getText();
        int x2 = Integer.parseInt(text1);

        String text2 = yTextField1.getText();
        int y1 = Integer.parseInt(text2);

        String text3 = yTextField2.getText();
        int y2 = Integer.parseInt(text3);

        String text4 = textFieldRadiusLeft.getText();
        int r1 = Integer.parseInt(text4);

        String text5 = textFieldRadiusRight.getText();
        int r2 = Integer.parseInt(text5);

        drawCircle.setCenterColumn1(x1, y1, r1);
        drawCircle.setCenterColumn2(x2, y2, r2);
        }
    }
}

我的拉板Class:

import javax.swing.*;
import java.awt.*;

public class DrawPanel extends JPanel{
    //    the coordinate's variables is the start point of it.
    int coordinateX1=50, coordinateX2=300, coordinateY1=50, coordinateY2=50, radius1=0, radius2=0, length1=200, length2=200;

public void setCenterColumn1(int x, int y, int radius1){
    this.coordinateX1 = x-radius1;
    this.coordinateY1 = y-radius1;
    this.radius1 = radius1;
    length1 = radius1*2;
    System.out.println(coordinateX1);
    System.out.println(coordinateY1);
    System.out.println(length1);
    System.out.println(coordinateX2);
    System.out.println(coordinateY2);
    System.out.println(length2);
    System.out.println("-------1-------");
    repaint();
}
public void setCenterColumn2(int x, int y, int radius2){
    this.coordinateX2=x-radius2;
    this.coordinateY2=y-radius2;
    this.radius2=radius2;
    length2 = radius2*2;
    System.out.println(coordinateX1);
    System.out.println(coordinateY1);
    System.out.println(length1);
    System.out.println(coordinateX2);
    System.out.println(coordinateY2);
    System.out.println(length2);
    System.out.println("-------2-------");
    repaint();
}

public DrawPanel(){
    setBackground(Color.WHITE);
}

//    error from here, when redraw pressed this paintComponent does not
//    show up the new circle.
    @Override
    public void paintComponent (Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);

//        Circle for column 1
        g.drawOval(coordinateX1,coordinateY1,length1,length1);

//        Circle for column 2
        g.drawOval(coordinateX2,coordinateY2,length2,length2);

    System.out.println(coordinateX1);
    System.out.println(coordinateY1);
    System.out.println(length1);
    System.out.println(coordinateX2);
    System.out.println(coordinateY2);
    System.out.println(length2);
    System.out.println("-------3-------");
    }
}

我的预期结果是用户输入圆的中心坐标并单击重绘按钮后圆会发生变化。

问题是你没有使用 never 你的变量 drawCircle (行: DrawPanel drawCircle = new DrawPanel();)

并且当您在 actionPerformed 方法内部使用时,您永远不会使用该变量,因为它创建了 Circle Project 的另一个实例。 (行:CircleProject frame2 = new CircleProject();)

drawCircle变量来自当前class你是运行,所以,当你再次实例化CircleProject时,变量里面的变化不会生效。

要解决此问题,无需再次实例化,您只需使用该变量即可。

您必须在构造函数中做的第一件事 CircleProject():

....
userInputPanel.add(gridPanel);
contentPanel.add(drawCircle);
contentPanel.add(userInputPanel);
....

The change here was only one, because you have contentPanel.add(new DrawPanel()); and that's not correct (as I said, you never are using the variable drawCircle), so, instead, you have to change it for contentPanel.add(drawCircle);

另一方面,为了避免实例化 CircleProject,您必须删除 actionPerformed 方法中的一些行,例如:

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == redraw) {
  
    String text = xTextField1.getText();
    int x1 = Integer.parseInt(text);

    String text1 = xTextField2.getText();
    int x2 = Integer.parseInt(text1);

    String text2 = yTextField1.getText();
    int y1 = Integer.parseInt(text2);

    String text3 = yTextField2.getText();
    int y2 = Integer.parseInt(text3);

    String text4 = textFieldRadiusLeft.getText();
    int r1 = Integer.parseInt(text4);

    String text5 = textFieldRadiusRight.getText();
    int r2 = Integer.parseInt(text5);

    drawCircle.setCenterColumn1(x1, y1, r1);
    drawCircle.setCenterColumn2(x2, y2, r2);
    }
}

我试过这个程序,它对我有用!我希望这对你也有用 :D