Java 在GridBagLayout中查找组件的行和列位置

Java 在GridBagLayout中查找组件的行和列位置,java,jbutton,gridbaglayout,Java,Jbutton,Gridbaglayout,我是一名高中生,正在制作一个抽认卡程序,用户可以在其中存储写入文本文件的集合和单词。这些集合以网格袋布局显示在主屏幕上 集合名称、主题和牌数是标签,而添加单词、播放和删除集合是按钮 当用户单击任何按钮时,他们应该能够与所选的组进行交互按钮都可以工作,但我遇到的问题是程序无法分辨哪个按钮属于哪个组。 如果有某种方法可以找到被单击按钮的列,我可以将其与文本文件对应,并在其上设置名称 按钮都是彼此的副本,不存储在数组中 我的按钮操作列表器: btnAddWords.addActionListener

我是一名高中生,正在制作一个抽认卡程序,用户可以在其中存储写入文本文件的集合和单词。这些集合以网格袋布局显示在主屏幕上

集合名称、主题和牌数是标签,而添加单词、播放和删除集合是按钮

当用户单击任何按钮时,他们应该能够与所选的组进行交互按钮都可以工作,但我遇到的问题是程序无法分辨哪个按钮属于哪个组。

如果有某种方法可以找到被单击按钮的列,我可以将其与文本文件对应,并在其上设置名称

按钮都是彼此的副本,不存储在数组中

我的按钮操作列表器:

btnAddWords.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent p) {     
   frmWords s = new frmWords(deckName, newXPoint, newYPoint);
   s.setVisible(true);      
   }
 });
我的按钮声明:

        JButton btnAddWords = new JButton("Add Words"); 
        btnAddWords.setOpaque(false);
        btnAddWords.setContentAreaFilled(false);
        btnAddWords.setBorder(null);
        btnAddWords.setBorderPainted(false);

        JButton btnPlay = new JButton("Play");
        btnPlay.setOpaque(false);
        btnPlay.setContentAreaFilled(false);
        btnPlay.setBorder(null);
        btnPlay.setBorderPainted(false);

        JButton btnDelete = new JButton("Delete Set");
        btnDelete.setOpaque(false);
        btnDelete.setContentAreaFilled(false);
        btnDelete.setBorder(null);
        btnDelete.setBorderPainted(false);
这是我的网格包设置:

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = i;
        layoutBackground.add(lblDeckName[i],gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = i;
        layoutBackground.add(new JLabel("      "),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx =2;
        gbc.gridy = i;
        layoutBackground.add(new JLabel (subject),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 3;
        gbc.gridy = i;
        layoutBackground.add(new JLabel("                   "),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 4;
        gbc.gridy = i;
        layoutBackground.add(new JLabel(strLineNumber2),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 5;
        gbc.gridy = i;
        layoutBackground.add(new JLabel("             |  "),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 6;
        gbc.gridy = i;
        layoutBackground.add(btnAddWords,gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 7;
        gbc.gridy = i;
        layoutBackground.add(new JLabel("  |  "),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 8;
        gbc.gridy = i;
        layoutBackground.add(layoutBackground.add(btnPlay),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 9;
        gbc.gridy = i;
        layoutBackground.add(new JLabel("  |  "),gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 10;
        gbc.gridy = i;
        layoutBackground.add(btnDelete,gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 11;
        gbc.gridy = i;
        layoutBackground.add(new JLabel("  |"),gbc);
如果有更好的方法来做这件事,我很想学习如何做,否则如果你能给我指出正确的方向,那将是令人惊讶的


谢谢堆!!!!:)

您应该在动作侦听器中包含这些信息。不需要检查它在布局中的位置。请你再详细说明一下好吗。我不太明白你怎么知道哪个按钮属于哪一组?您可以在创建动作侦听器时将其放入其中。我会尝试发布一个答案来说明这一点,但我不知道你是如何确定“集合”是什么。