我正在尝试用 Java 构建一个小型库存应用程序,它根据卡片布局切换视图(或页面)。对于标准更改,用户将使用应用程序顶部的菜单,并且效果很好。
但是,在我的一个屏幕上,用户将输入一个项目 ID 号进行检查。如果在数据库中未找到该 ID 号,应用程序应切换到“新项目”页面。这是我不知所措的地方。我试图更改当前查看的卡片,但似乎没有得到它。
如果这是一个基本问题(以及写得不好的 Java :)),请原谅我,我在编写这个应用程序时正在自学 Java。附上我正在添加部分主类(InventoryTrackingSystem)和 GUI 类(试图更改视图的类)。
/ * ** * * MAIN 类* ** * ** * *** /
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;
}
}
}