简体   繁体   中英

Setting variables one after another

I am trying to create a lock screen interface. I have 4 string variables called L11, L21, L31, and L41. In my LockButton class I am trying to set their values with an ActionListener. I set the first one, L11, to the text from the button. By checking if L11 != null I set L21 and so on. Instead the output shows that L11 is set every time a button is pressed which is not what I am intending.

LockScreen Class

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LockScreen
{
static int lockID;

static String L1, L2, L3, L4;


static JFrame frame;
static JPanel panel;
static JButton digits;
static GridLayout grid = new GridLayout(3,4);

public static void main(String [] args)
{
    frame = new JFrame("LockScreen");
    panel = new JPanel();

    for(int lockID = 1; lockID < 10; lockID++)
    {
        lockButton btn = new lockButton(lockID, L1, L2, L3, L4);
        panel.add(btn);
    }

    panel.setLayout(grid);

    frame.add(panel);
    frame.setSize(300,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

LockButton class

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class lockButton extends JButton
{
int lockID1;
String L11, L21, L31, L41;

public lockButton(int lockID, String L1, String L2, String L3, String L4)
{
    lockID1 = lockID;

    L1 = L11;
    L2 = L21;
    L3 = L31;
    L4 = L41;

    setText("" + lockID1);

    addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            if(L11 == null)
            {
                L11 = getText();
            }
            else
            {
                L21 = getText();
            }

            System.out.println("Btn Txt: " + getText());
            System.out.println("L1: " + L11 + " L2: " + L21 + " L3: " + L31 + " L4: " + L41);
        }
    });
}

public void setL1()
{
    L11 = getText();
}
}

I think these assignments are the wrong way around:

L1 = L11;
L2 = L21;
L3 = L31;
L4 = L41;

Should be:

L11 = L1;
L21 = L2;
L31 = L3;
L41 = L4;

Otherwise, they are leaving the fields null, and also setting the (unused) parameters to be null.

Also, I'm not sure of your intention, but I'm guessing the reason your fields have an extra '1' at the end of the name is so you can distinguish them from the parameter variables. You don't need to do this. You can give them the same name, then use the this keyword to refer to the fields:

this.L1 = L1;

Edit: After looking at the rest of the code I think you're a little confused about how references work. Each button has its own L11, L21, L31, L41. There are 4 more in class LockScreen. This is 40 variables in total, and all of them can be set individually. They are not connected.

I'd suggest grouping the 4 variables into a container object, then you can pass the reference to the single, shared container object around, settings its variables and having the change be visible everywhere.

An array can be used for this. Also if these are numeric digits they are really intended to be ints, not Strings. So in class LockScreen you will have something like:

int[] codeDigits = new int[4];
java.util.Arrays.fill(codeDigits, -1); // using -1 to mark unset digits

Then in LockButton's constructor:

public LockButton(final int digit, final int[] codeDigits) {
    setText("" + digit);

    addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < codeDigits.length; i++) {
                // set next digit:
                if (codeDigits[i] == -1) {
                    codeDigits[i] = digit;
                    break;
                }
            }
            System.out.println(java.util.Arrays.toString(codeDigits));
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM