2013-02-15 70 views
0

我在理解如何使用動作偵聽器來更改變量的值時遇到問題。如何使用動作偵聽器中設置的變量

在我的程序中,我需要通過選擇一些單選按鈕來存儲用戶的選擇。

我有一個卡片佈局的主類,然後幾個類,每個是不同的面板。在其中一個面板中,我有一些單選按鈕,並將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()還是主

+1

你會發布相關示例代碼?您是否研究過任何代碼示例? – 2013-02-15 13:27:12

+0

爲什麼添加一個actionlistener如果用戶必須(或不!!取決於他的選擇)勾選多個單選按鈕?爲什麼不添加一個說「發送表單/信息/完成」的按鈕並在那裏插入一個actionlistener? – Joetjah 2013-02-15 13:32:23

+0

@AaronKurtzhals我在上面添加了代碼。我做了很多研究,但仍然無法正確理解它。 – forumuser 2013-02-15 13:34:46

回答

3

使用JOptionPane/JDialog其中有modality

有上How to Make Dialogs

在示例這裏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; 
    } 
} 
+0

謝謝,這看起來不錯。雖然我不確定如何構建它,但如果我在卡片佈局中獲得了具有多個不同面板(屏幕)的框架,以便在面板之前顯示一些面板,我將更改所需的變量值從用戶輸入更改。在這種情況下使用卡布局是一個壞主意?將所有面板改爲對話框會更好嗎? – forumuser 2013-02-18 11:26:28

+0

CardLayous聽起來不錯 – 2013-02-18 13:22:01

+0

但我不能添加對話框到框架可以嗎?我有一個卡片佈局的主框架,不同的屏幕是添加到該框架的面板。對不起,但我很困惑! – forumuser 2013-02-21 10:11:35

相關問題