[英]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
注意二:下一個問題將是圖像加載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.