从对象arrayList向JButtons赋值 [英] Assigning values to JButtons from object arrayList

查看:51
本文介绍了从对象arrayList向JButtons赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在EPOS系统上工作,并且作为程序的一部分,我正在生成存储在ArrayList中的所有项目的GridLayout.在ArrayList项中,所有对象及其必需的成员变量(例如名称,条形码和价格)都存储在其中.我目前已经做到了,以便填充网格,但是按钮没有动作,而且我不确定如何处理类中的数据,也许有某种方法可以分配当前项目对象的值,即被迭代到正在制作的按钮上吗?因为每个按钮都是由我的代码制作的,而不是手工制作的".相关代码如下:

I'm working on an EPOS system, and as part of the program, I am generating a GridLayout of all items that are stored in an ArrayList. Within the items ArrayList, all the objects are stored with their necessary member variables, such as name, barcode and price. I have currently made it so that the grid is populated, however the buttons are action-less, and I am quite unsure how to handle the data from the classes, is there perhaps some way to assign the values of the current item object that is being iterated over onto the button being made? As each button is made by my code, and is not "hand made". The relevant code is as below:

public class gridCreator extends JFrame {
    ObjectCreator obj = new ObjectCreator();
    GridLayout itemGrid = new GridLayout();
    JFrame frame = new JFrame("pls work");
    static gridCreator instance;

public static void main(String[] args) throws FileNotFoundException {
    instance = new gridCreator();
    instance.createGrids();
    instance.createAndShowGUI();
}
public void createGrids() throws FileNotFoundException{
    obj.loadItems();
    itemGrid.setColumns(20);
    itemGrid.setRows(4);
    for (ObjectCreator.Item item : obj.items){
        addComponentsToPane(item);
    }
}
private void addComponentsToPane(ObjectCreator.Item item) {
    JButton button = new JButton(item.getName());
    frame.add(button);
}

请注意,ObjectCreator类是对象本身的生成位置.

As a side note, the ObjectCreator class is where the objects themselves are made.

推荐答案

您可以使该类实现ActionListener,并分配所有用于actionListener的按钮:

You can make the class implement ActionListener, and assign all the buttons that actionListener:

public class gridCreator extends JFrame implements ActionListener{
    ObjectCreator obj = new ObjectCreator();
    GridLayout itemGrid = new GridLayout();
    JFrame frame = new JFrame("pls work");
    static gridCreator instance;

public static void main(String[] args) throws FileNotFoundException {
    instance = new gridCreator();
    instance.createGrids();
    instance.createAndShowGUI();
}
public void createGrids() throws FileNotFoundException{
    obj.loadItems();
    itemGrid.setColumns(20);
    itemGrid.setRows(4);
    for (ObjectCreator.Item item : obj.items){
        addComponentsToPane(item);
    }
}
private void addComponentsToPane(ObjectCreator.Item item) {
    JButton button = new JButton(item.getName());
    frame.add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
    //do actions here   
}

然后,在JFrame的动作侦听器中,您可以进行大小写特定的动作.

Then, in the action listener of the JFrame, you can make a case specific action.

这篇关于从对象arrayList向JButtons赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆