Reputation: 11
What I am trying to do is to run a simulation using Java graphics and I am looping a bunch of jpanels. Once a condition is met in the simulation, I would like that specific jpanel to close. However, I am not sure how to do that. Currently I have tried using System.exit(0) but I want the other jpanels to continue running. Below is my code for the Pong class and the condition I am using. The condition is located in the board class and I also have a ball class and wall class.(Also this question is not about the game Pong I am using some old code to run the simulation)
package Pong2;
import javax.swing.*;
import java.util.*;
public class Pong2 extends JFrame {
static int a;
static int b;
public Pong2(int a,int b)
{
this.a=a;
this.b=b;
add(new Board2(a,b));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 1000);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
int[] primes=new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
for (int i = 0; i <primes.length ; i++) {
for (int j = i+1; j <primes.length ; j++) {
Pong2 e=new Pong2(primes[i]*100,primes[j]*100);
}
}
}
}
Board class
if(ball.intersects(0,0,5,5)){
ball.stop();
System.out.println("Top left"+a/100+" "+b/100);
setVisible(false);
System.exit(0);
}
Upvotes: 0
Views: 677
Reputation: 85
If you want to close the JPanel and only the JPanel, do
yourFrame.remove(yourPanel);
If you want to remove the entire frame with it, do
yourFrame.dispose();
Upvotes: 1