2016-03-23 16 views
1

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); 
    } 
} 
+0

あなたが直面している正確な問題は何ですか? – user3437460

+0

'revalidate'とそれに続く' repaint'を呼び出します。 – MadProgrammer

答えて

2

あなたはあなたの主なJPanelの上のレイアウトを設定する必要があります。

public JFrameClassen(){ 
    super("Window with pictures"); 

    p = new JPanel(); 
    p.setBackground(Color.GREEN); 

    // This will stack your newly created panels. 
    p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS)); 

    // This will generate a scroll bar. You may need it 
    JScrollPane pane = new JScrollPane(p); 

    add(pane); 

    setBounds(1300, 500, 400, 400); 
    setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

} 

もMadProgrammerのアドバイスに従うと再検証を呼び出す/

public void addPhoto(String s){ 

    somePictures.add(new Bild(s)); 
    p.add(somePictures.get(somePictures.size()-1)); 

    getContentPane().revalidate(); 
    getContentPane().repaint(); 
} 

// Simplify your code. Reuse 
public void addPhoto(String [] arr){ 
    for(String s : arr){ 
     addPhoto(s); 
    } 
} 

NOTEを塗り替える:BorderLayoutを利用可能なすべての幅を占有するために、あなたのインナーパネルのサイズを変更します。あなたは他のレイアウトを利用することができます。 詳細情報:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html http://www.oracle.com/technetwork/java/tablelayout-141489.html

NOTE II:あなたが直面するだろう次の問題は、画像の読み込みです。

ImageIcon Loading in Java

How to add an image to a JPanel?

関連する問題