My program works except for one problem. I run it, it pops up a GUI and allows me to enter text as required... but if I look at my task bar on the bottom of the screen, there are 2 more instances of the GUI. When I close one instance of the GUI, it closes all other instances. The program outputs correctly, but I don't want 3 identical GUIs
thank you for any help, and I apologize for doing a bad job of explaining, but if you copy/paste and run the code you'll see what I mean
thank you for any help, and I apologize for doing a bad job of explaining, but if you copy/paste and run the code you'll see what I mean
Code:
import java.util.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyList < T extends Comparable> extends JFrame
{
static MyList<Integer> listInt = new MyList<Integer>();
static MyList<String> listString = new MyList<String>();
private JTextField num1 = new JTextField(10);
private JTextField num2 = new JTextField(10);
private JTextField num3 = new JTextField(10);
private JTextField str1 = new JTextField(10);
private JTextField str2 = new JTextField(10);
private JTextField str3 = new JTextField(10);
private JLabel numMessage = new JLabel("Please enter \n3 integers");
private JLabel strMessagePt1 = new JLabel("Please enter");
private JLabel strMessagePt2 = new JLabel("3 one-word strings");
private JButton runNum = new JButton("Run Number Function");
private JButton runStr = new JButton("Run String Function");
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JPanel panel4 = new JPanel();
private JPanel panel5 = new JPanel();
private JPanel panel6 = new JPanel();
private JPanel blank1 = new JPanel();
private JPanel blank2 = new JPanel();
private JPanel blank3 = new JPanel();
private final int WINDOW_WIDTH = 500; // Window width
private final int WINDOW_HEIGHT = 300; // Window height
ArrayList<T> newlist = new ArrayList<T>();
public MyList()
{
// Set window parameters
setTitle("MyList Modified");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLayout(new GridLayout(3,3));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(numMessage);
panel2.add(num1);
panel2.add(num2);
panel2.add(num3);
runNum.addActionListener(new numListener());
panel3.add(runNum);
panel4.add(strMessagePt1);
panel4.add(strMessagePt2);
panel5.add(str1);
panel5.add(str2);
panel5.add(str3);
runStr.addActionListener(new strListener());
panel6.add(runStr);
add(panel1);
add(blank1);
add(panel4);
add(panel2);
add(blank2);
add(panel5);
add(panel3);
add(blank3);
add(panel6);
setVisible(true);
}
public void add(T thing)
{
newlist.add(thing);
}
public T getSmallest()
{
T smallest = (T) newlist.get(0);
for(int i=1; i<newlist.size(); i++)
{
if(((Comparable<T>) newlist.get(i)).compareTo(smallest) <0)
smallest = (T) newlist.get(i);
}
return smallest;
}
public T getLargest()
{
T largest = (T) newlist.get(0);
for(int i=1; i<newlist.size(); i++)
{
if(((Comparable<T>) newlist.get(i)).compareTo(largest) > 0)
largest = (T) newlist.get(i);
}
return largest;
}
private class numListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Comparable number1 = Integer.parseInt(num1.getText());
Comparable number2 = Integer.parseInt(num2.getText());
Comparable number3 = Integer.parseInt(num3.getText());
listInt.add((Integer) number1);
listInt.add((Integer) number2);
listInt.add((Integer) number3);
Comparable max = listInt.getLargest();
Comparable min = listInt.getSmallest();
JOptionPane.showMessageDialog(null, "You entered "+number1+", "+
number2+" & "+number3+"\n Largest Number: "+max+
"\nSmallest Number: "+min);
}
}
private class strListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Comparable string1 = str1.getText();
Comparable string2 = str2.getText();
Comparable string3 = str3.getText();
listString.add((String) string1);
listString.add((String) string2);
listString.add((String) string3);
Comparable max = listString.getLargest();
Comparable min = listString.getSmallest();
JOptionPane.showMessageDialog(null, "You entered "+string1+", "+
string2+" & "+string3+"\n Largest String: "+max+
"\nSmallest String: "+min);
}
}
public static void main(String[] args)
{
MyList myListIntAndStr = new MyList();
}
}