Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将MouseListener添加到arraylist中的标签_Java_Mouseevent - Fatal编程技术网

Java 将MouseListener添加到arraylist中的标签

Java 将MouseListener添加到arraylist中的标签,java,mouseevent,Java,Mouseevent,这是我在这里的第一篇文章,我尝试搜索一个已有的主题,但找不到任何与数组列表相关的内容 我需要将addMouseListener设置为在ArrayList中生成的所有JLabel 有两个数组,smLabelList和llabellist。这些包含两种类型的JLabel,smBay和lrBay——它们是使用for循环生成并添加到我的面板中的 第一个问题是,向列表中的对象添加操作侦听器的最佳方法是什么 第二个问题是,getSource()==smBay还是smLabelList数组 非常感谢——我今年

这是我在这里的第一篇文章,我尝试搜索一个已有的主题,但找不到任何与数组列表相关的内容

我需要将addMouseListener设置为在
ArrayList
中生成的所有
JLabel

有两个数组,
smLabelList
llabellist
。这些包含两种类型的JLabel,
smBay
lrBay
——它们是使用for循环生成并添加到我的面板中的

第一个问题是,向列表中的对象添加操作侦听器的最佳方法是什么

第二个问题是,getSource()==smBay还是smLabelList数组

非常感谢——我今年刚接触java,只学了几个月

package Despatch;

import java.util.ArrayList;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class GUI extends JPanel implements ActionListener {

    JPanel buttonPnl;
    JPanel topPnl;
    JPanel bottomPnl;
    JLabel smBay;
    JButton boxButton; //creates variable
    JButton tubeButton;
    JButton envelopeButton;
    JButton clearButton;
    JButton saveButton;
    JButton loadButton;
    JButton chargeButton;
    JButton totalChargeButton;

    int smCount = 0;
    int lrCount = 0;
    double currentCharge;

    private ArrayList<Parcel> smParcel = new ArrayList<Parcel>(9);
    private ArrayList<Parcel> lrParcel = new ArrayList<Parcel>(4);

    //array list for bay JLabels
    List<JLabel> smLabelList = new ArrayList<JLabel>();
    List<JLabel> lrLabelList = new ArrayList<JLabel>();

    //constructor to draw GUI
    GUI() {

        // draw top panel for small/medium parcels
        topPnl = new JPanel(new GridLayout(3, 3));
        topPnl.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        topPnl.setOpaque(true);

        for (int i = 0; i < (3 * 3); i++) {

            JLabel smBay = new JLabel();
            smBay.setBorder(BorderFactory.createLineBorder(Color.BLUE));

            smLabelList.add(smBay);
            topPnl.add(smBay);
        }

        // draw bottom panel for large parcels
        bottomPnl = new JPanel(new GridLayout(2, 4));
        bottomPnl.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        bottomPnl.setOpaque(true);

        for (int i = 0; i < (1 * 4); i++) {

            JLabel lrBay = new JLabel();
            lrBay.setBorder(BorderFactory.createLineBorder(Color.BLUE));
            lrLabelList.add(lrBay);
            bottomPnl.add(lrBay);
        }

        //add actionlisteners to bays???
            for (int i = 0; i < 9; ++i) {
            smLabelList.get(i).addMouseListener(null);
        }



        buttonPnl = new JPanel(new GridLayout(8, 1));// create new button panel object

        boxButton = new JButton("Add Box Package");
        buttonPnl.add(boxButton);

        tubeButton = new JButton("Add Tube Package");
        buttonPnl.add(tubeButton); // draws tube button

        envelopeButton = new JButton("Add Envelope Package");
        buttonPnl.add(envelopeButton); // draws envelope button

        clearButton = new JButton("Clear");
        buttonPnl.add(clearButton); // draws envelope button

        saveButton = new JButton("Save");
        buttonPnl.add(saveButton); // draws save button

        loadButton = new JButton("Load");
        buttonPnl.add(loadButton);// draws load button;

        chargeButton = new JButton("Current Charge");
        buttonPnl.add(chargeButton);// draws charge button;

        totalChargeButton = new JButton("Total Charge");
        buttonPnl.add(totalChargeButton);// draws charge button;

        boxButton.addActionListener(this);
        tubeButton.addActionListener(this);
        envelopeButton.addActionListener(this);
        clearButton.addActionListener(this);
        saveButton.addActionListener(this);
        loadButton.addActionListener(this);
        chargeButton.addActionListener(this);
        totalChargeButton.addActionListener(this);



    }//end of construct

    public void actionPerformed(ActionEvent ae) {
        //find out which button has been pressed
        if (ae.getSource() == boxButton) {

            addBox();//calls the addBox method.

        } else if (ae.getSource() == tubeButton) {

            addTube();
        } else if (ae.getSource() == envelopeButton) {
            addEnvelope();
        } else if (ae.getSource() == clearButton) {
            clear();
        } else if (ae.getSource() == saveButton) {
            System.out.println("you pressed the save button");
        } else if (ae.getSource() == loadButton) {
            System.out.println("you pressed the load button");
        } else if (ae.getSource() == chargeButton) {
            currentCharge();

        } else {
            System.out.println("you pressed the total button");

        }

    }

    public void mouseEntered(MouseEvent me) {
    }

    public void mouseExited(MouseEvent me) {
    }

    public void mousePressed(MouseEvent me) {
    }

    public void mouseClicked(MouseEvent me) {
    }

    public void mouseReleased(MouseEvent me) {

        if (me.getSource() == smBay) {
            if (SwingUtilities.isLeftMouseButton(me)) {
                JOptionPane.showMessageDialog(null, "you pressed the left mouse button");
            } else if (SwingUtilities.isMiddleMouseButton(me)) {
                JOptionPane.showMessageDialog(null, "you pressed the middle mouse button");
            } else if (SwingUtilities.isRightMouseButton(me)) {
                JOptionPane.showMessageDialog(null, "you pressed the right mouse button");

            }
        } else {
            JOptionPane.showMessageDialog(null, "doesn't matter");
        }
    }

    public void addBox() {

        int id = 0;
        while (id == 0) {
            try {
                String idStr = JOptionPane.showInputDialog("Please enter Box ID");
                id = Integer.parseInt(idStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid ID");
            }
        }

        int dz = 0;
        while ((dz < 1) || (dz > 3)) {
            try {
                String dzStr = JOptionPane.showInputDialog("Please enter a delivery zone");
                dz = Integer.parseInt(dzStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid delivery zone between 1 and 3");
            }
        }
        char dzChar = '0';
        if (dz == 1) {
            dzChar = '1';
        } else if (dz == 2) {
            dzChar = '2';
        } else {
            dzChar = '3';
        }

        //function to convert dz integar to a char.
        int boxL = 0;
        while (boxL == 0) {
            try {
                String boxLStr = JOptionPane.showInputDialog("Please enter box length");
                boxL = Integer.parseInt(boxLStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid length");
            }
        }

        int boxW = 0;
        while (boxW == 0) {
            try {
                String boxWStr = JOptionPane.showInputDialog("Please enter box width");
                boxW = Integer.parseInt(boxWStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid width");
            }
        }

        int boxH = 0;
        while (boxH == 0) {
            try {
                String boxHStr = JOptionPane.showInputDialog("Please enter box height in cm");
                boxH = Integer.parseInt(boxHStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid height");
            }
        }

        if (boxH >= 150) //checks if box height is over 150cm
        {
            if (lrCount < 4) {// secondary validation to check that that large array has spare slot

                lrParcel.add(new Box(id, dzChar, boxL, boxW, boxH) {
                });

                JLabel lrBay = lrLabelList.get(lrCount); //create new label from labels array refercing lrCounter
                lrBay.setIcon((lrParcel.get(lrCount).getImage()));

                currentCharge = (currentCharge + lrParcel.get(lrCount).getCharge());
                lrCount++;

                for (Parcel value : lrParcel) {
                    System.out.println(value);

                }
            } else {
                JOptionPane.showMessageDialog(null, "Sorry, there is no room for this parcel!");

            }
        } else {
            if (smCount < 9) {

                smParcel.add(new Box(id, dzChar, boxL, boxW, boxH) {
                });

                JLabel smBay = smLabelList.get(smCount); //create new label from labels array refercing lrCounter
                smBay.setIcon((smParcel.get(smCount).getImage()));

                currentCharge = (currentCharge + smParcel.get(smCount).getCharge());
                smCount++;
                //loop to print contents of array - for testing only.
                for (Parcel value : smParcel) {
                    System.out.println(value);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Sorry, there is no room for this parcel!");
            }

        }

    }

    public void addTube() {

        int id = 0;
        while (id == 0) {
            try {
                String idStr = JOptionPane.showInputDialog("Please enter Tube ID");
                id = Integer.parseInt(idStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid ID");
            }
        }

        int dz = 0;
        while ((dz < 1) || (dz > 3)) {
            try {
                String dzStr = JOptionPane.showInputDialog("Please enter a delivery zone");
                dz = Integer.parseInt(dzStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid delivery zone between 1 and 3");
            }
        }
        char dzChar = '0';
        if (dz == 1) {
            dzChar = '1';
        } else if (dz == 2) {
            dzChar = '2';
        } else {
            dzChar = '3';
        }

        int tubeL = 0;
        while ((tubeL >= 250 || tubeL <= 0)) {
            try {
                String tubeLStr = JOptionPane.showInputDialog("Please enter tube length(cm)");
                tubeL = Integer.parseInt(tubeLStr);
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid length(cm)");
            }
        }

        if (tubeL >= 150) //checks if tube height is over 1500cm
        {
            if (lrCount < 4) {// secondary validation to check that that large array has spare slot

                lrParcel.add(new Tube(id, dzChar, tubeL) {
                });

                JLabel lrBay = lrLabelList.get(lrCount); //create new label from labels array refercing lrCounter
                lrBay.setIcon((lrParcel.get(lrCount).getImage()));

                currentCharge = (currentCharge + lrParcel.get(lrCount).getCharge());
                lrCount++;

                for (Parcel value : lrParcel) {
                    System.out.println(value);

                }
            } else {
                JOptionPane.showMessageDialog(null, "Sorry, there is no room for this parcel!");

            }
        } else {
            if (smCount < 9) {

                smParcel.add(new Tube(id, dzChar, tubeL) {
                });

                JLabel smBay = smLabelList.get(smCount); //create new label from labels array refercing lrCounter
                smBay.setIcon((smParcel.get(smCount).getImage()));

                currentCharge = (currentCharge + smParcel.get(smCount).getCharge());
                smCount++;

                //loop to print contents of array - for testing only.
                for (Parcel value : smParcel) {
                    System.out.println(value);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Sorry, there is no room for this parcel!");
            }

        }

    }

    public void addEnvelope() {

        {

            int id = 0;
            while (id == 0) {
                try {
                    String idStr = JOptionPane.showInputDialog("Please enter Envelope ID");
                    id = Integer.parseInt(idStr);
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid ID");
                }
            }

            int dz = 0;
            while ((dz < 1) || (dz > 3)) {
                try {
                    String dzStr = JOptionPane.showInputDialog("Please enter a delivery zone");
                    dz = Integer.parseInt(dzStr);
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid delivery zone between 1 and 3");
                }
            }
            char dzChar = '0';
            if (dz == 1) {
                dzChar = '1';
            } else if (dz == 2) {
                dzChar = '2';
            } else {
                dzChar = '3';
            }

            char envelopeS = '0';
            while (envelopeS != 's' && envelopeS != 'm' && envelopeS != 'l') {
                try {
                    String envelopeSStr = JOptionPane.showInputDialog("Please enter the envelope size(s,m,l)");
                    envelopeS = envelopeSStr.charAt(0);
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "Invalid input, please enter a valid size(s,m,l)");
                }
            }

            if (envelopeS == 'l') //checks if envelope height is over 1500cm
            {
                if (lrCount < 4) {// secondary validation to check that that large array has spare slot

                    lrParcel.add(new Envelope(id, dzChar, envelopeS) {
                    });

                    JLabel lrBay = lrLabelList.get(lrCount); //create new label from labels array refercing lrCounter
                    lrBay.setIcon((lrParcel.get(lrCount).getImage()));

                    currentCharge = (currentCharge + lrParcel.get(lrCount).getCharge());
                    lrCount++;

                    for (Parcel value : lrParcel) {
                        System.out.println(value);

                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Sorry, there is no room for this parcel!");

                }
            } else {
                if (smCount < 9) {

                    smParcel.add(new Envelope(id, dzChar, envelopeS) {
                    });

                    JLabel smBay = smLabelList.get(smCount); //create new label from labels array refercing lrCounter
                    smBay.setIcon((smParcel.get(smCount).getImage()));

                    currentCharge = (currentCharge + smParcel.get(smCount).getCharge());
                    smCount++;
                    //loop to print contents of array - for testing only.
                    for (Parcel value : smParcel) {
                        System.out.println(value);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Sorry, there is no room for this parcel!");
                }

            }

        }

    }

    public void currentCharge() {

        JOptionPane.showMessageDialog(null, "Total charge for current items is " + currentCharge);

    }

    public void clear() {


        //clears each bay, removes icon
        for (int i = 0; i < smCount; ++i) {
            smLabelList.get(i).setIcon(null);
        }

        //clears each bay, removes icon
        for (int i = 0; i < lrCount; ++i) {
            lrLabelList.get(i).setIcon(null);
        }

        smCount = 0;
        lrCount = 0;
        smParcel.clear();//cleaers the sm parcel array
        lrParcel.clear();// clears the lr parcel array

        currentCharge = 0;
    }
}
包裹寄送;
导入java.util.ArrayList;
导入java.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
类GUI扩展JPanel实现ActionListener{
JPanel按钮npnl;
JPanel-topPnl;
JPanel-bottomPnl;
JLabel-smBay;
JButton boxButton;//创建变量
JButton-tubeButton;
按钮信封按钮;
JButton clearButton;
JButton保存按钮;
JButton加载按钮;
按钮充电按钮;
JButton按钮;
int-smCount=0;
int lrCount=0;
双电流充电;
私有ArrayList smParcel=新ArrayList(9);
私有ArrayList lrParcel=新ArrayList(4);
//间隔jlabel的数组列表
List smLabelList=new ArrayList();
List lrLabelList=new ArrayList();
//构造函数来绘制GUI
GUI(){
//绘制小型/中型包裹的顶部面板
topPnl=新JPanel(新网格布局(3,3));
topPnl.setBorder(BorderFactory.createLineBorder(Color.BLUE));
topPnl.setOpaque(真);
对于(int i=0;i<(3*3);i++){
JLabel smBay=新的JLabel();
smBay.setboorder(BorderFactory.createLineBorder(Color.BLUE));
smLabelList.add(smBay);
topPnl.add(smBay);
}
//绘制大包裹的底部面板
bottomPnl=新的JPanel(新的网格布局(2,4));
bottomPnl.setboorder(BorderFactory.createLineBorder(Color.BLUE));
bottomPnl.setOpaque(真);
对于(int i=0;i<(1*4);i++){
JLabel lrBay=新的JLabel();
lrBay.setboorder(BorderFactory.createLineBorder(Color.BLUE));
lrLabelList.add(lrBay);
底部PNL.添加(lrBay);
}
//将actionlisteners添加到托架???
对于(int i=0;i<9;++i){
smLabelList.get(i).addMouseListener(null);
}
buttonPnl=newjpanel(newgridlayout(8,1));//创建新的按钮面板对象
boxButton=新的JButton(“添加框包”);
按钮NL.add(框按钮);
tubeButton=新JButton(“添加管包”);
buttonPnl.add(tube按钮);//绘制tube按钮
信封按钮=新的JButton(“添加信封包”);
buttonPnl.add(信封按钮);//绘制信封按钮
clearButton=新的JButton(“Clear”);
buttonPnl.add(clearButton);//绘制信封按钮
saveButton=新的JButton(“Save”);
buttonPnl.add(saveButton);//绘制保存按钮
loadButton=新的JButton(“加载”);
buttonPnl.add(loadButton);//绘制加载按钮;
chargeButton=新按钮(“当前充电”);
buttonPnl.add(chargeButton);//提取充电按钮;
totalChargeButton=新按钮(“总费用”);
buttonPnl.add(totalChargeButton);//提取充电按钮;
addActionListener(这个);
tubeButton.addActionListener(此);
envelopeButton.addActionListener(此);
clearButton.addActionListener(这个);
saveButton.addActionListener(此);
loadButton.addActionListener(此);
chargeButton.addActionListener(此);
totalChargeButton.addActionListener(此);
}//施工结束
已执行的公共无效行动(行动事件ae){
//找出按下了哪个按钮
如果(ae.getSource()==boxButton){
addBox();//调用addBox方法。
}else if(ae.getSource()==tubeButton){
addTube();
}else if(ae.getSource()==信封按钮){
addEnvelope();
}else if(ae.getSource()==clearButton){
清除();
}else if(ae.getSource()==saveButton){
System.out.println(“您按下了保存按钮”);
}else if(ae.getSource()==loadButton){
System.out.println(“您按下了加载按钮”);
}else if(ae.getSource()==chargeButton){
电流电荷();
}否则{
System.out.println(“您按下了总计按钮”);
}
}
公共无效mouseenterned(MouseEvent me){
}
public void mouseexitted(MouseEvent me){
}
public void mousePressed(MouseEvent me){
}
公共无效mouseClicked(MouseEvent me){
}
公共无效MouseEvent me(MouseEvent me){
如果(me.getSource()==smBay){
if(SwingUtilities.isLeftMouseButton(me)){
showMessageDialog(null,“您按下了鼠标左键”);
}否则,如果(欺诈行为,是我的鼠标按钮){
showMessageDialog(null,“您按下了鼠标中键”);
}else if(SwingUtilities.isRightMouseButton(me)){
showMessageDialog(null,“您按下了鼠标右键”);
}
}否则{
showMessageDialog(null,“无所谓”);
}
}
公共void addBox(){
int id=0;
while(id==0){
试一试{
字符串idStr=JOptionPane.showInputDialog(“请输入框ID”);
id=Integer.parseInt(idStr);
}捕获(编号格式异常)
for (JLabel label : smLabelList)
{
    label.addActionListener(myActionListener);
}
for (JLabel label : lrLabelList)
{
    label.addActionListener(myActionListener);
}