2017-06-19 98 views
0

我試圖在編程器模式下編寫功能完整的Windows 7計算器。現在,我正在努力讓按鈕正常工作。所以我想要用於hexa的按鈕被禁用,直到選擇了十六進制的單選按鈕。因此,在dec或Bint模式下,A-F按鈕將被禁用,直到它被選中。需要禁用按鈕,取決於是否選中單選按鈕

下面是按鈕A:

JButton button_A = new JButton("A"); 
button_A.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     button_A.setEnabled(false); 
     hexDisable(); 
     textField.setText(textField.getText() + "A"); 
    } 
}); 

這裏是單選按鈕:

JRadioButton rButton_Hex = new JRadioButton("Hex"); 
rButton_Hex.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     hexDisable(); 
    } 
}); 
rButton_Hex.setBounds(9, 218, 54, 23); 
contentPane.add(rButton_Hex); 
Group1.add(rButton_Hex); 

這是將啓用和禁用相應的按鈕的方法。

public void hexDisable(){ 
    button_A.setEnabled(true); 
} 

我對於在Java中使用GUI非常新穎。

+1

如果我可以建議,也許是考慮爲不同的計算器模式創建按鈕工具欄或個人按鈕面板和簡單的隱藏(.setVisible(假) )那些你目前不需要的,並顯示你需要的(.setVisible(true))。 – DevilsHnd

+0

你可以:將按鈕組放置在某種「列表」中,以便更改它們的狀態;您可以:將'List'放入某種'Map'中以便管理。 – MadProgrammer

回答

0

只是要setOnCheckedChangeListener您RadioGroup中

RadioGroup radG = (RadioGroup) findViewById(R.id.yourRadioGroup);   
radG.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     // checkedId is the RadioButton selected 
     switch(checkedId) 
     { 
     case R.id.hexa:  
     //enable or disable button 
     break; 

     case R.id.normal: 
     //enable or disable button   
     break; 

     case R.id.other: 
     //enable or disable button  
     break; 
     } 
    } 
}); 
+1

問題是關於Swing,而不是Android。 – Mordechai

0
Import Javax.swing.Jframe; 
Private JRadioButton rdbtnOn=null; 
Private JRadioButton rdbtnOff=null; 
Private ButtonGroup bg = new ButtonGroup(); 
bg.add(rdbtnOn); 
bg.add(rdbtnOFF); 
+2

請添加一些文字來解釋您的答案 – EBH