Swing Java重绘不使用2个jpanels

时间:2023-02-09 15:33:37

As you can see from the code I am expecting to see a rectangle on DrawingPanel but I am not sure why repaint() method is not working. Any help will be much appreciated. I also tried revalidate method but that is not working either.

从代码中我可以看到我期望在DrawingPanel上看到一个矩形,但我不确定为什么repaint()方法不起作用。任何帮助都感激不尽。我也尝试了revalidate方法,但这也没有用。

Here are my Classes:

这是我的课程:

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

public class Designer extends JFrame {

static JFrame f = new Designer();

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel();

 public Designer(){

    Rectangle bounds = GraphicsEnvironment
            .getLocalGraphicsEnvironment()
            .getMaximumWindowBounds();

    splitPane.setTopComponent(propertyPanel);
    splitPane.setBottomComponent(drawingPanel);

    add(splitPane, BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(bounds);
    setVisible(true);

 }

 public static void main(String[] args) {
    System.out.println("App started...");
 }
}

Class for first Jpanel: PropertyPanel.Java

第一个Jpanel的类:PropertyPanel.Java

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

public class PropertyPanel extends JPanel {

  private DrawingPanel drawingPanel = new DrawingPanel();

  public PropertyPanel(){

    JButton addShapesBtn = new JButton("Add");

    addShapesBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            drawingPanel.addRectangle();
        }
    });
    add(addShapesBtn);
  }

}

Class for second Jpanel: DrawingPanel.Java

第二个Jpanel的类:DrawingPanel.Java

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class DrawingPanel extends JPanel{

  private List<Rectangle> squares = new ArrayList<Rectangle>();


 @Override
 protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    System.out.println("painting");

    for (Rectangle rect : squares) {
        g2.draw(rect);
    }

}


  public void addRectangle() {

    Rectangle rectx = new Rectangle(10, 10, 100, 100);
    squares.add(rectx);
    repaint(); <-- this is not working
  }
}

1 个解决方案

#1


2  

Your repaint works fine. Your problem is that you're calling addRectangle() to the wrong DrawingPanel instance, not the one that is currently displayed in the GUI. To solve this, pass a reference from the displayed one to the code that needs to call the method.

你的重画工作正常。您的问题是您正在将addRectangle()调用到错误的DrawingPanel实例,而不是当前在GUI中显示的实例。要解决此问题,请将显示的引用传递给需要调用该方法的代码。

To see that this is correct, simply search this page for new DrawingPanel(). You should see this only once in your own code (and your code posted). You see it twice meaning you've created two instances of this.

要查看这是正确的,只需在此页面中搜索新的DrawingPanel()。您应该只在自己的代码中看到一次(并且您的代码已发布)。你看到它两次意味着你已经创建了两个这样的实例。

e.g., change this:

例如,改变这个:

public class PropertyPanel extends JPanel {   
    private DrawingPanel drawingPanel = new DrawingPanel();

    public PropertyPanel(){

to this:

public class PropertyPanel extends JPanel {    
    private DrawingPanel drawingPanel;  // don't create new

    public PropertyPanel(DrawingPanel drawingPanel){
        this.drawingPanel = drawingPanel;

and then pass in the true DrawingPanel into this object on creation:

然后在创建时将真正的DrawingPanel传递给此对象:

DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel(drawingPanel);

Better still: structure your code in a M-V-C way, and use dependency injection to make connections -- but this may be overkill for your simple GUI.

更好的是:以M-V-C方式构建代码,并使用依赖注入来建立连接 - 但这对于简单的GUI来说可能是过度的。

#1


2  

Your repaint works fine. Your problem is that you're calling addRectangle() to the wrong DrawingPanel instance, not the one that is currently displayed in the GUI. To solve this, pass a reference from the displayed one to the code that needs to call the method.

你的重画工作正常。您的问题是您正在将addRectangle()调用到错误的DrawingPanel实例,而不是当前在GUI中显示的实例。要解决此问题,请将显示的引用传递给需要调用该方法的代码。

To see that this is correct, simply search this page for new DrawingPanel(). You should see this only once in your own code (and your code posted). You see it twice meaning you've created two instances of this.

要查看这是正确的,只需在此页面中搜索新的DrawingPanel()。您应该只在自己的代码中看到一次(并且您的代码已发布)。你看到它两次意味着你已经创建了两个这样的实例。

e.g., change this:

例如,改变这个:

public class PropertyPanel extends JPanel {   
    private DrawingPanel drawingPanel = new DrawingPanel();

    public PropertyPanel(){

to this:

public class PropertyPanel extends JPanel {    
    private DrawingPanel drawingPanel;  // don't create new

    public PropertyPanel(DrawingPanel drawingPanel){
        this.drawingPanel = drawingPanel;

and then pass in the true DrawingPanel into this object on creation:

然后在创建时将真正的DrawingPanel传递给此对象:

DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel(drawingPanel);

Better still: structure your code in a M-V-C way, and use dependency injection to make connections -- but this may be overkill for your simple GUI.

更好的是:以M-V-C方式构建代码,并使用依赖注入来建立连接 - 但这对于简单的GUI来说可能是过度的。