Java GUI,setVisible不工作,显示空白帧

时间:2017-08-01 06:45:03

标签: java swing user-interface jframe frame

我有这个库存系统应用程序,现在应该在其他窗口上工作但不能导致我遇到这个相当简单的问题。

按钮添加项目和库存转移显示空白窗口时应该显示我制作的另一个类。

 public class InventoryMainUI extends javax.swing.JPanel implements ActionListener {
        private boolean DEBUG = false;
        private JTable table;
        private JTextField searchitem;
        private TableRowSorter<TableModel> sorter;
        private JTextField jTextField1;
        public JButton STbutton;
        protected JButton b1, b2, b3;
        public InventoryMainUI() {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

            //Create a table with a sorter.
            TableModel model = new TableModel();
            sorter = new TableRowSorter<TableModel>(model);
            table = new JTable(model);
            table.setRowSorter(sorter);
            table.setPreferredScrollableViewportSize(new Dimension(500, 250));
            table.setFillsViewportHeight(true);

            //For the purposes of this example, better to have a single
            //selection.
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            //When selection changes, provide user with row numbers for
            //both view and model.

            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);

            //Add the scroll pane to this panel.
            add(scrollPane);

            //Create a separate form for searchitem and jTextField1
            JPanel mainpanel = new JPanel();

            JLabel srch = new JLabel("Search Item (case sensitive):");
            mainpanel.add(srch);
            searchitem = new JTextField();
            searchitem.setColumns(40);
            //Whenever searchitem changes, invoke newFilter.
            searchitem.getDocument().addDocumentListener(
                    new DocumentListener() {
                        public void changedUpdate(DocumentEvent e) {
                            newFilter();
                        }
                        public void insertUpdate(DocumentEvent e) {
                            newFilter();
                        }
                        public void removeUpdate(DocumentEvent e) {
                            newFilter();
                        }
                    });
            srch.setLabelFor(searchitem);
            mainpanel.add(searchitem);
            jTextField1 = new JTextField();
            mainpanel.add(jTextField1);
            jTextField1.setVisible(false);
            JButton aditem = new JButton("Add Item");
            mainpanel.add(aditem);
            aditem.addActionListener(this);

            JButton STransfer = new JButton("Stock Transfer");
            mainpanel.add(STransfer);
            STransfer.addActionListener(this);


            add(mainpanel);
        }


        public void actionPerformed(ActionEvent e){
            AddItemUI aditemui = new AddItemUI();
            aditemui.setVisible(true);
            aditemui.setSize(500,400);

        }

        public void ActionPerformed(java.awt.event.ActionEvent evt)
        {
            new StockTransfer().setVisible(true);
            //StockTransfer().setSize(550,650);
        }



        /** 
         * Update the row filter regular expression from the expression in
         * the text box.
         */
        private void newFilter() {
            RowFilter<TableModel, Object> rf = null;
            //If current expression doesn't parse, don't update.
            try {
                rf = RowFilter.regexFilter(searchitem.getText(), 0);
            } catch (java.util.regex.PatternSyntaxException e) {
                return;
            }
            sorter.setRowFilter(rf);
        }

        class TableModel extends AbstractTableModel {

            //,"Perishable","Quantity"
            private String[] columnName = {"Item","PIN","Delivered","Obtained","Expiry"};

            private Object[][] data = {
            {"AMD Ryzen 3 1300x", "2027",
             "7/12/17", new Integer(5), new Boolean(false)},
            {"Intel i7-6700K 8M Skylake", "4531",
             "7/12/17", new Integer(3), new Boolean(true)},
            {"Razer Blackwidow Chroma v2", "6742",
             "7/18/17", new Integer(2), new Boolean(false)},
            {" Nvidia GTX Titan Black", "9441",
             "7/13/17", new Integer(20), new Boolean(true)},
            {"CORSAIR Hydro Series H60", "1134",
             "7/13/17", new Integer(10), new Boolean(false)}
            };

            public int getColumnCount() {
                return columnName.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int clmns) {
                return columnName[clmns];
            }

            public Object getValueAt(int rows, int clmns) {
                return data[rows][clmns];
            }

            /*
             * JTable uses this method to determine the default renderer/
             * editor for each cell.  If we didn't implement this method,
             * then the last column would contain text ("true"/"false"),
             * rather than a check box.
             */
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }

            /*
             * Don't need to implement this method unless your table's
             * editable.
             */
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }

            /*
             * Don't need to implement this method unless your table's
             * data can change.
             */
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                                       + " to " + value
                                       + " (an instance of "
                                       + value.getClass() + ")");
                }

                data[row][col] = value;
                fireTableCellUpdated(row, col);

                if (DEBUG) {
                    System.out.println("New value of data:");
                    printDebugData();
                }
            }

            private void printDebugData() {
                int numRows = getRowCount();
                int numCols = getColumnCount();

                for (int i=0; i < numRows; i++) {
                    System.out.print("    row " + i + ":");
                    for (int j=0; j < numCols; j++) {
                        System.out.print("  " + data[i][j]);
                    }
                    System.out.println();
                }
                System.out.println("--------------------------");
            }
        }

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

            //Create and set up the content pane.
            InventoryMainUI newContentPane = new InventoryMainUI();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);

            //Display the window.
            frame.setSize(800,500);
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(InventoryMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(InventoryMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(InventoryMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(InventoryMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });

        }
    }

我一直试图在线寻找解决方案,请尽快提供帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

请发布您的AddItemUI和StockItem类的代码。

这可能是我们可以帮助你的唯一方式。

这两个类是否扩展了JFrame,并且在调用构造函数时是否填充了所需的组件?