从对象 arrayList 为 JButtons 赋值
Assigning values to JButtons from object arrayList
我在 EPOS 系统上工作,作为程序的一部分,我正在为存储在 ArrayList 中的所有项目生成一个 GridLayout。在项目 ArrayList 中,所有对象都存储有它们必要的成员变量,例如名称、条形码和价格。我目前已经做到了填充网格,但是按钮没有动作,而且我很不确定如何处理来自 classes 的数据,是否有某种方法可以分配正在迭代到正在制作的按钮上的当前项目对象?由于每个按钮都是由我的代码制作的,而不是 "hand made"。相关代码如下:
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 class 是创建对象的地方。
您可以使 class 实现 ActionListener
,并分配 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 的动作侦听器中,您可以进行特定于案例的动作。
我在 EPOS 系统上工作,作为程序的一部分,我正在为存储在 ArrayList 中的所有项目生成一个 GridLayout。在项目 ArrayList 中,所有对象都存储有它们必要的成员变量,例如名称、条形码和价格。我目前已经做到了填充网格,但是按钮没有动作,而且我很不确定如何处理来自 classes 的数据,是否有某种方法可以分配正在迭代到正在制作的按钮上的当前项目对象?由于每个按钮都是由我的代码制作的,而不是 "hand made"。相关代码如下:
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 class 是创建对象的地方。
您可以使 class 实现 ActionListener
,并分配 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 的动作侦听器中,您可以进行特定于案例的动作。