繁体   English   中英

如何使用在动作侦听器中设置的变量

[英]How to use variables set in an actionlistener

我在理解如何使用动作侦听器更改变量值时遇到问题。

在我的程序中,我需要通过选择一些单选按钮来存储用户所做的选择。

我有一个带有卡片布局的主班,然后是几个班,每个班都是不同的面板。 在其中一个面板中,我有一些单选按钮,其中一个动作侦听器是内部类。

当我尝试在主类中打印变量值时,在用户做出选择之前,它会立即打印出来,因为我实例化了面板类并从中获取变量,所以我在用户更改变量之前获取了变量。

我知道我不应该以线性方式考虑Java,但是如何确保在用户更改了变量之后而不是之前获取变量? 我将无法做到这一点吗? 我知道我的想法有一些缺陷,但是我好久没有睡好觉了,我只是无法解决这个问题。

public class Screen3 extends JPanel{

JRadioButton addition = new JRadioButton("Addition");
JRadioButton subtraction = new JRadioButton("Subtraction");
JRadioButton multiplication = new JRadioButton("Multiplication");
JRadioButton division = new JRadioButton("Division");
JRadioButton all = new JRadioButton("All");

JRadioButton single = new JRadioButton("Single");
JRadioButton two = new JRadioButton("Double");
JRadioButton triple = new JRadioButton("Triple");
JRadioButton mix = new JRadioButton("Mix");

JRadioButton five = new JRadioButton("5");
JRadioButton ten = new JRadioButton("10");

private int type, digit, rounds;

public Screen3() {

JPanel firstButtonPanel = new JPanel();
    JPanel secondButtonPanel = new JPanel();
    ButtonGroup myFirstGroup = new ButtonGroup();
    ButtonGroup mySecondGroup = new ButtonGroup();

    myFirstGroup.add(addition);
    myFirstGroup.add(subtraction);
    myFirstGroup.add(multiplication);
    myFirstGroup.add(division);
    //myFirstGroup.add(all);

    mySecondGroup.add(single);
    mySecondGroup.add(two);
    mySecondGroup.add(triple);
    //mySecondGroup.add(mix);

    firstButtonPanel.setLayout(new FlowLayout());
    firstButtonPanel.add(addition);
    firstButtonPanel.add(subtraction);
    firstButtonPanel.add(multiplication);
    firstButtonPanel.add(division);
    //firstButtonPanel.add(all);

    secondButtonPanel.setLayout(new FlowLayout());
    secondButtonPanel.add(single);
    secondButtonPanel.add(two);
    secondButtonPanel.add(triple);
    //secondButtonPanel.add(mix);

    JPanel buttons = new JPanel();
    buttons.setLayout(new BorderLayout());
    buttons.add(selectionLabel, BorderLayout.NORTH);
    buttons.add(firstButtonPanel, BorderLayout.CENTER);
    buttons.add(secondButtonPanel, BorderLayout.SOUTH);

ButtonGroup myThirdGroup = new ButtonGroup();
    JPanel endButtons = new JPanel();

    myThirdGroup.add(five);
    myThirdGroup.add(ten);

    endButtons.add(five);
    endButtons.add(ten);

    endPanel.setLayout(new BorderLayout());
    endPanel.add(rounds, BorderLayout.NORTH);
    endPanel.add(endButtons, BorderLayout.CENTER);

    setLayout(new BorderLayout());
    add(buttons, BorderLayout.NORTH);

Selection sn = new Selection();

    addition.addActionListener(sn);
    subtraction.addActionListener(sn);
    multiplication.addActionListener(sn);
    division.addActionListener(sn);

    single.addActionListener(sn);
    two.addActionListener(sn);
    triple.addActionListener(sn);

    five.addActionListener(sn);
    ten.addActionListener(sn);
}

public int getType() {
    return type;
}

public int getDigit() {
    return digit;
}

public int getRounds() {
    return rounds;
}

public class Selection implements ActionListener {  
    public void actionPerformed(ActionEvent e) {
        if(addition.isSelected()) { 
            type = 1;
        }
        else if(subtraction.isSelected()) {
            type = 2;
        }
        else if(multiplication.isSelected())
            type = 3;
        else if(division.isSelected())
            type = 4;
        //else if(all.isSelected())
            //type = 5;

        if(single.isSelected()) {
            digit = 1;
            System.out.println("single");
        }
        else if(two.isSelected())
            digit = 2;
        else if(triple.isSelected())
            digit = 3;

        if(five.isSelected())
            rounds = 5;
        else if(ten.isSelected())
            rounds = 10;
    }
}

}

这是主要的类:

public class Driver {

public JFrame frame = new JFrame("Math Game");

public JPanel screens = new JPanel(new CardLayout());

int digit = 1;
int rounds = 1;
int type = 1;

Driver() {

}

public void show() {

    JPanel buttonPanel = new JPanel();
    JButton next = new JButton("Next");
    JButton previous = new JButton("Previous");
    buttonPanel.add(previous);
    buttonPanel.add(next);

    Screen1 screen1 = new Screen1();  
    Screen2 screen2 = new Screen2();
    Screen3 screen3 = new Screen3();

    screens.add(screen1, "welcome");
    screens.add(screen2, "next");
    screens.add(screen3, "selection");

    frame.add(screens);
    frame.add(buttonPanel, BorderLayout.PAGE_END);
    frame.setSize(400, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    next.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            CardLayout cl = (CardLayout)(screens.getLayout());
            cl.next(screens);
        }
    });

    previous.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            CardLayout cl = (CardLayout)(screens.getLayout());
            cl.previous(screens);
        }
    });
}

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Driver dr = new Driver();
            dr.show();
        }
    });
}

}

我只是尝试对System.out.println(screen3.getType())进行测试打印; 在show()或main中

使用具有模态的 JOptionPane / JDialog

阅读如何制作对话框

在示例中仅在关闭JDialog之后才打印:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDialog jd = new JDialog();
            jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            jd.setModal(true);
            jd.pack();
            jd.setVisible(true);

            System.out.println("Here");
        }
    });
}

在此示例中仅在关闭JOptionPane之后才打印:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JPanel panel=new JPanel();
            panel.add(new JLabel("Hello, world!"));

            JOptionPane.showMessageDialog(null, panel, "Panel Message",JOptionPane.PLAIN_MESSAGE);

            System.out.println("Here");
        }
    });
}

我知道我不应该以线性方式考虑Java,但是如何确保在用户更改了变量之后而不是之前获取变量?

使用模式后JDialog / JOptionPane ,你会简单地使用公共干将访问包含的类实例中的变量:

public class Test {
    public static void main(String[] args) {
       X x= new X();//will only return after dialog closed

       System.out.println(x.getY());
    }
}

class X {

    private int y=0;//will be assigned during dialog/joptionpanes life span

    public X() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
               //creates and shows the modal dialog/optionpane which will allow modification of variable y through some input/controls
            }
       });
    }

    public int getY() {
        return y;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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