So I've been playing around with GUIs and created a relatively nice welcome-frame for my user.
Said frame has two text options, where each text option should, in theory, open a new window.
One of the text frames does exactly what it's suppose to, opens a new window, as desired, the other, however, does not.
The following is my code:
package gui;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Frame1Welcome extends JFrame {
private JLabel intro1 = new JLabel(
"Welcome to Twitter Farmer.");
private JLabel intro2 = new JLabel(
"In order to start farming up those tweets, first decide if you want to farm for a specific amount of time, or farm a certain amount of tweets.");
private JLabel intro3 = new JLabel(
"By specifying the amount of time you wish to farm for, Twitter Farmer will gather as many tweets as it can in said time.");
private JLabel intro4 = new JLabel(
"If you know how many you want however, Twitter Farmer will instead gather up the exact desired amount.");
private JLabel intro5 = new JLabel(
"Please enter the time in seconds, or the amount of tweets as whole number, and press the 'ENTER'-key.");
private JLabel tweets = new JLabel("Tweets:");
private JLabel time = new JLabel("Time:");
private JTextField twitterText = new JTextField(10);
private JTextField timeText = new JTextField(10);
private String inputText; // REMOVE ME
public Frame1Welcome(String title) {
super(title);
launchGUI.setMyDefaults(this, 800, 300);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 10, 0);
JPanel northernPanel = new JPanel(new GridBagLayout());
this.add(northernPanel, BorderLayout.NORTH);
gbc.gridy = 0;
northernPanel.add(intro1, gbc);
gbc.gridy = 1;
northernPanel.add(intro2, gbc);
gbc.gridy = 2;
northernPanel.add(intro3, gbc);
gbc.gridy = 3;
northernPanel.add(intro4, gbc);
gbc.gridy = 4;
northernPanel.add(intro5, gbc);
JPanel southernPanel = new JPanel(new GridBagLayout());
this.add(southernPanel, BorderLayout.CENTER);
gbc.gridx = 0;
southernPanel.add(tweets, gbc);
gbc.gridx = 1;
southernPanel.add(twitterText, gbc);
gbc.gridy = 1;
gbc.gridx = 0;
southernPanel.add(time, gbc);
gbc.gridx = 1;
southernPanel.add(timeText, gbc);
twitterText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText = twitterText.getText(); // REMOVE ME
dispose();
System.out.println("TWEETS:" + inputText); // REMOVE ME
try {
new Frame3Tweets("Twitter Farmer - Tweet-farm by desired amount", Integer.parseInt(timeText.getText()));
} catch (NumberFormatException error) {
//ADD INVALID NUMBER WINDOW
}
}
});
timeText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText = timeText.getText(); // REMOVE ME
dispose();
System.out.println("TIME:" + inputText); // REMOVE ME
try {
new Frame2Time("Twitter Farmer - Tweet-farm over desired time", Integer.parseInt(timeText.getText()));
} catch (NumberFormatException error) {
//ADD INVALID NUMBER WINDOW
}
}
});
}
}
Firstly, all comment are simply to remind me of things to do later in the code.
Secondly, assume that launchGUI.setMyDefaults(this, 800, 300);
is just my method to set the size and the visibility and all that, and it works for that for other windows.
Thirdly, assume that the new Frame2Time
and new Frame3Tweets
are both functional.
My question is thus:
Why does timeText.addActionListener
create a new window, and twitterText.addActionListener
does not?
I've tried to have twitterText.addActionListener
make other windows too after a number has been given by the user, without any such window popping up.
Could anyone show me what I've done wrong, and preferably, how to fix it?