将鼠标事件添加到java中的数组创建的文本字段中

时间:2013-07-13 01:52:39

标签: java swing

这是按钮动作监听器的代码,按下此按钮后,将设置GridLayout大小,并在里面添加文本字段,但是当我尝试将鼠标事件添加到文本中时会出现问题(我想要的功能)当我按下proID [0],然后proName [0]将settext设置为“0”并且proID [1],然后proName [1]将settext设置为“1”,然后是全部)

static int k = 1;
static int i = 1;
static int countttt = 0;
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
    JTextField proID[] = new JTextField[i];
    JTextField proName[] = new JTextField[i];
    JTextField pUnitPrice[] = new JTextField[i];
    JTextField Qty[] = new JTextField[i];
    JTextField Linetotal[] = new JTextField[i];


    GridLayout experimentLayout = new GridLayout(k, 5, 1, 1);
    jPanel6.setLayout(experimentLayout);
    proID[countttt] = new JTextField("a" + countttt);
    proName[countttt] = new JTextField();
    pUnitPrice[countttt] = new JTextField();
    Qty[countttt] = new JTextField();
    Linetotal[countttt] = new JTextField();


    proID[countttt].addMouseListener(new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println(countttt);
            proName[countttt].setText(countttt);
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    });

    jPanel6.add(proID[countttt]);
    jPanel6.add(proName[countttt]);
    jPanel6.add(pUnitPrice[countttt]);
    jPanel6.add(Qty[countttt]);
    jPanel6.add(Linetotal[countttt]);
    i++;
    countttt++;
    k++;
    jPanel6.repaint();
    jPanel6.revalidate();


}

0 个答案:

没有答案