gpt4 book ai didi

java - 如何访问外部类的公共(public)方法以便在 JPanel 上重绘?

转载 作者:行者123 更新时间:2023-11-30 07:29:52 24 4
gpt4 key购买 nike

我无法访问扩展 JPanel 的类 MyPanel 的公共(public)方法。在 MyPanel 中,我之前有一个名为 moveSquareprivate 方法,但为了使其适用于外部请求,我将其更改为 public ,但还是不行?如何让它发挥作用?

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class mull {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel p = createAndShowGUI();
iterate(p);
// "The method iterate(MyPanel) in the type mull is not applicable for the arguments (JPanel)"
}
});
}

private static JPanel createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// how to grab link to this panel - in order to use it in iteration loop ?
MyPanel p = new MyPanel();
f.add(p);
// f.add(new MyPanel());
f.pack();
f.setVisible(true);
return p;
}
private static void iterate(JPanel p){
// the loop should change square position on each iteration
// how to implement ?

for (int i = 0; i < 999; i++){
((MyPanel) p).moveSquare(100 + i*10, 200 + i*10); // here is problem:
//"Cannot make a static reference to the non-static method moveSquare(int, int) from the type MyPanel"
}


}
}

class MyPanel extends JPanel {

private int squareX = 50;
private int squareY = 50;
private int squareW = 200;
private int squareH = 200;

public MyPanel() {

setBorder(BorderFactory.createLineBorder(Color.black));

addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
moveSquare(e.getX(),e.getY());
}
});

addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
moveSquare(e.getX(),e.getY());
}
});

}
// originally this method was private - in orger to access it within mull, it vas changed to public
public void moveSquare(int x, int y) {
int OFFSET = 1;
if ((squareX!=x) || (squareY!=y)) {
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
squareX=x;
squareY=y;
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
}
}


public Dimension getPreferredSize() {
return new Dimension(900,700);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
g.setColor(Color.RED);
g.fillRect(squareX,squareY,squareW,squareH);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,squareW,squareH);
}
}

此语法的作用 ((MyPanel) p).moveSquare(100 + i*10, 200 + i*10); ?如何在 iterate(p) 中绘制自定义形状(椭圆)?

最佳答案

您似乎甚至没有调用 iterate()

但在任何情况下,您的 moveSquare 方法都不是静态的,因此需要特定的 MyPanel 实例才能运行。因此,您可能希望保留对面板的引用。比方说,您可以使用:

而不是 f.add(new MyPanel())
MyPanel p = new MyPanel();
f.add(p);

然后将 p 传递给 iterate 并在其中调用 p.moveSquare(100,200)

关于java - 如何访问外部类的公共(public)方法以便在 JPanel 上重绘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com