views:

19

answers:

0

hi guys, i'm having small problems in calling variables and changing the variables after the user hit the change button. i'm new here trying to learn Jave , hope you can help.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class NestedPanelsTwo {

    private JPanel primaryPanel, subPanel1, subPanel2; 
    private JLabel label; 
    private JButton button; 
    private JFrame frmWindows;
    private int i,i2;
    private String [] x={" Panel One"," Panel Two"};
    private String[] y ={" On left"," On Right"};
    private int[] loction = {SwingConstants.LEFT,SwingConstants.RIGHT};

    public NestedPanelsTwo(){
        primaryPanel= new JPanel(new GridLayout(1,2));
        subPanel1 = new JPanel(new FlowLayout());
        subPanel2 = new JPanel(new FlowLayout());
        ImageIcon icon = new ImageIcon("bluej-84-toned.JPG");
        subPanel1.setPreferredSize(new Dimension(400,200));
        subPanel1.setBackground(Color.green);
        button = new JButton("Change one");
        i=0;
        label = new JLabel(x[i] + " "+ y[i] , icon , loction[i]);

        label.setHorizontalTextPosition(loction[i]);
        button.addActionListener(new ClickEvent(label));
        subPanel1.add(label);
        subPanel1.add(button);
        primaryPanel.add(subPanel1);

        frmWindows = new JFrame("Panels");
        frmWindows.getContentPane().add(primaryPanel);
        frmWindows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmWindows.setVisible(true);
        frmWindows.pack();

        subPanel2 = new JPanel(new FlowLayout());
        subPanel2.setPreferredSize(new Dimension(300,100));
        subPanel2.setBackground(Color.blue);
        button = new JButton("Change Two");
        i2=1;
        label = new JLabel(x[i2] + " "+ y[i2] , icon , loction[i2]);
        label.setHorizontalTextPosition(loction[i2]);
        button.addActionListener(new ClickEvent(label));
        subPanel2.add(label);
        subPanel2.add(button);
        primaryPanel.add(subPanel2);


    }   
    private class ClickEvent implements ActionListener {

        private JLabel label;
          public ClickEvent(JLabel label)
          {

          this.label = label;
          this.i  = i;
          }

           public void actionPerformed(ActionEvent event)
           {
               i =(i+1)%2;
               label.setText(x[i]+ y[i]);
               label.setHorizontalTextPosition(loction[i]);
           }
    }
    public static void main(String[] args) {
        NestedPanelsTwo x = new NestedPanelsTwo();
    }
    }

the problem in changing x and y after the action thanks in advance