2016-10-13 4 views
0

EPOS 시스템에서 작업 중이며 프로그램의 일부로 ArrayList에 저장된 모든 항목의 GridLayout을 생성하고 있습니다. itemsList 내에서 모든 객체는 이름, 바코드 및 가격과 같은 필요한 멤버 변수와 함께 저장됩니다. 현재 그리드가 채워지도록 버튼을 만들었지 만 버튼은 액션이 적고 클래스에서 데이터를 처리하는 방법을 잘 모르겠습니다. 아마도 현재 항목 객체의 값을 할당하는 방법이있을 것입니다. 단추 위에 반복되는거야? 각 단추가 내 코드에 의해 만들어지며 "손으로 만든"것이 아닙니다. 관련 코드는 다음과 같습니다 : 객체 자신이 한 위치arrayList 객체에서 JButton에 값을 할당합니다.

보조 노트로
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 클래스입니다.

당신은 클래스가 ActionListener을 구현하게하고,의 actionListener가 모든 버튼에 할당 할 수 있습니다

답변

3

:

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의의 액션 청취자에, 당신이 경우 특정 작업을 할 수 있습니다.

+0

많은 분들께 감사드립니다 ** ** 매우 유용하지만 각 버튼이 클래스의 값에 액세스 할 수 있는지 확인하는 방법은 아직 확실하지 않습니다. 각 단추를 반복되는 현재 개체에 매핑하려고 시도했지만 거기에서 어디로 가야할지 확실하지 않습니다. – TooLateTheHero

+0

@TooLateTheHero - 잘 모르겠습니다. – ItamarG3

관련 문제