Hello,
I have two classes, Help and RecylingGui. I have a button on the RecyclingGui called Help, which when clicked should open the text area created in the Help class. I added the ActionListener for this but nothing is happening. Any help would be appreciated. Thanks.
The Code:
Recycling Class
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* Simple Graphical User Interface for the Recycling Machine.
*
*/
public class RecyclingGUI extends JFrame implements ActionListener {
CustomerPanel myPanel = new CustomerPanel(new Display());
public CustomerPanel getPanel() { return myPanel; }
public void actionPerformed(ActionEvent e) {
//System.out.println("Received: e.getActionCommand()="+e.getActionCommand()+
// " e.getSource()="+e.getSource().toString() );
if( e.getSource() == slot1 )
{
myPanel.itemReceived(1);
} else if( e.getSource() == slot2 ) {
myPanel.itemReceived(2);
} else if( e.getSource() == slot3 ) {
myPanel.itemReceived(3);
} else if(e.getSource() == slot4 ){
myPanel.itemReceived(4);
} else if( e.getSource() == receipt ) {
myPanel.printReceipt();
} else if(e.getSource() == check){
int siize = Integer.parseInt(size.getText().toString());
int weeight = Integer.parseInt(weight.getText().toString());
myPanel.printitem(siize,weeight);
}else if(e.getSource() == unknown){
myPanel.itemReceived(5);
}
}
static JButton slot1 = new JButton("Slot 1");
static JButton slot2 = new JButton("Slot 2");
static JButton slot3 = new JButton("Slot 3");
static JButton slot4 = new JButton("Slot 4");
static JButton unknown = new JButton("unknown");
static JButton help = new JButton("help");
static JButton receipt = new JButton("Receipt");
static JTextField weight = new JTextField(5);
static JTextField size = new JTextField(5);
static JButton check = new JButton("Check");
static JLabel sizeLabel = new JLabel("Size");
static JLabel weightLabel = new JLabel("Weight");
private static JPanel getFieldPanel() {
JPanel p = new JPanel(new GridLayout(4,2));
p.setBorder(BorderFactory.createTitledBorder("Recycle"));
p.add(new JLabel("Weight:",SwingConstants.RIGHT));
p.add(size);
p.add(new JLabel("Size:",SwingConstants.RIGHT));
p.add(weight);
p.add(check);
p.add(help);
return p;
}
public static JPanel getButtonPanel() {
JPanel p = new JPanel(new GridLayout(5,1));
//p.setBorder(BorderFactory.createTitledBorder("Recycle"));
p.add(slot1);
p.add(slot2);
p.add(slot3);
p.add(slot4);
p.add(unknown);
p.add(receipt);
return p;
}
public RecyclingGUI() {
super();
setSize(400, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.add(getButtonPanel());
panel.add(getFieldPanel());
slot1.addActionListener(this);
slot2.addActionListener(this);
slot3.addActionListener(this);
slot4.addActionListener(this);
receipt.addActionListener(this);
check.addActionListener(this);
unknown.addActionListener(this);
help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Help();
}
});
getContentPane().add(panel);
panel.repaint();
}
public static void main(String [] args ) {
RecyclingGUI myGUI = new RecyclingGUI();
myGUI.setVisible(true);
myGUI.pack();
}
}
Help Class
import javax.swing.*;
import java.awt.*;
public class Help extends JPanel {
public Help(){
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(400,200));
JTextArea textArea = new JTextArea(5,40);
textArea.setText("Help - FAQ");
textArea.setEditable(false);
JScrollPane ScrollPane = new JScrollPane(textArea);
this.add(ScrollPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
//Help GU = new Help();
JPanel panel = new Help();
panel.setOpaque(true);
JFrame frame = new JFrame("Help");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
}