gpt4 book ai didi

java - 跟踪鼠标单击的面板

转载 作者:行者123 更新时间:2023-12-02 07:03:32 25 4
gpt4 key购买 nike

这是我的目标代码::

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

}
});

最佳答案

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

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

关于java - 跟踪鼠标单击的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com