Yesterday I had a problem with my code being nested and having too many static classes. I have cleaned up the code and am now trying to add an action listener to the JButtons. i have 5 different buttons that is on the GUI, "Profile, Market, Users, Notes, Information." each one will be a button on a GUI. The user will be able to click on one of the JButtons such as "Profile" and it will open another GUI. "I'm writing this in eclipse.". "I'm also not using a GUI builder." I have made another GUI that will open when one of the buttons are clicked :
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
I just cannot figure out how to add it to each JButton. If there is anyways someone could give a visual idea or link on how to add the GUI above to all of the JButtons, that would be greatly appreciated. Here is the code with all of my JButtons, and the GUI ActionEvent :
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Stocks {
public static void main(String [] args) {
JFrame frame = new JFrame ("Java Stocks");
frame.setSize(700,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel (new GridBagLayout());
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints ();
JButton button1 = new JButton("Profile");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(40, 40, 40, 40);
panel.add(button1, c);
button1.addActionListener(new Action());
JButton button2 = new JButton("Market");
c.gridx = 0;
c.gridy = 1;
panel.add(button2, c);
button2.addActionListener(new Action());
JButton button3 = new JButton("Users");
c.gridx = 0;
c.gridy = 2;
panel.add(button3, c);
button3.addActionListener(new Action());
JButton button4 = new JButton("Notes");
c.gridx = 0;
c.gridy = 3;
panel.add(button4, c);
button4.addActionListener(new Action());
JButton button5 = new JButton("Information");
c.gridx = 0;
c.gridy = 4;
panel.add(button5, c);
button5.addActionListener(new Action());
}
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
Here is a picture of the buttons. The user will be able to click one of the buttons and it will open a new GUI