Java의 JFrame에 여러 클래스 인스턴스를 추가하려고하지만 JFrame에 두 개의 다른 인스턴스를 추가하려고하면 가장 최근에 추가 된 인스턴스 만 표시됩니다. 여기 How to add multiple components to a JFrame?과 : Adding multiple classes to a Jframejframe에 여러 클래스 인스턴스 추가
내 코드가 https://www.youtube.com/watch?v=I3usNR8JrEE&index=51&list=PL53A7C2BE1F8D780C 를 모델로 나는 이클립스 자바 산소 방출 4.7.1a 를 사용하고 있지만, 아무것도 나를 위해 일하지 않았다
나는 여기에 표시된 답변 살펴 보았다. 여기 내 주요 기능입니다 :
package BlockPack;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Block extends JPanel implements ActionListener {
@Override public Dimension getPreferredSize() {
return new Dimension(900, 900);
}
Timer tm = new Timer(50, this);
int blockWidth, blockHeight;
int x, y, velX = 2, velY = 0, gravity = 0;
String thisName;
boolean collidingGround = false;
public Block(String _name, int _x, int _y, int _width, int _height) {
thisName = _name;
x = _x;
y = _y;
blockWidth = _width;
blockHeight = _height;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(x, y, blockWidth, blockHeight);
tm.start();
}
public void actionPerformed(ActionEvent e) {
performMovement();
repaint();
}
private void performMovement() {
y += 1;
}
public static void main(String[] args) {
Block b = new Block("block1", 10, 30, 50, 50);
Block bb = new Block("block2", 30, 60, 10, 60);
JFrame jf = new JFrame();
jf.setLayout(new FlowLayout());
jf.setTitle("Ball");
jf.setSize(600, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = jf.getContentPane();
c.add(new Block("block1", 10, 30, 50, 50));
c.add(new Block("block2", 30, 60, 10, 60));
jf.setVisible(true);
}
}
https://stackoverflow.com/questions/10086936/java-adding-components-to-jframe – IEE1394
전체 문제를 재현하는 간단한 예제를 게시하십시오. ** 여기서 블록을 추가하고 Swing의 스레딩 규칙을 존중해야 프레임을 볼 수 있도록해야하지만 여기서는 잘 작동합니다. 여기 내 모든 최소한의 예가 나와 있습니다 : https://gist.github.com/jnizet/3910ae413d22ca7f153d9fdc41e22086 –
@ IEE1394 추천 해 주셔서 감사합니다. 그러나 그 결과는 이전과 동일합니다. 마지막으로 추가 된 것만 표시됩니다. – Ninjakid14