'Lo everyone, I am having some pretty frustrating issues that I can not seem to get rid of.
The main goal of my program is to make a GUI with buttons that trigger custom events.
C:\Java Tests>javac JGUITest.java JGUITest.java:13: cannot find symbol symbol : class ButtonHandler location: class LabelFrame private ButtonHandler handler = new ButtonHandler(); ^ JGUITest.java:13: cannot find symbol symbol : class ButtonHandler location: class LabelFrame private ButtonHandler handler = new ButtonHandler(); ^ JGUITest.java:18: call to super must be first statement in constructor super("Testing Event Capturing"); ^ JGUITest.java:70: cannot find symbol symbol : constructor JList(int[]) location: class javax.swing.JList jList1 = new JList(intArray);
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.EventObject;
class LabelFrame extends JFrame
{
private JButton newNumber;
private JButton dispAll;
private JButton totalNumber;
private ButtonHandler handler = new ButtonHandler();
public void LabelFrame()
{
super("Testing Event Capturing");
setLayout(new FlowLayout());
Icon testIcon2 = new ImageIcon(getClass().getResource("icon_test2"));
Icon testIcon3 = new ImageIcon(getClass().getResource("icon_test3"));
Icon testIcon4 = new ImageIcon(getClass().getResource("icon_test4"));
newNumber = new JButton("New Number", testIcon2);
add(newNumber);
dispAll = new JButton("Display All Numbers", testIcon3);
add(dispAll);
totalNumber = new JButton("Total All Numbers",testIcon4);
add(totalNumber);
newNumber.addActionListener(handler);
dispAll.addActionListener(handler);
totalNumber.addActionListener(handler);
}
}
class custEvents
{
int[] intArray = {0,0,0,0,0,0,0,0,0,0};
int total;
JList jList1;
public void addNumber()
{
if (intArray[9] != 0)
{
for (int i = 0; i < intArray.length; i++)
{
if (intArray[i] == 0)
{
String uInput = JOptionPane.showInputDialog("Please enter a number");
intArray[i] = Integer.parseInt(uInput);
i = 10;
}
}
}
else
{
JOptionPane.showMessageDialog(null,"The number array is full! (10)", "WARNING!",JOptionPane.PLAIN_MESSAGE);
}
}
public void displayNumbers()
{
jList1 = new JList(intArray);
jList1.setVisibleRowCount(10);
//JOptionPane.showMessageDialog(null,JList1.setVisible(true),"List of Numbers",JOptionPane.PLAIN_MESSAGE);
JFrame newFrame = new JFrame("List of Numbers");
JPanel newPanel = new JPanel();
newPanel.add(jList1);
newFrame.add(newPanel);
newFrame.setSize(400,400);
newFrame.setVisible(true);
}
public void addNumbers()
{
total = 0;
for (int i = 0; i < intArray.length; i++)
{
if (intArray[i] == 0)
{
i = 10;
}
else
{
total += intArray[i];
}
}
JOptionPane.showMessageDialog(null,"The sum of the numbers in the array are " + total,"Sum of Array",JOptionPane.PLAIN_MESSAGE);
total = 0;
}
public void error()
{
JOptionPane.showMessageDialog(null,"Message not detected or not sent!!!","ERROR",JOptionPane.PLAIN_MESSAGE);
}
}
class custListener
{
custEvents cEvents = new custEvents();
public void actionPreformed(ActionEvent cust)
{
if ((cust.getActionCommand()) .equals ("newNumber"))
{
cEvents.addNumber();
}
else if ((cust.getActionCommand()) .equals ("dispAll"))
{
cEvents.displayNumbers();
}
else if ((cust.getActionCommand()) .equals ("totalNumber"))
{
cEvents.addNumbers();
}
else
{
cEvents.error();
}
}
}
public class JGUITest
{
public static void main (String args[])
{
LabelFrame newFrame = new LabelFrame();
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newFrame.setSize(600,600);
newFrame.setVisible(true);
}
}Hello Geowil.
Your issue starts with this line:
private ButtonHandler handler = new ButtonHandler();
What is a ButtonHandler? It is likely you meant to create a new Class called ButtonHandler which is not anywhere to be found.
Hello Geowil.
Your issue starts with this line:
private ButtonHandler handler = new ButtonHandler();What is a ButtonHandler? It is likely you meant to create a new Class called ButtonHandler which is not anywhere to be found.
After taking a break then reading into it a bit more that is what I came to the conclusion of, that I needed and actual inter class for this. I made it and those errors went away, also found out that JList's, as far as array's go, are string only. So I got that error as well, just trying to get it running to test it at the moment.
On top of those I found that buttons do not send their names, but rather the text associated with the buttons, to the event manager, which I find very strange.
My new code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LabelFrame extends JFrame
{
JButton newNumber;
JButton dispAll;
JButton totalNumber;
public LabelFrame()
{
super("Testing Event Capturing");
setLayout(new FlowLayout());
Icon testIcon2 = new ImageIcon(getClass().getResource("icon_test2.jpg"));
Icon testIcon3 = new ImageIcon(getClass().getResource("icon_test3.jpg"));
Icon testIcon4 = new ImageIcon(getClass().getResource("icon_test4.jpg"));
newNumber = new JButton("New Number", testIcon2);
add(newNumber);
dispAll = new JButton("Display All Numbers", testIcon3);
add(dispAll);
totalNumber = new JButton("Total All Numbers",testIcon4);
add(totalNumber);
ButtonHandler handler = new ButtonHandler();
newNumber.addActionListener(handler);
dispAll.addActionListener(handler);
totalNumber.addActionListener(handler);
}
private class ButtonHandler implements ActionListener
{
custEvents cEvents = new custEvents();
public void actionPerformed(java.awt.event.ActionEvent event)
{
if ((event.getActionCommand()) .equals ("New Number"))
{
cEvents.addNumber();
}
else if ((event.getActionCommand()) .equals ("Display All Numbers"))
{
cEvents.displayNumbers();
}
else if ((event.getActionCommand()) .equals ("Total All Numbers"))
{
cEvents.addNumbers();
}
else
{
cEvents.error(event.getActionCommand());
}
}
}
}
class custEvents
{
String[] stringArray = {"0","0","0","0","0","0","0","0","0","0"};
int total;
JList jList1;
public void addNumber()
{
if ((stringArray[9]).equals ("0"))
{
for (int i = 0; i < stringArray.length; i++)
{
if ((stringArray[i]) .equals ("0"))
{
String uInput = JOptionPane.showInputDialog("Please enter a number");
stringArray[i] = uInput;
i = 10;
}
}
}
else
{
JOptionPane.showMessageDialog(null,"The array is full! (10)", "Warning!",JOptionPane.PLAIN_MESSAGE);
}
}
public void displayNumbers()
{
jList1 = new JList(stringArray);
jList1.setVisibleRowCount(10);
//JOptionPane.showMessageDialog(null,JList1.setVisible(true),"List of Numbers",JOptionPane.PLAIN_MESSAGE);
JFrame newFrame = new JFrame("List of Numbers");
JPanel newPanel = new JPanel();
newPanel.add(jList1);
newFrame.add(newPanel);
newFrame.setSize(400,400);
newFrame.setVisible(true);
}
public void addNumbers()
{
total = 0;
for (int i = 0; i < stringArray.length; i++)
{
if ((stringArray[i]) .equals ("0"))
{
i = 10;
}
else
{
total += Integer.parseInt(stringArray[i]);
}
}
JOptionPane.showMessageDialog(null,"The sum of the numbers in the array are " + total,"Sum of Array",JOptionPane.PLAIN_MESSAGE);
total = 0;
}
public void error(String event)
{
JOptionPane.showMessageDialog(null,"Message not detected or not sent!!! Recevied " + event,"ERROR",JOptionPane.PLAIN_MESSAGE);
}
}
public class JGUITest
{
public static void main (String args[])
{
LabelFrame newFrame = new LabelFrame();
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newFrame.setSize(600,600);
newFrame.setVisible(true);
}
}
And so far it works (for all I know, I have not recompiled since I fixed the check to add a number to the stringArrray, had it only continuing is a 0 was not present in element 9 :D).