2014-06-17 2 views
0

대학 과정에서 JPanel 과제를 수행하고 있으며 최종 출력에 문제가 있습니다. 패널 자체가 멋지게 보이지만 모든 것을 할 수없는 유일한 방법은 인쇄 할 줄을 표시하는 것입니다.(자바) JPanel 인쇄되지 않음

이름, 가격, 카테고리 및 수량이 모두 사용자가 입력하는 식료품 품목의 입력을 받도록 고안되었습니다. '장바구니에 추가'버튼을 클릭하면 이전에 입력 한 모든 정보가 표시됩니다.하지만 지금은 그렇지 않습니다.

여기 내가 사용 두 클래스의 :

MyCart.java

package assignment2; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 

public class MyCart extends JFrame { 

    private JPanel display; 
    private JLabel itemLabel; 
    private JTextField item; 
    private JLabel catLabel; 
    private JComboBox category; 
    private JLabel quanLabel; 
    private JTextField quantity; 
    private JLabel priceLabel; 
    private JTextField price; 

    private JPanel btns; 
    private JButton addBtn; 
    private JButton exitBtn; 

    private JPanel list; 
    private JList myList; 

    private ArrayList<Item> itemList; 

    public MyCart() { 

     itemList = new ArrayList(); 
     myList = new JList(itemList.toArray()); 

     this.setLayout(new GridLayout(2,1,5,5)); 

     display = new JPanel(); 
     display.setLayout(new GridLayout(5,2,5,5)); 

     itemLabel = new JLabel(" Item: "); 
     item = new JTextField(10); 
     catLabel = new JLabel(" Category: "); 
     category = new JComboBox(new String[]{"Meat", "Fruit/Vegetable", "Dairy", "Grains", "Sweets", "Other"}); 
     quanLabel = new JLabel(" Quantity: "); 
     quantity = new JTextField(10); 
     priceLabel = new JLabel(" Price: "); 
     price = new JTextField(10); 

     display.add(itemLabel); 
     display.add(item); 
     display.add(catLabel); 
     display.add(category); 
     display.add(quanLabel); 
     display.add(quantity); 
     display.add(priceLabel); 
     display.add(price); 

     this.add(display); 

     btns = new JPanel(); 
     btns.setLayout(new GridLayout(1,3,5,5)); 

     addBtn = new JButton("Add to Cart"); 
     exitBtn = new JButton("Exit"); 

     btns.add(addBtn); 
     btns.add(exitBtn); 

     this.add(btns); 

     list = new JPanel(); 
     list.setLayout(new FlowLayout()); 
     list.add(new JLabel("My Cart:")); 
     JScrollPane myScrollPane = new JScrollPane(myList); 
     list.add(myScrollPane); 

     setLayout(new BorderLayout()); 
     this.add(display,BorderLayout.NORTH); 
     this.add(btns,BorderLayout.CENTER); 
     this.add(list,BorderLayout.SOUTH); 

     //event listener object created 
     ButtonListeners buttonListener = new ButtonListeners(); 
     addBtn.addActionListener(buttonListener); 
     exitBtn.addActionListener(buttonListener); 

    } 

    public static void main(String[] args) { 
     MyCart cart = new MyCart(); 
     cart.setTitle("Cart <Adam>"); 
     cart.setSize(400,350); 
     cart.setLocationRelativeTo(null); 
     cart.setVisible(true); 
     cart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 

    //event listener class for adding 
    class ButtonListeners implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      switch(e.getActionCommand()) { 

       case "Add": String n = item.getText(); 
          String c = category.getSelectedItem().toString(); 
          double q = Double.valueOf(quantity.getText()); 
          double p = Double.valueOf(price.getText()); 
          Item myItem = new Item(); 
          myItem.setName(n); 
          myItem.setCategory(c); 
          myItem.setQuantity(q); 
          myItem.setPrice(p); 

          itemList.add(myItem); 
          myList.setListData(itemList.toArray()); 
          break; 

       case "Exit": System.exit(0); 

      } 
     } 
    } 
    //exit listener 
    class ExitListener implements ActionListener{ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      System.exit(0); 
     } 
    } 
} 

Item.java

package assignment2; 

public class Item { 

    private String name; 
    private String category; 
    private double price; 
    private double quantity; 

    public Item(String name, String category, double price, double quantity) { 

    } 

    public Item() { 

    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getCategory() { 
     return category; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 

    public double getPrice() { 
     return price; 
    } 

    public void setPrice(double price) { 
     this.price = price; 
    } 

    public double getQuantity() { 
     return quantity; 
    } 

    public void setQuantity(double quantity) { 
     this.quantity = quantity; 
    } 

    public double calcAmount() { 
     return quantity * price; 
    } 

    public String toString(){ 
     return(name + ", " + category + ", " + quantity + ", " + price + ", " + calcAmount()); 
    } 
} 

답변

3

당신은 사건을 검사하는 JButtonActionCommand을 추가하는 것을 잊었다 귀하의 성명서 ActionListener

이 전화를 할 때마다 switch(e.getActionCommand()) 청취자는 추가 된 버튼의 ActionCommand을 잡고 있으며, 추가되지 않으면 버튼 이름이 ActionCommand입니다.

솔루션 :

addBtn = new JButton("Add to Cart"); 
addBtn.setActionCommand("Add"); //will call the Add case actionListener 
+0

+1을, 조치 명령. – camickr

+0

고마워, 친구! :) – CSRadical

+0

@CSRadical 당신은 환영합니다 :)). –

1

로드하는 그러나 당신이 JList 제대로 ListModel를 사용하지 않는, 당신에게 당신의 실제 문제에 대한 솔루션을 제공하고있다.

모델의 데이터 변경은 ListModel을 통해 수행해야합니다. 따라서 ArrayList가 항목을 추적 할 필요가 없습니다. ListModel이 처리합니다.

그래서 기본 코드는 다음과 같아야합니다

DefaultListModel<Item> model = new DefaultListModel<Item>(); 
JList list = new JList(model); 

그런 다음 당신은 당신이 실제로하는 ListModel를 업데이트 JList의 항목을 추가 할 :

model.addElement(myItem); 
+0

그 코드가 아직 표시되지 않았으므로이 코드에서 사용한 형식은 지금까지 배웠던 것입니다. :피 – CSRadical