?? radiobuttongroup.java
字號:
import java.awt.*;
import java.util.*;
// 維護一組按鈕,這樣在任意時刻只有一個被選中
public class RadioButtonGroup extends Object
{
// 用來裝單選按鈕的動態可增長vector
protected Vector buttons;
// 遍歷上述Vector的枚舉器
protected Enumeration e;
// 創建一個新的RadioButtonGroup對象
public RadioButtonGroup()
{
buttons = new Vector();
}
// 將傳入的單選按鈕添加到vector中去
public void add(RadioButton2D rb)
{
buttons.add(rb);
}
// 得到當前所選中的單選按鈕
//如果沒有按鈕被選中的話則返回null
public RadioButton2D getSelection()
{
for(e = buttons.elements(); e.hasMoreElements(); )
{
RadioButton2D rb = (RadioButton2D)e.nextElement();
if(rb.isSelected())
{
return rb;
}
}
return null;
}
// 整組按鈕的可用/不可用狀態
public void setEnabled(boolean b)
{
for(e = buttons.elements(); e.hasMoreElements(); )
{
((RadioButton2D) e.nextElement()).setEnabled(b);
}
}
// 設置整個按鈕組的可見性
public void setVisible(boolean v)
{
for(e = buttons.elements(); e.hasMoreElements(); )
{
((RadioButton2D) e.nextElement()).setVisible(v);
}
}
// 更新按鈕組,讓除了傳入的按鈕之外的按鈕設置為未選中狀態
public void updateGroup(RadioButton2D rb)
{
for(e = buttons.elements(); e.hasMoreElements(); )
{
Object o = e.nextElement();
if(rb != o)
{
((RadioButton2D) o).setSelected(false);
}
}
}
// 繪制組中的每一個按鈕
public void paint(Graphics2D g2d)
{
for(e = buttons.elements(); e.hasMoreElements(); )
{
((RadioButton2D)e.nextElement()).paint(g2d);
}
}
// 在給定的位置繪制組中的每一個按鈕
public void paint(Graphics2D g2d, double dx, double dy)
{
for(e = buttons.elements(); e.hasMoreElements(); )
{
((RadioButton2D)e.nextElement()).paint(g2d, dx, dy);
}
}
} // RadioButtonGroup
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -