Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以将INTEGER签名分配给JButton对象?_Java_Swing_User Interface - Fatal编程技术网

Java 是否可以将INTEGER签名分配给JButton对象?

Java 是否可以将INTEGER签名分配给JButton对象?,java,swing,user-interface,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.e

我正在创建一个动态数量的按钮,由于这个原因,我不能命名每个按钮,因为我想,而且每个按钮内的文本必须是空的,所以我不能使用它。另一方面,我需要用一个整数(甚至两个整数)来标识每个按钮

以下是我的特定课程的代码:

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

                }});

              }
        };
    }
包视图;
导入java.awt.Color;
导入java.awt.GridLayout;
导入java.awt.Label;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.Collections;
导入java.util.Iterator;
导入java.util.LinkedList;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入view.Nodes.Node;
公共类邻接矩阵窗口{
公共框架;
整数指数;
链接列表节点;
int rowNumber=1;
迭代器节点器;
JButton-btn;
公共邻接矩阵窗口(int索引、LinkedList节点){
这个。索引=索引;
这个。节点=节点;
sortNodes();
用于(节点:节点)
sortConnecteds(node.isConnectedToNodes);
nodeIter=nodes.iterator();
初始化();
应用();
}
私有void初始化(){
frame=新的JFrame();
框架.立根(100100358297);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(新的GridLayout(nodes.size()+1,nodes.size()+1));
}
public void insertFirstRowLabels()
{
对于(int i=0;i
另一方面,我需要用一个整数(甚至两个整数)来标识每个按钮

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

button.setActionCommand("1");
action命令是一个字符串,用于标识单击了哪个按钮,因此它可以是一个包含两个数字的字符串(您需要解析该字符串才能获得这两个数字)

然后在
ActionListener
中,从
ActionEvent
访问此数据:

String actionCommand = event.getActionCommand();

您可以创建一个继承
JButton
类的新类,并在该类中包含要存储的
Integer
值。 我已经用
JPanel
完成了这项工作。我希望在我的窗口上显示多个面板,其中包含不同的信息。因此,我创建了自己的对象,该对象继承了
JPanel
。 下面是一些示例代码:

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

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

自定义JPanel:

public class ScanBarcode extends JPanel {

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

希望能有所帮助。

你能给我写个例子吗?(如何在我的ActionListener中使用此actionCommand)@camickryea我知道如何使用parseInt和String.valueOf…但我的问题是我从未见过“String actionCommand=event.getActionCommand();”所以,如果你不介意为我做一个例子或一个链接来阅读它,那么当我像你在底部看到的那样写它时,我有nullpointexceptionRunnable changeButtonStatus=new Runnable(){public void run(){btn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent b){String actionCommand=btn.getActionCommand();System.out.println(actionCommand);}}}}};是否也可以使用相反的方法?我可以通过调用特定按钮的actionCommand来找到它吗?例如button.actionCommand(a).disable();
public class ScanBarcode extends JPanel {

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