java中单选按钮里面给外面变量赋值问题
边卓琳 2017-10-13 02:26:07 public int model=1;
public void buildDialog() {
final JDialog dialog=new JDialog(this,"游戏模式",true);
//得到的对话框的容器面板对象,并设置布局
Container container=dialog.getContentPane();
container.setLayout(new GridLayout(3,1));
//第一行
ButtonGroup radioGroup=new ButtonGroup();//单选按钮组对象
JRadioButton radio1=new JRadioButton("经典2048",false);//初始不选中
radioGroup.add(radio1);
radio1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
model=1;
// setm(1);
// Game g= new Game();
// g.setmodel(2);
// m=1;//用来记录选择的模式
JOptionPane.showMessageDialog(null,"model="+model,"",JOptionPane.WARNING_MESSAGE);
dialog.dispose();//选完对话框自动消失
}
});
container.add(radio1);
//第二行
JRadioButton radio2=new JRadioButton("车之魂",false);//初始不选中
radioGroup.add(radio2);
radio2.addItemListener(new ItemListener() {//增加选项监听
public void itemStateChanged(ItemEvent event) {
model=2;//此处准备三种模式选择的后台赋值操作
// setm(2);
// Game g= new Game();
// g.setmodel(3);
JOptionPane.showMessageDialog(null,"model="+model,"",JOptionPane.WARNING_MESSAGE);
dialog.dispose();//选完对话框自动消失
}
});
container.add(radio2);
//第三行
JRadioButton radio3=new JRadioButton("炼体修身",false);//初始不选中
radioGroup.add(radio3);
radio3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
model=3;
// setm(3);
JOptionPane.showMessageDialog(null,"model="+model,"",JOptionPane.WARNING_MESSAGE);
dialog.dispose();//选完对话框自动消失
}
});
container.add(radio3);
JOptionPane.showMessageDialog(null,"model="+model,"外面",JOptionPane.WARNING_MESSAGE);
问题:打印出来的model为赋值后的,但是在“外面”的时候model值还是1没变,怎么让外面的model值跟着改变?