2012-01-05 77 views
1

我試圖在Java中構建一個基於卡布局切換視圖(或頁面)的小型庫存應用程序。對於標準更改,用戶將使用應用程序頂部的菜單,並且工作正常。從另一個類更改Java卡布局中的活動「卡」

但是,我的屏幕中的一個用戶將輸入一個項目ID號進行檢查。如果在數據庫中找不到該ID號,應用程序應該切換到New Item頁面。這是我無所適從的地方。我試圖改變目前看到的卡,但似乎沒有得到它。

請原諒我,如果這是一個基本的問題(以及寫得不好的Java :)),我在教我自己的Java,因爲我寫這個程序。附加我添加了一部分主類(InventoryTrackingSystem)和GUI類(試圖更改視圖的類)。

/* ** * *主類* ** * ** * ***/

package inventorytrackingsystem; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.rmi.*; 

public class InventoryTrackingSystem implements ItemListener { 
    JPanel mainPanel; 
    JFrame frame; 

    JPanel cards = new JPanel(new CardLayout());; //a panel that uses CardLayout 

    final static String MAINPANEL = "Main"; 
    final static String CHECKITEMSPANEL = "Check Items"; 
    final static String NEWITEMPANEL = "New Item"; 
    final static String CHECKOUTITEMPANEL = "Check Out Item"; 
    final static String ITEMINFOPANEL = "Item Information"; 
    final static String LISTALLITEMSPANEL = "List All Items"; 
    JPanel comboBoxPane; 
    private JComboBox cb; 
    static String comboBoxItems[] = {MAINPANEL,CHECKITEMSPANEL,NEWITEMPANEL,CHECKOUTITEMPANEL,ITEMINFOPANEL,LISTALLITEMSPANEL}; 

    public static void main(String[] args) { 
    InventoryTrackingSystem ITS = new InventoryTrackingSystem(); 
    /* Use an appropriate Look and Feel */ 
    try { 
     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
    } catch (UnsupportedLookAndFeelException ex) { 
     ex.printStackTrace(); 
    } catch (IllegalAccessException ex) { 
     ex.printStackTrace(); 
    } catch (InstantiationException ex) { 
     ex.printStackTrace(); 
    } catch (ClassNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
    /* Turn off metal's use of bold fonts */ 
    UIManager.put("swing.boldMetal", Boolean.FALSE); 

    //Schedule a job for the event dispatch thread: 
    //creating and showing this application's GUI. 
// javax.swing.SwingUtilities.invokeLater(new Runnable() { 
//  public void run() { 
     ITS.createAndShowGUI(); 
//  } 
// }); 

    } 

    public void addComponentToPane(Container pane){ 
    //Put the JComboBox in a JPanel to get a nicer look. 
    comboBoxPane = new JPanel(); //use FlowLayout 
    cb = new JComboBox(comboBoxItems); 
    cb.setEditable(false); 
    cb.addItemListener(this); 
    comboBoxPane.add(cb); 
    cb.setVisible(false); 

    //Create the "cards". 
    JPanel main = new guiBuilder().buildGui("main"); 
    JPanel checkItems = new guiBuilder().buildGui("checkItems"); 
    JPanel newItems = new guiBuilder().buildGui("newItems"); 
    JPanel checkOutItems = new guiBuilder().buildGui("checkOutItems"); 
    JPanel itemInfo = new guiBuilder().buildGui("itemInfo"); 
    JPanel listAllItems = new guiBuilder().buildGui("listAllItems"); 

    //Create the panel that contains the "cards". 
    cards = new JPanel(new CardLayout()); 
    cards.add(main, MAINPANEL); 
    cards.add(checkItems, CHECKITEMSPANEL); 
    cards.add(newItems, NEWITEMPANEL); 
    cards.add(checkOutItems, CHECKOUTITEMPANEL); 
    cards.add(itemInfo, ITEMINFOPANEL); 
    cards.add(listAllItems, LISTALLITEMSPANEL); 

    pane.add(comboBoxPane, BorderLayout.PAGE_START); 
    pane.add(cards, BorderLayout.CENTER); 
    } 

    public void itemStateChanged(ItemEvent evt) { 
    CardLayout cl = (CardLayout)(cards.getLayout()); 
    cl.show(cards, (String)evt.getItem()); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event dispatch thread. 
    */ 
    private void createAndShowGUI() { 
     //Create and set up the window. 
     frame = new JFrame("Inventory Tracking System"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.setLayout(new BorderLayout()); 
     Menu m1 = new Menu("Options"); 

     MenuItem mi1_0 = new MenuItem("Main Page"); 
     mi1_0.setActionCommand("main"); 
     mi1_0.addActionListener(new menuListener()); 

     MenuItem mi1_1 = new MenuItem("Check Item"); 
     mi1_1.setActionCommand("checkItem"); 
     mi1_1.addActionListener(new menuListener()); 
     MenuItem mi1_2 = new MenuItem("Add New Item"); 
     mi1_2.setActionCommand("addItem"); 
     mi1_2.addActionListener(new menuListener()); 
     MenuItem mi1_3 = new MenuItem("List All Items"); 
     mi1_3.setActionCommand("listAllItems"); 
     mi1_3.addActionListener(new menuListener()); 
     MenuItem mi1_4 = new MenuItem("Check Out Item"); 
     mi1_4.setActionCommand("checkOutItem"); 
     mi1_4.addActionListener(new menuListener()); 
     MenuItem mi1_5 = new MenuItem("Exit"); 
     mi1_5.setActionCommand("exit"); 
     mi1_5.addActionListener(new menuListener()); 

     Menu m2 = new Menu("Help"); 
     MenuItem mi2_0 = new MenuItem("About"); 
     mi2_0.setActionCommand("about"); 
     mi2_0.addActionListener(new menuListener()); 

     m1.add(mi1_0); 
     m1.add(mi1_1); 
     m1.add(mi1_2); 
     m1.add(mi1_3); 
     m1.add(mi1_4); 
     m1.add(mi1_5); 

     m2.add(mi2_0); 

     MenuBar mb = new MenuBar(); 
     frame.setMenuBar(mb); 
     mb.add(m1); 
     mb.add(m2); 

     //Create and set up the content pane. 
     //InventoryTrackingSystem setGUI = new InventoryTrackingSystem(); 
     addComponentToPane(frame.getContentPane()); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 

     frame.setSize(780, 830); 

     frame.addWindowListener(new WindowAdapter(){ 
     public void windowClosing(WindowEvent we){ 
      System.exit(0); 
     } 
     public void windowClosed(WindowEvent we){ 
      System.exit(0); 
     } 
     }); 

    } 

    class menuListener implements ActionListener{ 
     public void actionPerformed(ActionEvent ev){ 
     String thisAction=ev.getActionCommand(); 

     if(thisAction.equals("main")){ 
      cb.setSelectedItem(MAINPANEL); 
     }else if(thisAction.equals("checkItem")){ 
      //change GUI 
      cb.setSelectedItem(CHECKITEMSPANEL); 
     }else if(thisAction.equals("addItem")){ 
      //change GUI 
      cb.setSelectedItem(NEWITEMPANEL); 
     }else if(thisAction.equals("checkOutItem")){ 
      //change GUI 
      cb.setSelectedItem(CHECKOUTITEMPANEL); 
     }else if(thisAction.equals("listAllItems")){ 
      //change GUI 
      cb.setSelectedItem(LISTALLITEMSPANEL); 
     }else if(thisAction.equals("exit")){ 
      System.exit(0); 
     }else if(thisAction.equals("about")){ 
      JOptionPane.showMessageDialog(frame, "About This App"); 
     } 
     } 
    } 

    public void swapView(String s){ 
    CardLayout cl = (CardLayout)(cards.getLayout()); 
    cl.show(cards, s); 
    } 
} 

/* ** * * GUI類* ** * ** * ***/

package inventorytrackingsystem; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.Image.*; 
import java.awt.image.BufferedImage.*; 
import javax.imageio.*; 
import com.sun.jimi.core.*; 


public class guiBuilder { 
    JLabel itemIdLabel; 
    JTextField itemID; 
    JButton checkIt; 
    JButton getSignature; 
    mySqlStuff sql=new mySqlStuff(); 

    public JPanel buildGui(String guiType){ 
    JPanel thisGUI; 
    if(guiType.equals("main")){ 
     thisGUI=mainGUI(); 
    }else if(guiType.equals("checkItems")){ 
     thisGUI=checkItemsGUI(); 
    }else if(guiType.equals("newItems")){ 
     thisGUI=newItemsGUI(); 
    }else if(guiType.equals("checkOutItems")){ 
     thisGUI=checkOutItemsGUI(); 
    }else if(guiType.equals("itemInfo")){ 
     thisGUI=itemInfoGUI(); 
    }else if(guiType.equals("listAllItems")){ 
     thisGUI=listAllItemsGUI(); 
    }else{ 
     thisGUI=mainGUI(); 
    } 

    return thisGUI; 
    } /* close buildGui() Method */ 

    private JPanel mainGUI(){ 
    JPanel thisPanel = new JPanel(); 

    return thisPanel; 
    } 

    private JPanel checkItemsGUI(){ 
    JPanel thisPanel = new JPanel(); 

    JPanel itemSection=new JPanel(); 
    JPanel exitSection=new JPanel(); 

    itemIdLabel=new JLabel("Enter/Scan Item ID"); 
    itemID=new JTextField(4); 
    itemID.addKeyListener(new myItemIdListener()); 

    checkIt=new JButton("Check Item"); 
    checkIt.addActionListener(new myItemCheckListener()); 

    itemSection.add(itemIdLabel); 
    itemSection.add(itemID); 
    itemSection.add(checkIt); 

    JButton exitButton=new JButton("Exit"); 
    exitButton.addActionListener(new exitButtonListener()); 

    exitSection.add(exitButton); 

    thisPanel.add(itemSection); 
    thisPanel.add(exitSection); 

    return thisPanel; 
    } 

    private JPanel newItemsGUI(){ 
    JPanel thisPanel = new JPanel(); 

    return thisPanel; 
    } 

    private JPanel checkOutItemsGUI(){ 
    JPanel thisPanel = new JPanel(); 

    return thisPanel; 
    } 

    private JPanel itemInfoGUI(){ 
    JPanel thisPanel = new JPanel(); 

    return thisPanel; 
    } 

    private JPanel listAllItemsGUI(){ 
    JPanel thisPanel = new JPanel(); 

    return thisPanel; 
    } 

    class myItemIdListener implements KeyListener{ 
    boolean keyGood=false; 
    public void keyPressed(KeyEvent keyEvent){ 
     int keyCode=keyEvent.getKeyCode(); 
     if((keyCode>=48 && keyCode<=57) || (keyCode>=96 && keyCode<=105)){ 
     keyGood=true; 
     } 
    } 
    public void keyReleased(KeyEvent keyEvent){ 
     int keyCode=keyEvent.getKeyCode(); 
     if((keyCode>=48 && keyCode<=57) || (keyCode>=96 && keyCode<=105)){ 
     printIt("Released",keyEvent); 
     }else if(keyCode==10){ 
     checkIt.doClick(); 
     } 
    } 
    public void keyTyped(KeyEvent keyEvent){ 
     int keyCode=keyEvent.getKeyCode(); 
     if((keyCode>=48 && keyCode<=57) || (keyCode>=96 && keyCode<=105)){ 
     printIt("Typed",keyEvent); 
     } 
    } 
    private void printIt(String title,KeyEvent keyEvent){ 
     int keyCode=keyEvent.getKeyCode(); 
     String keyText=KeyEvent.getKeyText(keyCode); 
     String currentData; 

     if(title.equals("Pressed")){ 
     keyGood=true; 
     }else if(title.equals("Released")){ 
     System.out.println(title+ " -> "+keyCode+"/"+keyText); 
     }else if(title.equals("Typed")){ 
     System.out.println(title+ " -> "+keyCode+"/"+keyText); 
     } 

     try{ 
     String text=itemID.getText(); 
     if(text.length()==4){ 
      checkIt.doClick(); 
     }else{ 
      System.out.println("currentlLength: "+itemID.getText().length()); 
     } 
     }catch(Exception ex){ 
     ex.printStackTrace(); 
     } 
    } 
    } 

/**** THIS IS WHERE THE SWAP VIEW IS CALLED ****/ 
    class myItemCheckListener implements ActionListener{ 
    public void actionPerformed(ActionEvent ev){ 
     String itemNum=itemID.getText(); 
     itemID.setText(""); 
     System.out.println("Checking ID#: "+itemNum); 
     ArrayList checkData=new ArrayList(sql.checkInventoryData(itemNum)); 
     if(checkData.get(0).toString().equals("[NULL]")){ 
     System.out.println("New Item -> "+checkData.get(0).toString()); 
     InventoryTrackingSystem ITS = new InventoryTrackingSystem(); 
     ITS.swapView("NEWITEMPANEL"); 
     }else{ 
     System.out.println("Item Exists -> "+checkData.get(0).toString()); 

     } 
     System.out.println(checkData); 
    } 
    } 
    class signaturePadButtonListener implements ActionListener{ 
    public void actionPerformed(ActionEvent ev){ 
     signaturePadStuff signature = new signaturePadStuff(); 
     while(signature.imageFileMoved==false){ 
     // wait for the signature to be collected and the image moved to the server. 
     } 
     System.out.println(signature.newFileName); 
    } 
    } 

    class exitButtonListener implements ActionListener{ 
    public void actionPerformed(ActionEvent ev){ 
     System.exit(0); 
    } 
    } 

    /** Returns an ImageIcon, or null if the path was invalid. */ 
    protected ImageIcon createImageIcon(String path, String description) { 
    java.net.URL imgURL = getClass().getResource(path); 
    if(imgURL != null){ 
     return new ImageIcon(imgURL, description); 
    }else{ 
     System.err.println("Couldn't find file: " + path); 
     return null; 
    } 
    } 

    class submitItemButtonListener implements ActionListener{ 
    public void actionPerformed(ActionEvent ev){ 
     String errorMsg = ""; 
     String[] formResults = new String[25]; 

    } 
    } 

    class clearFormButtonListener implements ActionListener{ 
    public void actionPerformed(ActionEvent ev){ 
     return; 
    } 
    } 

} 
+0

@Alex - 這使得完整意義......但是,如果我沒有創建一個InventoryTrackingSystem的實例,我一直得知無法找到方法swapView。 如何在創建實例時從GUI類調用該函數? – W3BGUY 2012-01-05 22:53:00

+0

好了,所以我改變guiBuilder' 到 '公共類guiBuilder擴展InventoryTrackingSystem' '公共類,現在它使得人們認爲交換的功能,但它仍然沒有實際上改變的是「卡」是鑑於... – W3BGUY 2012-01-05 23:19:04

+0

我終於設法使它工作。我最終在'InventoryTrackingSystem'類中移動了'guiBuilder'類。我相信這不是最好的解決方案,但它應該適用於第一版,所以我可以給我的老闆一些東西...... :)謝謝你的幫助@亞歷克斯... – W3BGUY 2012-01-06 16:23:11

回答

1

有了這個代碼在你的actionPerformed方法...

InventoryTrackingSystem ITS = new InventoryTrackingSystem(); 
ITS.swapView("NEWITEMPANEL"); 

...您所呼叫swapView在InventoryTrackingSystem的新實例上(這不是您在主窗口中創建的實例,並且UI在屏幕上顯示的實例)。

-edited-

你需要在你的動作監聽InventoryTrackingSystem的實例 例如,您可以將其存儲在myItemCheckListener的這樣一個成員變量:

class myItemCheckListener implements ActionListener{ 

    private InventoryTrackingSystem its; 

    // constructor takes its instance as argument 
    public myItemCheckListener(InventoryTrackingSystem its){ 
     // ...assigns it to the member variable 
     this.its = its; 
    } 

    public void actionPerformed(ActionEvent ev){ 
     // call swapView on the correct instance of InventoryTrackingSystem 
     its.swapView() 
    } 
} 

當然,因爲你的行動監聽器是在guiBuilder.buildGui()/ checkItemsGUI()中創建的,您也需要ITS實例。

BTW: 這是不是真的有必要建立新的guiBuilder情況是這樣的:

JPanel main = new guiBuilder().buildGui("main"); 
... 
JPanel listAllItems = new guiBuilder().buildGui("listAllItems"); 

,而不是你可以:

guiBuilder builder = new guiBuilder(); 
builder.buildGui("main"); 
builder.build("whatever"); 
+0

設置爲JFrame好吧,我有點看到它是如何工作的,但現在當我將構造函數放入'guiBuilder builder = new guiBuilder();'給出錯誤'類庫inventoryTrackingSystem.guiBuilder中的構造函數guiBuilder不能應用於給定的類型;''我嘗試添加'its'作爲參數,但沒有運氣。我開始認爲我可能需要廢棄很多這樣的代碼,從頭開始,多讀一點...... :) – W3BGUY 2012-01-06 13:51:42

相關問題