2015-07-10 65 views
0

的我想的ActionListener添加到各個位於內側BigPanel該按鈕。 actionListener用於deleteAction方法。在我給出的代碼中,我只添加了actionListener到一個按鈕,它是btnBleeding注意事項。現在,我想將actionListener添加到BigPanel中的所有按鈕,其中按鈕計數爲20+。有沒有什麼方法可以用來添加的ActionListener,而不是把每一個按鈕,添加的ActionListener來了很多的JButton

buttonName.addActionListener(deleteAction);

JButton btnEcg = new JButton("ECG"); 
     BigPanel.add(btnEcg, "2, 1, default, fill"); 

     JButton btnWeigh = new JButton("Weight"); 
     BigPanel.add(btnWeigh, "4, 1, default, fill"); 

     JButton btnEkg_1 = new JButton("ECG"); 
     btnEkg_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      } 
     }); 
     BigPanel.add(btnEkg_1, "6, 1"); 

     JButton button_5 = new JButton("Weight"); 
     BigPanel.add(button_5, "8, 1, default, fill"); 

     JButton btnEkg = new JButton("ECG"); 
     BigPanel.add(btnEkg, "10, 1"); 

     JButton button_7 = new JButton("Weight"); 
     BigPanel.add(button_7, "12, 1, default, fill"); 

     JButton btnBloodPressure = new JButton("Blood pressure"); 
     BigPanel.add(btnBloodPressure, "2, 2"); 

     JButton btnLabs = new JButton("Labs"); 
     BigPanel.add(btnLabs, "4, 2"); 

     JButton button_4 = new JButton("Blood pressure"); 
     BigPanel.add(button_4, "6, 2"); 

     JButton button_12 = new JButton("Labs"); 
     BigPanel.add(button_12, "8, 2"); 

     JButton btnStressTest = new JButton("Stress test"); 
     BigPanel.add(btnStressTest, "10, 2"); 

     JButton button_13 = new JButton("Labs"); 
     BigPanel.add(button_13, "12, 2"); 

     JScrollBar bar2 = new JScrollBar(JScrollBar.VERTICAL); 
     BigPanel.add(bar2, "20, 1, 1, 23"); 

     JButton btnVitalSigns = new JButton("Vital signs"); 
     BigPanel.add(btnVitalSigns, "2, 3"); 

     JButton btnPostCathh = new JButton("Post Cath"); 
     BigPanel.add(btnPostCathh, "6, 3, default, fill"); 

     JButton btnCkTroponinI = new JButton("CK-MB"); 
     BigPanel.add(btnCkTroponinI, "8, 3"); 

     JButton button_6 = new JButton("Cardiac rhythm"); 
     BigPanel.add(button_6, "10, 3, default, fill"); 

     JButton btnCardiacRhythm = new JButton("Cardiac rhythm"); 
     BigPanel.add(btnCardiacRhythm, "2, 4"); 

     JButton button_2 = new JButton("Cardiac rhythm"); 
     BigPanel.add(button_2, "6, 4"); 

     JButton btnChestXray = new JButton("Chest X-ray"); 
     BigPanel.add(btnChestXray, "2, 5"); 

     JButton button_11 = new JButton("Chest X-ray"); 
     BigPanel.add(button_11, "6, 5"); 

     JButton btnPulseOximetry = new JButton("Pulse oximetry"); 
     BigPanel.add(btnPulseOximetry, "2, 6"); 

     JButton btnTpa = new JButton("Thrombolysis"); 
     btnTpa.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       UI3One thr = new UI3One(); 
        thr.setVisible(true); 
        thr.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 



      } 
     }); 
     BigPanel.add(btnTpa, "2, 8, default, fill"); 

     JButton btnPci = new JButton("PCI"); 
     BigPanel.add(btnPci, "6, 8, default, fill"); 

     JButton btnBleedingPrecautions = new JButton("Bleeding precautions"); 
     btnBleedingPrecautions.addActionListener(deleteAction); 
     BigPanel.add(btnBleedingPrecautions, "8, 8, default, fill"); 

     JButton btnHbaic = new JButton("HbAIC"); 
     BigPanel.add(btnHbaic, "10, 8"); 

     JButton button_10 = new JButton("Bleeding precautions"); 
     BigPanel.add(button_10, "12, 8"); 

     JButton btnSurgery = new JButton("Surgery"); 
     BigPanel.add(btnSurgery, "2, 9"); 

     JButton btnCath = new JButton("Cath"); 
     BigPanel.add(btnCath, "6, 9"); 

     JButton btnOxygen = new JButton("Oxygen"); 
     BigPanel.add(btnOxygen, "2, 11, default, fill"); 

     JButton btnAspirin = new JButton("Aspirin"); 
     BigPanel.add(btnAspirin, "4, 11, default, fill"); 

     JButton button_8 = new JButton("Oxygen"); 
     BigPanel.add(button_8, "6, 11"); 

     JButton btnSmokingBenzodiaepams = new JButton("Smoking benzodiaepams"); 
     BigPanel.add(btnSmokingBenzodiaepams, "8, 11"); 

     JButton button_9 = new JButton("Oxygen"); 
     BigPanel.add(button_9, "10, 11"); 

     JButton btnUptitrateBb = new JButton("Uptitrate BB"); 
     BigPanel.add(btnUptitrateBb, "12, 11"); 

     JButton btnBetablocked = new JButton("Beta-blockers"); 
     BigPanel.add(btnBetablocked, "2, 12"); 

     JButton btnAcei = new JButton("Morphin"); 
     BigPanel.add(btnAcei, "4, 12"); 

     JButton btnAcei_1 = new JButton("ACEI"); 
     BigPanel.add(btnAcei_1, "6, 12"); 

     JButton btnAntacid = new JButton("Antacid"); 
     BigPanel.add(btnAntacid, "8, 12"); 

     JButton button_14 = new JButton("ACEI"); 
     BigPanel.add(button_14, "10, 12"); 

     JButton btnWarfarin = new JButton("Warfarin"); 
     BigPanel.add(btnWarfarin, "12, 12"); 

     JButton btnNitrates = new JButton("Nitrates"); 
     BigPanel.add(btnNitrates, "2, 13"); 

     JButton btnCalciumChannelBlockers = new JButton("Calcium Channel Blockers"); 
     BigPanel.add(btnCalciumChannelBlockers, "4, 13"); 

     JButton btnPlavix = new JButton("Plavix"); 
     BigPanel.add(btnPlavix, "6, 13"); 

     JButton btnStollSoftener = new JButton("Stoll Softener"); 
     BigPanel.add(btnStollSoftener, "8, 13"); 

     JButton button_15 = new JButton("Nitrates"); 
     BigPanel.add(button_15, "10, 13"); 

     JButton btnLipidLoweringAgent = new JButton("Lipid lowering agent"); 
     BigPanel.add(btnLipidLoweringAgent, "12, 13"); 

     JButton btnAntithrombotics = new JButton("Antithrombotics"); 
     BigPanel.add(btnAntithrombotics, "2, 14"); 

     JButton btnGpiibiiia = new JButton("Anxiolytic"); 
     BigPanel.add(btnGpiibiiia, "4, 14"); 

     JButton btnNpo = new JButton("NPO"); 
     BigPanel.add(btnNpo, "6, 14"); 

     JButton btnIvHeparin = new JButton("IV Heparin"); 
     BigPanel.add(btnIvHeparin, "10, 14"); 

     JButton btnBedrest = new JButton("Bedrest"); 
     BigPanel.add(btnBedrest, "2, 16"); 

     JButton btnBedrest_1 = new JButton("Bedrest"); 
     BigPanel.add(btnBedrest_1, "6, 16"); 











     JButton btnNutrition = new JButton("Nutrition"); 
     BigPanel.add(btnNutrition, "10, 16"); 

     JButton btnEcho = new JButton("Echo"); 
     BigPanel.add(btnEcho, "6, 17"); 

     JButton btnEcho_1 = new JButton("Echo"); 
     BigPanel.add(btnEcho_1, "10, 17"); 

     JButton btnMedicalManagement = new JButton("Medical Management"); 
     BigPanel.add(btnMedicalManagement, "2, 20"); 

     JButton button_16 = new JButton("Medical Management"); 
     BigPanel.add(button_16, "6, 20"); 

     JButton button_17 = new JButton("Medical Management"); 
     BigPanel.add(button_17, "10, 20"); 

     JButton btnPatientEducation = new JButton("Patient education"); 
     BigPanel.add(btnPatientEducation, "2, 21"); 

     JButton button_18 = new JButton("Patient education"); 
     BigPanel.add(button_18, "6, 21"); 

     JButton button_19 = new JButton("Patient education"); 
     BigPanel.add(button_19, "10, 21"); 

     JButton btnOutpatientFollowup = new JButton("Outpatient follow-up"); 
     BigPanel.add(btnOutpatientFollowup, "2, 22"); 

     JButton button_20 = new JButton("Outpatient follow-up"); 
     BigPanel.add(button_20, "6, 22"); 

     JButton button_21 = new JButton("Outpatient follow-up"); 


      BigPanel.add(button_21, "10, 22"); 

      JScrollBar bar1 = new JScrollBar(JScrollBar.HORIZONTAL);    
      BigPanel.add(bar1, "2, 24, 15, 1"); 

回答

2

創建做出方法/添加按鈕:

protected JButton createButton(JPanel panel, String text, 
           String position, ActionListener listener) { 
    JButton b = new JButton(text); 
    panel.add(b, position); 
    if (listener != null) { 
     b.addActionListener(listener); 
    } 
    return b; 
} 

然後用這個方法來創建按鈕。例如:

JButton btnAntithrombotics = createButton(bigPanel, "Antithrombotics", 
           "2, 14", deleteAction); 

如果您不想將偵聽器添加到該按鈕,則可以將null作爲最後一個參數。

注:慣例是用小寫字母開頭的變量名,所以「bigPanel」而不是「BigPanel」。

+0

非常感謝你的先生。我使用了你提供的代碼,它的工作原理。但我對你的說法不清楚「的約定是用小寫字母開頭的變量名,所以‘bigPanel’而不是‘BigPanel’」因爲我用BigPanel代替bigPanel,它仍然工作。 – doraemon

+0

@doraemon這是'慣例',意味着人們通常使用的是什麼,而不是僅僅是有效的。 – rodit

+0

哦,我明白了。感謝@rodit爵士 – doraemon

1

創建所有的按鈕後,你可以通過BigPanel所有組件,並添加動作監聽器有循環:

for(Component c : BigPanel.getComponents()){ 
    if(c instanceof JButton)//check if the component is a button 
     ((JButton)c).addActionListener(listener);//cast to a JButton and add the action listener. 
}