繁体   English   中英

repaint()JFrame和JPanel

[英]repaint() JFrame and JPanel

我尝试在一个ArrayList和另一个JPanel中添加JPanel。 然后重新绘制()JPanel所在的JFrame。经过数小时的尝试,我开始感到疲倦并难以思考。 我对程序进行了多次更改,以至于可能出现了一些我不再看到的简单错误(错误也可能在我在这里写的英文中找到)。

如果无法理解,我谨向您致歉。

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

您需要在主JPanel上设置一个Layout。

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

注意: BorderLayout将调整内部面板的大小,以占据所有可用宽度。 您可以使用其他布局。 更多信息: https : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html http://www.oracle.com/technetwork/java/tablelayout-141489.html

注意二:下一个问题将是图像加载。

用Java加载ImageIcon

如何将图像添加到JPanel?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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