I am a beginner, and i need help with my code that creates GUI. In main function, i use default constructor for class GUI that creates a frame with menu. Then i want the main function to wait with executing the rest of the code (which will start a simulation) until user presses START JButton in the GUI frame. How can I do that without using any loop? Below is my code, kinda long because of the GroupLayout that i use
package simulation;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;
public class GUI extends JFrame {
//by default map set to riverside
public String mapName = "riverside";
//user number of creatures input is stored in a hashmap
HashMap<String, Integer> mapOfCreatures = new HashMap<>();
public GUI()
{
this.setTitle("Simulation Of Evolution");
this.setDefaultCloseOperation(3);
setVisible(true);
this.setSize(500, 500);
this.setLocation(300, 300);
initComponents();
}
public void initComponents()
{
biomes.add(island);
biomes.add(riverside);
biomes.add(seaside);
island.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mapName = "island";
}
});
riverside.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mapName = "riverside";
}
});
seaside.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mapName = "seaside";
}
});
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mapOfCreatures.put("wolves", (Integer)l1.getValue());
mapOfCreatures.put("humans", (Integer)l2.getValue());
mapOfCreatures.put("cockroach", (Integer)l3.getValue());
mapOfCreatures.put("dinos", (Integer)l4.getValue());
mapOfCreatures.put("bird", (Integer)l5.getValue());
mapOfCreatures.put("fish", (Integer)l6.getValue());
System.out.println(mapName);
dispose();
}
});
//this.getContentPane().setBackground(Color.BLUE);
GroupLayout layout = new GroupLayout(getContentPane());
this.getContentPane().setLayout(layout);
/*here is the grouplayout, it is long so i deleted it here/*
//components
JRadioButton island = new JRadioButton("Island");
JRadioButton riverside = new JRadioButton("Riverside");
JRadioButton seaside = new JRadioButton("Seaside");
ButtonGroup biomes = new ButtonGroup();
JButton button = new JButton("START");
JLabel label1 = new JLabel("Please choose number of creatures in the beginning of simulation:");
JLabel label2 = new JLabel("Please choose a biome:");
JLabel label3 = new JLabel("Wolves");
JLabel label4 = new JLabel("Humans");
JLabel label5 = new JLabel("Cockroach");
JLabel label6 = new JLabel("Dinos");
JLabel label7 = new JLabel("Bird");
JLabel label8 = new JLabel("Fish");
JSpinner l1 = new JSpinner(new SpinnerNumberModel(1, 1, 300, 1));
JSpinner l2 = new JSpinner(new SpinnerNumberModel(1, 1, 300, 1));
JSpinner l3 = new JSpinner(new SpinnerNumberModel(1, 1, 300, 1));
JSpinner l4 = new JSpinner(new SpinnerNumberModel(1, 1, 300, 1));
JSpinner l5 = new JSpinner(new SpinnerNumberModel(1, 1, 300, 1));
JSpinner l6 = new JSpinner(new SpinnerNumberModel(1, 1, 300, 1));
**
public static void main(String[] args) {
new GUI();
/here will be rest of code, which should be executed when user presses START Jbutton/
}**
}
My teacher said i shouldnt use any loops, maybe try callbacks or thread but i have no idea how to implement it
user25304176 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.