gpt4 book ai didi

java - 如何找到组件属于哪个面板?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:54:21 25 4
gpt4 key购买 nike

我需要帮忙。

我有一个面板,我正在用对象填充它。这些对象各自在面板内创建一个面板,面板内填充有标签。可以说这是一个自制的 JTable。现在我需要制作一个右键单击菜单,在其中单击编辑/删除/等。它需要知道我点击了什么对象。

public class Aktivitet {

static JPanel sPanel;
static String navn;
static String kontakt;
static String event;
static String oprettet;
static String note;
static String deadline;
static int tilstand;

public Aktivitet(JPanel panel, String sNavn, String sKontakt, String sEvent, String sOprettet, String sNote, String sDeadline, int sTilstand) {
this.navn = sNavn;
this.kontakt = sKontakt;
this.event = sEvent;
this.oprettet = sOprettet;
this.note = sNote;
this.deadline = sDeadline;
this.tilstand = sTilstand;
this.sPanel = panel;
JPanel akPan = new JPanel();
JPanel fillerPanel = new JPanel();
if (tilstand == 0) akPan.setBackground(Color.GREEN);
if (tilstand == 1) akPan.setBackground(Color.YELLOW);
if (tilstand == 2) akPan.setBackground(Color.RED);
akPan.setLayout(new GridLayout(4,3));
akPan.setBorder(BorderFactory.createRaisedBevelBorder());
akPan.setSize(new Dimension(10000, 75));
akPan.setMaximumSize(new Dimension(10000, 75));
fillerPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
fillerPanel.setSize(new Dimension(10000, 1));
fillerPanel.setMaximumSize(new Dimension(10000, 1));
JLabel navnP = new JLabel();
JLabel kontaktP = new JLabel();
JLabel eventP = new JLabel();
JLabel oprettetP = new JLabel();
JLabel noteP = new JLabel();
JLabel deadlineP = new JLabel();
JLabel label_navn = new JLabel();
JLabel label_kontakt = new JLabel();
JLabel label_event = new JLabel();
JLabel label_oprettet = new JLabel();
JLabel label_note = new JLabel();
JLabel label_deadline = new JLabel();


navnP.setText("Aktivitetsnavn:");
navnP.setFont(new Font("dialog",Font.ITALIC,9));

kontaktP.setText("Kontaktperson:");
kontaktP.setFont(new Font("dialog",Font.ITALIC,9));

eventP.setText("Event:");
eventP.setFont(new Font("dialog",Font.ITALIC,9));

label_navn.setText(navn);
label_navn.setFont(new Font("monospaced",Font.BOLD,16));

label_kontakt.setText(kontakt);
label_kontakt.setFont(new Font("monospaced",Font.BOLD,16));

label_event.setText(event);
label_event.setFont(new Font("monospaced",Font.BOLD,16));

oprettetP.setText("Oprettet:");
oprettetP.setFont(new Font("dialog",Font.ITALIC,9));

noteP.setText("Evt. Note:");
noteP.setFont(new Font("dialog",Font.ITALIC,9));

deadlineP.setText("Deadline:");
deadlineP.setFont(new Font("dialog",Font.ITALIC,9));

label_oprettet.setText(oprettet);
label_oprettet.setFont(new Font("monospaced",Font.BOLD,16));

label_note.setText(note);
label_note.setFont(new Font("monospaced",Font.BOLD,16));

label_deadline.setText(deadline);
label_deadline.setFont(new Font("monospaced",Font.BOLD,16));
akPan.add(navnP);
akPan.add(kontaktP);
akPan.add(eventP);
akPan.add(label_navn);
akPan.add(label_kontakt);
akPan.add(label_event);
akPan.add(oprettetP);
akPan.add(noteP);
akPan.add(deadlineP);
akPan.add(label_oprettet);
akPan.add(label_note);
akPan.add(label_deadline);
panel.add(akPan);
panel.add(fillerPanel);
panel.addMouseListener(new MouseActionListener());
akPan.setVisible(true);
fillerPanel.setVisible(true);
}
public class MouseActionListener implements MouseListener {

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}
}

}

非常感谢!

最佳答案

您可以使用从 MouseEvent 本身检索生成事件的源组件:

public void mouseClicked(MouseEvent e) {
Object o = e.getSource();
}

您应该使用 instanceof 检查源类型并将对象转换为它:

if (o instanceof JLabel){
JLabel label = (JLabel)o;
}else if (o instanceof JPanel){
JPanel panel = (JPanel)o;
}

为了区分相同类型的不同组件,您可以使用 putClientProperty() 为它们添加一个属性。方法,然后检索它:

JLabel label = new JLabel();
label.putClientProperty("id", new Integer(10));

然后从事件处理程序中检索属性:

if (o instanceof JLabel){
JLabel label = (JLabel)o;
Integer labelId = (Integer)label.getClientProperty("id");
}

您可以使用 getParent()组件上的方法来找到它的父级等等...

关于java - 如何找到组件属于哪个面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7372562/

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