gpt4 book ai didi

java - 引用其他类添加MouseListener

转载 作者:行者123 更新时间:2023-12-01 11:20:45 25 4
gpt4 key购买 nike

我想将 MouseListener 添加到扩展 JPanel 的类中。 Listener 引用另一个类,在该类中创建 JPanel 本身并将其添加到 contentPane。通过在 JPanel 上按鼠标,它应该被删除。它运行良好,但我无法调用 repaint() 因为我的删除方法是静态的。我该如何解决这个问题?

这是扩展类:

public class PanelDraggable extends JPanel {

public PanelDraggable () {

.
.
.

MouseInputAdapter closeMouseAdapter = new CloseMouseHandler();
addMouseListener(closeMouseAdapter);

}


private class CloseMouseHandler extends MouseInputAdapter {

public void mouseClicked(MouseEvent e) {

TblManagement.RemoveTable();
}
}

}

在这个类中,我想创建面板并删除它:

public void AddTables() {

String dbShortName = combo.getSelectedItem().toString();
String dbName = prop.get(dbNamesFile, dbShortName);

int x = 50; int y = 150; int width = 150; int height = 220; //set Size and location

int [] indices = tblList.getSelectedIndices();

panel = new PanelDraggable [indices.length];

for (int i = 0; i < indices.length ; i++) {
String tblName = tblList.getModel().getElementAt(i).toString();

String sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE "+
"`TABLE_SCHEMA`='"+dbName+"' AND `TABLE_NAME`='"+tblName+"'";

JList<?> columnslist = new JList<String>();
columnslist.setModel(getListModel(dbName, sql, 1));
JScrollPane sc = new JScrollPane(columnslist);
panel[i] = new PanelDraggable(x, y , width, height, tblName, sc);

MouseInputAdapter mousehandler = new TableMouseHandler();
panel[i].addMouseListener(mousehandler);

mainPanel.add(panel[i]);
x+=15;
y+=20;

}
revalidate();
repaint();
}

private class TableMouseHandler extends MouseInputAdapter {

public void mouseEntered(MouseEvent e) {

removeComp = (JComponent) e.getComponent();
}
}

public static void RemoveTable() {

mainPanel.remove(removeComp);
}

感谢您的帮助。

最佳答案

您需要将 TableManagement 类的实例作为每个面板的参数,这样您就不必在静态上下文中执行此操作。另外,我删除了您正在创建的 mouseAdapter 的不必要的子类,并以更可接受的方式创建了它们。

public class PanelDraggable extends JPanel {

public PanelDraggable (..., final TableManagement tblManagement) {
.
.
.
addMouseListener(new MouseAdapter() {
@Override
public void (MouseEvent e) {
tblManagement.removeTable();
}
});

}
}

然后在您的 TableManagement 类中

public void AddTables() {

String dbShortName = combo.getSelectedItem().toString();
String dbName = prop.get(dbNamesFile, dbShortName);

int x = 50; int y = 150; int width = 150; int height = 220; //set Size and location

int [] indices = tblList.getSelectedIndices();

panel = new PanelDraggable [indices.length];

for (int i = 0; i < indices.length ; i++) {
String tblName = tblList.getModel().getElementAt(i).toString();

String sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE "+
"`TABLE_SCHEMA`='"+dbName+"' AND `TABLE_NAME`='"+tblName+"'";

JList<?> columnslist = new JList<String>();
columnslist.setModel(getListModel(dbName, sql, 1));
JScrollPane sc = new JScrollPane(columnslist);

// There is an extra argument here. Whatever your
// mainPanel used to be in the static method
panel[i] = new PanelDraggable(x, y , width, height, tblName, sc, mainPanel);
panel[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered() {
removeComp = (JComponent) e.getComponent();
}
});

mainPanel.add(panel[i]);
x+=15;
y+=20;

}
revalidate();
repaint();
}

public void RemoveTable() {
this.remove(removeComp);
}

旁注:您应该研究 Java 命名约定。只有类的第一个字符大写。

关于java - 引用其他类添加MouseListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252423/

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