是否可以为JButton对象分配Interger签名?

时间:2015-05-06 19:18:28

标签: java swing user-interface

我创建了一个动态数量的按钮,并且我无法按照自己的意愿为每个按钮命名,每个按钮内的文本也必须为空,所以我也不能使用它。另一方面,我需要通过整数(甚至两个整数)来识别每个按钮

在下面你有我的特定类的代码:

    package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;

import view.Nodes.Node;

public class AdjacencyMatrixWindow {

    public JFrame frame;
    int index;
    LinkedList<Node> nodes;
    int rowNumber=1;
    Iterator<Node> nodeIter;
    JButton btn;

    public AdjacencyMatrixWindow(int index,LinkedList<Node> nodes) {
        this.index=index;
        this.nodes=nodes;
        sortNodes();
        for (Node node : nodes)
            sortConnecteds(node.isConnectedToNodes);
        nodeIter = nodes.iterator();
        initialize();
        apply();
    }


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 358, 297);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(nodes.size()+1, nodes.size()+1));


    }

    public void insertFirstRowLabels()
    {
        for (int i=0;i<=nodes.size();i++)
        {
            if (i==0)
            {
                Label lbl = new Label(" ");
                lbl.setBackground(new Color(240,240,240));
                frame.getContentPane().add(lbl);
            }
            else
            {
                Label lbl = new Label(String.valueOf(i));
                frame.getContentPane().add(lbl);
            }
        }
    }

    public void insertRow(Node node)
    {
        for (int i=0;i<=nodes.size();i++)
        {
            if (i==0)
            {
                Label lbl = new Label(String.valueOf(rowNumber));
                frame.getContentPane().add(lbl);
                rowNumber++;
            }
            else
            {
                if (node.isConnectedToNodes.contains(node.getNodeByID(i)))
                {
                    btn = new JButton("T");
                    frame.getContentPane().add(btn);
                }
                else
                {
                    btn = new JButton(" ");
                    frame.getContentPane().add(btn);

                }
            }
        }
    }

    public int columnNumbers()
    {
        if (nodeIter.hasNext())
            return Integer.parseInt(nodeIter.next().getNodeID());
        return -1;
    }

    public void apply()
    {
        insertFirstRowLabels();
        for (int i=0;i<=nodes.size()-1;i++)
        {
            insertRow(nodes.get(columnNumbers()-1));
        }
    }

    public void sortNodes()
    {
        Collections.sort(nodes, new Nodes.IDComperator());
    }

    public void sortConnecteds(LinkedList<Node> node)
    {
        Collections.sort(node, new Nodes.IDComperator());
    }

    Runnable selectAndDraw = new Runnable() 
    {
          public void run() 
          {

              btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent b) {

                }});

              }
        };
    }
抱歉,代码写得不是很干净。

2 个答案:

答案 0 :(得分:2)

  

另一方面,我需要用整数(甚至两个整数)来识别每个按钮

您可以为按钮设置操作命令:

button.setActionCommand("1");

action命令是一个String,用于标识单击的按钮,因此它可以是包含两个数字的字符串(您需要解析String以获取这两个数字)。

然后在ActionListener中,您可以从ActionEvent

访问此数据
String actionCommand = event.getActionCommand();

答案 1 :(得分:0)

您可以创建一个继承JButton类的新类,并在类中包含要存储的Integer值。 我用JPanels做了这个。我希望在我的窗口上显示多个面板,并显示不同的信息。所以我创建了自己的对象,继承了JPanel。 这是一些示例代码:

添加JPanel(注意:holdingPanel只是当前窗口上的另一个JPanel)...

    ScanBarcode scanBarcodePanel = new ScanBarcode();
    scanBarcodePanel.setLabelText(barcodeLabel);
    scanBarcodePanel.setBarcodeText(initialBarcodeText);
    this.holdingPanel.add(scanBarcodePanel);
    this.revalidate();

重新验证对于添加组件后显示的所有内容非常重要。 setLabelTextsetBarcodeTextScanBarcode类中的自定义方法。

自定义JPanel:

public class ScanBarcode extends JPanel {

    public ScanBarcode() {
        super();
        initComponents();
    }
...
}

希望有所帮助。

相关问题