简体   繁体   中英

repaint() JFrame and JPanel

I try to add JPanel in an ArrayList and in another JPanel. Then repaint () the JFrame which JPanel is located in. After several hours of attempts, I start to get tired and find it difficult to think. I changed the program so many times that there may have been some simple mistakes that I no longer see.(Errors may also be found in my English I write here).

I apologize in advance if this is not understandable.

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

You need to set a Layout on your main 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);

 }

Also follow MadProgrammer's advice and invoke revalidate / repaint

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 will resize your inner panels to occupy all width available. You can user other layouts. More info: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html http://www.oracle.com/technetwork/java/tablelayout-141489.html

NOTE II: Next problem you'll face is image loading.

ImageIcon Loading in Java

How to add an image to a JPanel?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM