JPanelをArrayListと別のJPanelに追加しようとしています。その後、JPanelが配置されているJFrameをrepaint()します。数時間の試行の後、私は疲れて、考えるのが難しいです。私はプログラムを何度も変更して、私が見たことのない簡単な間違いがあったかもしれません(エラーは私の英語でも見つけられます)。repaint()JFrameとJPanel
これはわかりませんが、事前にお詫びします。
JFrameの
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class JFrameClassen extends JFrame{
ArrayList <Bild> somePictures= new <Bild> ArrayList();
JPanel p;
public JFrameClassen(){
super("Window with pictures");
p = new JPanel();
p.setBackground(Color.GREEN);
add(p);
setBounds(1300, 500, 400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void addPhoto(String s){
somePictures.add(new Bild(s));
p.add(somePictures.get(somePictures.size()-1));
getContentPane().repaint();
}
public void addPhoto(String [] arr){
for(String s : arr){
somePictures.add(new Bild(s));
p.add(somePictures.get(somePictures.size()-1));
}
getContentPane().repaint();
}
public static void main(String[] args) {
JFrameClassen j = new JFrameClassen();
String oneArray[] = {"blab.gif", "peli.gif"};
j.addPhoto(oneArray);
j.addPhoto("stef.gif");
j.addPhoto("pear.gif");
}
}
のJPanel
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Bild extends JPanel{
ImageIcon myImage;
int posX = 50;
int posY = 50;
Muslyssnare m = new Muslyssnare(this);
public Bild(String name){
myImage= new ImageIcon(name);
addMouseListener(m);
addMouseMotionListener(m);
}
public void move(int x, int y){
posX = x;
posY = y;
super.repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(myImage.getImage(), posX, posY, this);
}
}
MouseAdapterのは
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Muslyssnare extends MouseAdapter implements MouseMotionListene{
Bild oneImage;
public Muslyssnare(Bild b){
oneImage = b;
}
public void mouseClicked (MouseEvent e) {
System.out.println("(" + e.getX() + "," + e.getY() + ")");
}
public void mouseDragged (MouseEvent e) {
int x = e.getX();
int y = e.getY();
oneImage.move(x, y);
}
}
あなたが直面している正確な問題は何ですか? – user3437460
'revalidate'とそれに続く' repaint'を呼び出します。 – MadProgrammer