主要將每個實驗項目的分頁填入內容,並監聽客戶端的選項動作,且記錄下來,以供未來計算分數使用!
事實上這部分有些缺點。所有選項處於監聽狀態無法關閉,造成記錄的暫存檔也無法關閉,該如何於作答結束後關閉監聽呢?暫存檔無法關閉,使得在關閉程式時,無法刪除檔案!這有其優點,亦有其缺點嘍!
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import java.io.*;
public class ChoButtonGro{
static int ccc[][];
public static Container CreateChoseButtonGroup(final String elements[], final String elements2[], final String elements3) throws IOException{
final File fw= new File(***************);
final BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fw.toString(), true),"big5"));
String A[][]=new String[elements.length][2];
for(int i=0, n=elements.length; i<n; i++){
A[i][0]=elements[i];
}
for(int i=0, n=elements.length; i<n; i++){
A[i][1]=elements2[i];
}
JPanel panel = new JPanel(new GridLayout(0,1));
ButtonGroup group = new ButtonGroup();
/* 提出被選擇的項目 */
ActionListener crustActionListener = new ActionListener(){
String lastSelected;
public void actionPerformed(ActionEvent actionEvent){
AbstractButton aButton = (AbstractButton) actionEvent.getSource(); //這裡的 Source 是哪一個選項
String label = aButton.getText();
try{
for(int i=0, n=elements.length; i<n; i++){
if(aButton.getText()==elements[i]){
bfw.write(elements3+elements2[i]+" "+elements[i]); // 執行寫入
bfw.newLine(); //換行
String corses[][] = new String[elements.length][elements.length];
for(int h=0, g=elements.length; h<g; h++){
do{
corses[h][h]="0";
}while(corses[h][h]== null);
}
}
}
}
catch(IOException e){}
String msg;
try{
if (label.equals(lastSelected)) {
msg = "重選: ";
}
else {
msg = "選:";
}
lastSelected = label;
bfw.flush();
}
catch(IOException e){}
}
};
/* 產生題目的 panel 主要部分 */
JRadioButton aRadioButton;
for(int i=0, n=elements.length; i<n; i++){
aRadioButton = new JRadioButton(A[i][0]);
aRadioButton.addActionListener(crustActionListener);
panel.add(aRadioButton);
group.add(aRadioButton);
}
return panel;
}
}
沒有留言:
張貼留言