将actionListener添加到很多JButton

将actionListener添加到很多JButton

本文介绍了将actionListener添加到很多JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将actionListener添加到位于BigPanel内的各种按钮。 actionListener用于deleteAction方法。在我给出的代码中,我只将actionListener添加到一个btnBleedingPrecautions的按钮。现在,我想将actionListener添加到BigPanel中的所有按钮,其中按钮被计为20+。有没有什么方法可以用来添加actionListener,而不是把每个按钮都放在

I want to add actionListener to various of buttons that located inside BigPanel. The actionListener is for deleteAction method. In the code I gave, I only add actionListener to one button which is btnBleedingPrecautions. Now, I want to add the actionListener to all button inside BigPanel where the buttons are counted as 20+. Is there any method can be used to add the actionListener instead of put every button with

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");


推荐答案

创建一个添加/添加按钮的方法: p>

Create a method to make/add buttons:

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;
}

然后使用此方法创建按钮。例如:

Then use this method to create the buttons. For example:

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

如果您不想将侦听器添加到该按钮,则可以将null作为最后一个参数传递

You can pass null as the last parameter if you don't want to add the listener to that button.

注意:约定是用小写字母开始变量名,所以bigPanel而不是BigPanel。

Note: the convention is to begin variable names with a lower case letter, so "bigPanel" instead of "BigPanel".

这篇关于将actionListener添加到很多JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:06