I'm using IntelliJ IDEA, and I have created two GUI forms I want to use as screens for my application. However I am stuck on how to get my separate main-class to incorporate the two JFrames, and how to control the switch from one JFrame to another. In the code below in the main-class, I'm first initializing an object of both GUI forms, afterwards I'm running the setup-frames 'run' method, which sets the frame visible. This step works.
Now I want an actionListener on my button 'udførButton' which gets the values typed in it's TextFields, so that I can use them as parameters to initialize an instance of the 'Billetautomat' class. Furthermore, I want the button to close the 'setup' JFrame and to start up the 'gui' JFrame with the gui.run() method.
My intended flow in the program is:
- Run 'setup' gui, where I can type in three names and three ints
- Run 'gui' (not done designing yet), where I can press multiple buttons to trigger different actions
- Run other JFrames for non-specified actions and return to 'gui' (step 2)
Problem is, that the actionListener won't work as I want it to... If I change the code inside the actionListener to billet1Label.setText("HELLO");
it indeed changes the Label's text to display HELLO, but I can't make it work as I intend... I think the problem is that the program doesn't stop and wait for my button click, but just races through the main code...
Do I need some sort of check to see if the button has been pressed, or have you got any expert-advise?
This is my first time working with Swing in Java, so please be patient with me...
The final
and the []
on the variables was suggested by IntelliJ....
BenytBilletautomat_GUI.java (main-class)
package automat;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BenytBilletautomat_GUI {
public static void main(String[] args) {
final int[] pris1 = new int[1];
final int[] pris2 = new int[1];
final int[] pris3 = new int[1];
final String[] navn1 = new String[1];
final String[] navn2 = new String[1];
final String[] navn3 = new String[1];
Setup_GUI setup = new Setup_GUI();
Billetautomat_GUI gui = new Billetautomat_GUI();
setup.udførButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
navn1[0] = setup.getBillet1Text();
pris1[0] = setup.getBilletPris1();
navn2[0] = setup.getBillet1Text();
pris2[0] = setup.getBilletPris1();
navn3[0] = setup.getBillet1Text();
pris3[0] = setup.getBilletPris1();
Billetautomat automat = new Billetautomat(navn1[0],pris1[0],navn2[0],pris2[0],navn3[0],pris3[0]);
setup.frame.setVisible(false);
setup.frame.dispose();
gui.run();
}
});
setup.run();
}
}
Setup_GUI.java (setup gui class)
package automat;
import javax.swing.*;
public class Setup_GUI {
private JPanel mainPanel;
private JLabel gui_title;
private JTextField billet1Text;
private JTextField billet2Text;
private JTextField billet3Text;
private JLabel billet1Label;
private JLabel billet2Label;
private JLabel billet3Label;
private JLabel setupLabel;
private JTextField billetPris1;
private JTextField billetPris2;
private JTextField billetPris3;
public JButton udførButton;
JFrame frame = new JFrame("Setup");
public void run() {
frame.setContentPane(new Setup_GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public String getBillet1Text() {
return billet1Text.toString();
}
public String getBillet2Text() {
return billet2Text.toString();
}
public String getBillet3Text() {
return billet3Text.toString();
}
public int getBilletPris1() {
return Integer.parseInt(billetPris1.toString());
}
public int getBilletPris2() {
return Integer.parseInt(billetPris2.toString());
}
public int getBilletPris3() {
return Integer.parseInt(billetPris3.toString());
}
}
Billetautomat_GUI.java (gui class, not done yet)
package automat;
import javax.swing.*;
import java.awt.event.ComponentAdapter;
public class Billetautomat_GUI {
JFrame frame = new JFrame("Billetautomat_GUI");
private JPanel mainPanel;
public void run() {
frame.setContentPane(new Billetautomat_GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}