0

actionlistener を使用して変数の値を変更する方法を理解するのに問題があります。

私のプログラムでは、ユーザーがいくつかのラジオボタンを選択して行った選択を保存する必要があります。

カード レイアウトを持つメイン クラスと、それぞれが異なるパネルであるいくつかのクラスがあります。パネルの 1 つに、内部クラスとして 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 のいずれか

4

1 に答える 1

3

モダリティを持つ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;
    }
}
于 2013-02-15T13:35:56.990 に答える