0

这是我的目标代码::

package build;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Table
{
    private ArrayList<Column> columns;
    private String NAME;
    private static int count=0;
    public Table() 
    {
        setColumns(new ArrayList<Column>());
        NAME=new String();

        this.NAME="Table"+count;
        count++;
    }

    public void AddColumn(Column column)
    {
        /*for(Column c: getColumns())
        {
           if(c.getname().equals(column.getname())){return;}
        }
        */
        getColumns().add(column);

    }
public int getCount(){return count;}
    public void SaveToFile() throws IOException
    {


        FileWriter fw = new FileWriter("C:/Users/Ashad/Desktop/text.txt", true);
        BufferedWriter bw = new BufferedWriter(fw);
         bw.write("\r\n");
        bw.write(this.NAME);
         bw.write("\r\n");

for(Column c: getColumns())
{
     bw.write("\r\n");
            bw.write(c.getname()+"  ");

            bw.write(c.getdatatype()+"  ");


     if(c.getPK()== true)
     {
            bw.write("true"+"  ");

     }    
        else
        {   bw.write("false"+"  ");

        }
    if(c.getNN()== true)
    {
        bw.write("true"+"   ");

    }     
    else
        {bw.write("false"+"  "); } 



}
bw.close();
    }

    public boolean DeleteColumn(String name)
    {
        for (Column c : getColumns())
        {
            if (c.getname().equals(name)) {return getColumns().remove(c);}
        }
        return false;
    }

    public void Delete()
    {

        for (Column c : getColumns())
        {
            c.setname(null);
            c.setdatatype(null);
            c.setNN(false);
            c.setNN(false);

        }

    }

    public void viewColumns()
    {
          System.out.append(NAME+"  ");

        for(Column c: getColumns())
      {
          System.out.append("\n");
          System.out.append(c.getname()+"  ");

          System.out.append( c.getdatatype()+"  ");

    if(c.getNN()== true)
    {
        System.out.print("true");
    }     
    else
        System.out.print("false");  
      }
    }

    public String getNAME() {

        return NAME;
    }

    public ArrayList<Column> getColumns() {
        return columns;
    }

    public void setColumns(ArrayList<Column> columns) {
        this.columns = columns;
    }

    public void setNAME(String text) {
        NAME=text;

    }

    public Column findPK()
    {

         for (Column c : getColumns())
            {
                if (c.getPK()==true) {return c;}
            }
        return null;

    }

    public void equal(Table temp) 
    {
        this.columns=temp.columns;
    }



}

我已将这个对象(即通过面板的表格)添加到我的 Jframe :: 中,如下所示

          final Table T=new Table();
          JPanel panel = new JPanel();
          panel.setBounds(197, 5+T.getCount(), 114+T.getCount(), 104+T.getCount());

          layeredPane.add(panel);

          final JLabel lblNewLabel = new JLabel(T.getNAME());
          panel.add(lblNewLabel);
          handleDrag(panel);
          table = new JTable();
          table.setShowVerticalLines(false);
          panel.add(table);
          table.setModel(new DefaultTableModel(
            new Object[][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
            },
            new String[] {
                "Name", "type"
            }
          ) {
            Class[] columnTypes = new Class[] {
                String.class, Object.class
            };
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
            boolean[] columnEditables = new boolean[] {
                false, true
            };
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }

我想添加一个鼠标侦听器,每当单击此表(对象)时,该表的名称应执行“ Console.writeline(Table.getNAME());” ::我想在对象时在控制台上打印对象的名称被点击。

我已通过 panel.setName(text); 为这些组件分配名称,即面板;

 String name=new String();

                   mntmOneToOne.addMouseListener(new MouseAdapter()
                   {
                        @Override
                        public void mouseClicked(MouseEvent arg0)
                        {
                            int count = arg0.getClickCount();
                            if (count == 1)
                            {
           // below Line is showing error as it doesnot know which panel is it about                        

                             name = panel.getName(); 
                            }

                        }
                   });
4

1 回答 1

2

您可以使用Component.setName()为 Swing 和 AWT 组件命名。

然后您可以Component.getName()在单击特定组件时使用打印名称。

于 2013-05-03T10:54:12.170 回答