lostinspace9
asked on
JButton
I am having trouble using
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Market extends JFrame{
public Market(){
}
public static void main(String[] args){
float values[] = {98,62,82,69,45,12,98,69,87,85,100,96,97,98,99};
Person [] people = new Person[2];
people[0] = new Teacher("Test ", "Test", 98);
people[1] = new Student("Test2 ", "Test2", 45);
((Student)people[1]).inputGradeList(values);
for (int i=0; i< people.length; ++i) System.out.println (people[i]);
Market frame = new Market();
frame.setTitle("Listing");
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
p1.add(new JLabel("" + people[0]), BorderLayout.SOUTH);
p1.setBackground(Color.BLACK);
p1.setForeground(Color.PINK);
p1.setBorder(new TitledBorder("Name"));
frame.add(p1);
JButton jbtRight = new JButton("Next");
jbtRight.setBackground(Color.BLACK);
jbtRight.setForeground(Color.WHITE);
p1.add(jbtRight);
ActionListener listener1 = new NewListener();
jbtRight.addActionListener(listener1);
class NewListener implements ActionListener {
public void actionPerformed(Action e)
{
for (int i=0; i < people.length; ++i)
System.out.println (people[i]);
}
}
}
JButton to advance through an array. Here is what I have.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is great! Thank you.
ActionListener listener1 = new NewListener();
Then you will need to put the class definition outside of the main() method. Either in another file, or you can make it an inner class in your Market class. If you make it an inner class, be sure it is defined as "static" otherwise you cannot instantiate it from within the static main() method.
(Alternatively, you can implement it as an anonymous inner class, providing the implementation within the main() method. I'm not sure if that's the purpose of this assignment, but I can provide info on how to do that as well.)
Then you will also need to define a constructor for NewListener() and pass it your "people" array. Store that in a member variable of your NewListener class, such that the actionPerformed() will have access to it, and be able to print it.
Also the actionPerformed() method takes an ActionEvent as parameter, not an Action.
http://docs.oracle.com/javase/7/docs/api//java/awt/event/ActionListener.html
You will need to change the method signature otherwise the compiler will complain that NewListener does not implement ActionListener.