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

时间:2017-07-03 10:23:46

标签: java jbutton gridbaglayout

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

enter image description here

设置名称,主题和卡数是标签,而添加单词,播放和删除集是按钮。

当用户点击任何按钮时,他们应该能够与所选择的组进行交互。 按钮全部工作,但我遇到的问题是程序无法告诉哪个按钮属于哪个按钮。

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

按钮是彼此的所有副本,并且不存储在数组中。

我的按钮操作列表:

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

这是我的Grid Bag设置:

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

如果有更好的方法可以做到这一点,我很乐意学会如何做到这一点,否则,如果你能指出我正确的方向,那将是惊人的!

谢谢堆!!!! :)

0 个答案:

没有答案
相关问题