如何使用MouseListener获取数组的索引?

时间:2016-10-17 23:43:14

标签: java arrays mouselistener

我正在创建一个日历,每个日期由JTextArea表示。我为给定月份的日历上的所有日期创建了一个JTextAreas数组。

这是CalenderFrame类

import java.awt.Color;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;



public class CalendarFrame extends JFrame{

private static final int FRAME_HEIGHT = 700;
private static final int FRAME_WIDTH = 700;

private static final int NUM_OF_CELLS = 35;
private static final int DIM_OF_CELLS = 3;

private JLabel month;
private JPanel panel;
private CellBox[] allCells;
String text = "";




public CalendarFrame(){

    month = new JLabel("October");
    add(month, BorderLayout.CENTER);

    class CellListener implements MouseListener{


        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
            text = JOptionPane.showInputDialog("Set text for date: ");
            System.out.println(text);
            allCells[].get.setText(text);

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }



    }
    CellListener listener = new CellListener();

    createCellsPanel();
    for(int k= 0; k < NUM_OF_CELLS; k++){

        allCells[k].getCell().addMouseListener(listener);

    }
    setSize(FRAME_WIDTH, FRAME_HEIGHT);

}

public CellBox[] createAllCells(int numOfCells){

    CellBox[] cells = new CellBox[numOfCells];

    for(int i = 0; i < NUM_OF_CELLS; i++){
        cells[i].createCell(new JTextArea(DIM_OF_CELLS, DIM_OF_CELLS));
        Border border = BorderFactory.createLineBorder(Color.BLACK);
        cells[i].getCell().setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(10, 10, 10, 10)));
        cells[i].getCell().setEditable(false);

        cells[i].setCellNum(i);

    }
    return cells;


}

public void createCellsPanel(){

    allCells = createAllCells(NUM_OF_CELLS);
    panel = new JPanel();
    panel.setLayout(new GridLayout(5,7));
    for(int i = 0; i < NUM_OF_CELLS; i++){
        panel.add(allCells[i]);
    }

    add(panel, BorderLayout.SOUTH);


}

public void popOutEvent(){

}

}

主要课程

import javax.swing.JFrame;

public class CalendarFrameViewer {

public static void main(String[] args) {
    JFrame frame = new CalendarFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

和CellBox类

import javax.swing.JTextArea;

public class CellBox {

private int cellNum;
private int date;
private JTextArea cell;

public void setCellNum(int num){
    cellNum = num;
}

public void setDate(int date){
    this.date = date;
}

public void createCell(JTextArea cell){
    this.cell = cell;
}

public int getCellNum(){
    return cellNum;
}

public int getDate(){
    return date;
}

public JTextArea getCell(){
    return cell;
}
}

我希望能够单击一个单元格并在单元格中设置文本。我如何检索单元格编号或索引编号?

1 个答案:

答案 0 :(得分:0)

您的问题是(我认为)如何检索已点击的组件的单元格编号?如果是这样,最简单的解决方案是拥有一个特定于每个文本区域的鼠标侦听器。

例如,您可以使用以下方法:

private JTextArea makeCell(final int id) {
    JTextArea cell = new JTextArea(DIM_OF_CELLS, DIM_OF_CELLS));
    cell.setBorder(...);
    cell.setEditable(false);
    cell.addMouseListener(new MouseAdaptor() {
        public void mouseClicked(MouseEvent ev) {
            String text = JOptionPane.showInputDialog("Set text for date: ");
            allCells[id].get.setText(text);      
        }
    });
    return cell;
}

但是我认为你的根本问题是你只有一半封装了CellBox类。不是使用getCell方法来访问JTextArea,而是最好在该类中创建组件以及提供鼠标侦听器。然后您根本不需要访问具有索引的单元格。事实上,我怀疑你根本不需要存储索引。

public class CellBox {
    private final JTextArea cell;

    public CellBox() {
        cell = new JTextArea(DIM_OF_CELLS, DIM_OF_CELLS));
        cell.setBorder(...);
        cell.setEditable(false);
        cell.addMouseListener(new MouseAdaptor() {
            public void mouseClicked(MouseEvent ev) {
                String text = JOptionPane.showInputDialog("Set text for date: ");
                cell.setText(text);      
            }
        });
    }
}