gpt4 book ai didi

java - repaint() JFrame 和 JPanel

转载 作者:行者123 更新时间:2023-11-30 03:00:19 32 4
gpt4 key购买 nike

我尝试将 JPanel 添加到 ArrayList 和另一个 JPanel 中。然后repaint() 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);

}
}

鼠标适配器

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 上设置布局。

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

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

ImageIcon Loading in Java

How to add an image to a JPanel?

关于java - repaint() JFrame 和 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173425/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com