我想将actionListener添加到位于BigPanel内的各个按钮。 actionListener用于deleteAction方法。在我给出的代码中,我只将actionListener添加到一个按钮,即btnBleedingPrecautions。现在,我想将actionListener添加到BigPanel内的所有按钮,其中按钮被计为20+。是否可以使用任何方法添加actionListener而不是使用
放置每个按钮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");
答案 0 :(得分: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"。
答案 1 :(得分: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.
}