actionlistener no working,, help
it is giving me no error but still the actionlistener not working
can u help me pls
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
public class plaza extends JFrame
{
double price;
int num;
double tp=0.0;
JButton B04,B05,B06,B07;
JPanel P01,P03,P04;
ImageIcon I01,I02,I03,I04,I05;
JLabel L01,L02,L03,L04,L07,L08,L11,L12,L15,L16,L17,L18;
public plaza()
{
//create objects for the componenets.
P01=new JPanel();
P03=new JPanel();
P04=new JPanel();
I01=new ImageIcon("Plaza.png");
I02=new ImageIcon("pic1.jpg");
I03=new ImageIcon("pic5.jpg");
I04=new ImageIcon("pic3.jpg");
I05=new ImageIcon("pic4.jpg");
L01 = new JLabel(I01);
L02 = new JLabel("How Many People:");
L03 = new JLabel(I04);
L04 = new JLabel("Executive Suite For individual use $478 ");
//$478
L07 = new JLabel(I05);
L08 = new JLabel("Room for the business For one $285");
//$285
L11 = new JLabel(I02);
L12 = new JLabel("Standard Room for two $237 per person");
// $237
L15 = new JLabel(I03);
L16 = new JLabel("King Room for two $310 per person");
// $110
L17 = new JLabel(" $" + tp);
L18=new JLabel("Price=");
B04 = new JButton("OK");
B05 = new JButton("OK");
B06 = new JButton("OK");
B07 = new JButton("OK");
// add the components to pannels
add(P01, BorderLayout.NORTH);
P01.add(L01);
add(P03, BorderLayout.CENTER);
P03.setLayout(new GridLayout(4,5,10,10));
//P03.setBackground(new Color(215,198,132));
P03.add(L03);
P03.add(L04);
P03.add(B04).setBackground(new Color(215,198,132));;
P03.add(L07);
P03.add(L08);
P03.add(B05).setBackground(new Color(215,198,132));;
P03.add(L11);
P03.add(L12);
P03.add(B06).setBackground(new Color(215,198,132));;
P03.add(L15);
P03.add(L16);
P03.add(B07).setBackground(new Color(215,198,132));;
add(P04, BorderLayout.SOUTH);
P04.setBackground(Color.GRAY);
P04.add(L18);
P04.add(L17);
//P04.add();
ChangeButton b=new ChangeButton();
B04.addActionListener(b);
B05.addActionListener(b);
B06.addActionListener(b);
B07.addActionListener(b);
}
// add the action listener to the component.
public class ChangeButton implements ActionListener{
public void actionPerformed( ActionEvent e)
{
if (e.getSource()==B04)
{
tp+=478;
} else if(e.getSource()==B05){
tp+=285;
}else if(e.getSource()==B06){
num=2;
price=237;
tp+=(num*price);
}else if( e.getSource()==B07){
num=2;
price=310;
tp+=(num*price);
}else{
System.out.println("Choose again");
}
}
}
// call the main class
public static void main(String[] args)
{
JFrame f = new plaza();
JPanel p =new JPanel();
f.setTitle("The Plaza Hotel");
f.setSize(800,800);
f.setVisible(true); //set frame visible
p.setVisible(true); //set panel visible
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Re: actionlistener no working,, help
Please explain what you mean.
Add a println first statement in the actionPerformed method to see if the listener is being called.
Re: actionlistener no working,, help
You have only set values in the actionListener.
I believe it IS working but you think it is not because you have not set up any Observers to relay the changed information back to your View.
Either use setter methods and set up Observers following the Model-View method or manually type what form fields or JLabels you want updated after new values are set in your action listener.