Swing Java 使用 2 个 jpanels 重绘不工作
Swing Java repaint not working using 2 jpanels
正如您从代码中看到的那样,我希望在 DrawingPanel 上看到一个矩形,但我不确定为什么 repaint() 方法不起作用。任何帮助都感激不尽。我也尝试过 revalidate 方法,但这也不起作用。
这是我的 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 对于第一个 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 对于第二个 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
}
}
您的重绘效果很好。您的问题是您正在调用 addRectangle()
到 错误的 DrawingPanel 实例,而不是当前显示在 GUI 中的实例。要解决此问题,请将显示的引用传递给需要调用该方法的代码。
要确认这是正确的,只需在本页中搜索 new DrawingPanel()
。您应该只在您自己的代码(以及您发布的代码)中看到 一次。你看到它两次意味着你已经创建了两个实例。
例如,更改此:
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
public PropertyPanel(){
对此:
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel; // don't create new
public PropertyPanel(DrawingPanel drawingPanel){
this.drawingPanel = drawingPanel;
然后在创建时将真正的 DrawingPanel 传入此对象:
DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel(drawingPanel);
更好的是:以 M-V-C 的方式构建代码,并使用依赖注入来建立连接——但这对于简单的 GUI 来说可能有点过头了。
正如您从代码中看到的那样,我希望在 DrawingPanel 上看到一个矩形,但我不确定为什么 repaint() 方法不起作用。任何帮助都感激不尽。我也尝试过 revalidate 方法,但这也不起作用。
这是我的 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 对于第一个 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 对于第二个 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
}
}
您的重绘效果很好。您的问题是您正在调用 addRectangle()
到 错误的 DrawingPanel 实例,而不是当前显示在 GUI 中的实例。要解决此问题,请将显示的引用传递给需要调用该方法的代码。
要确认这是正确的,只需在本页中搜索 new DrawingPanel()
。您应该只在您自己的代码(以及您发布的代码)中看到 一次。你看到它两次意味着你已经创建了两个实例。
例如,更改此:
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
public PropertyPanel(){
对此:
public class PropertyPanel extends JPanel {
private DrawingPanel drawingPanel; // don't create new
public PropertyPanel(DrawingPanel drawingPanel){
this.drawingPanel = drawingPanel;
然后在创建时将真正的 DrawingPanel 传入此对象:
DrawingPanel drawingPanel = new DrawingPanel();
PropertyPanel propertyPanel = new PropertyPanel(drawingPanel);
更好的是:以 M-V-C 的方式构建代码,并使用依赖注入来建立连接——但这对于简单的 GUI 来说可能有点过头了。