2013-10-07 70 views
1

我想創建一個gui應用程序,使用JTabbedPane創建5個連接到數據庫的選項卡。每個選項卡包含一個面板,其中包含3個面板(一個面板用於容納JButton,另一個用於容納JLabels和JTextField,最後一個用於容納JTable,分別位於South,Center和North)。但是在創建標籤及其組件後,只顯示最後一個標籤並顯示不正確(它顯示所有5個標籤中的JLabel,JTextfields和JButton)。如果我刪除所有選項卡並保留一個選項卡,但如果選擇了多個選項卡,則會顯示在最後一個選項卡上。我不知道如何解決它。請幫幫我。如何將多個JPanel(3)放入使用JTabbedPane的JPanel中?

源代碼有點冗長,請耐心等待,並幫我檢查一下。如果有更好的方法來編寫代碼,請讓我知道。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.event.ActionListener; 
import java.sql.*; 
import java.util.Vector; 

public class Project_SalesDatabase extends JFrame { 

JTabbedPane tabbedPane = new JTabbedPane(); 
ImageIcon icon = createImageIcon("images/middle.gif"); 
JButton view = new JButton(" View "); 
JButton save = new JButton(" Save "); 
JButton addNew = new JButton(" Add New "); 
JButton exit = new JButton(" Exit Application "); 
JPanel displayBiscuitsPanel = new JPanel(); 
JPanel displayCookingPanel = new JPanel(); 
JPanel displayCustomersPanel = new JPanel(); 
JPanel displayEmployeesPanel = new JPanel(); 
JPanel displayProvisionsPanel = new JPanel(); 
JPanel displayButton = new JPanel(); 
JPanel displayContent = new JPanel((new GridLayout(10, 2))); 
JPanel displayTable = new JPanel(new GridLayout(1, 2)); 
JTextField biscuitName = new JTextField(); 
JTextField biscuitPrice = new JTextField(); 
JTextField biscuitCompany = new JTextField(); 
JTextField quantityOfBiscuitsBought = new JTextField(); 
JTextField quantityOfBiscuitsSold = new JTextField(); 
JTextField quantityInStock = new JTextField(); 
JTextField itemName = new JTextField(); 
JTextField itemPrice = new JTextField(); 
JTextField itemType = new JTextField(); 
JTextField quantityOfitemsBought = new JTextField(); 
JTextField quantityOfitemsSold = new JTextField(); 
JTextField firstName = new JTextField(); 
JTextField lastName = new JTextField(); 
JTextField customerAttendant = new JTextField(); 
JTextField customerAttendantPosition = new JTextField(); 
JTextField isCustomerADebtor = new JTextField(); 
JTextField orderNumber = new JTextField(); 
JTextField debtAmount = new JTextField(); 
JTextField address = new JTextField(); 
JTextField phoneNumber = new JTextField(); 
JTextField position = new JTextField(); 
JTextField age = new JTextField(); 
JTextField salary = new JTextField(); 
JTextField nextOfKin = new JTextField(); 
JTextField relationshipWithNextOfKin = new JTextField(); 
JTextField nextOfKinPhoneNumber = new JTextField(); 
JTextField itemCompany = new JTextField(); 

public static void main(String[] args) { 
    Project_SalesDatabase mainFrame = new Project_SalesDatabase(); 
    mainFrame.setLocationRelativeTo(null); 
    mainFrame.setVisible(true); 
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

public Project_SalesDatabase() { 

    setTitle(" Database App "); 
    setSize(1000, 500); 

    // Create the tab pages 
    biscuitsTable(); 
    cookingIngredientsTable(); 
    customersTable(); 
    employeesTable(); 
    provisionsTable(); 

    JPanel topPanel = new JPanel(); 
    topPanel.setLayout(new GridLayout(1, 3)); 
    getContentPane().add(topPanel); 
    topPanel.add(tabbedPane, BorderLayout.CENTER); 


    // Create tabs in tabbedPane 
    tabbedPane.addTab("Biscuits Database", icon, displayBiscuitsPanel, 
      "Allows you to view or Enter Data into the Biscuits Database"); 

    tabbedPane.addTab("Cooking Ingredients Database", icon, displayCookingPanel, 
      "Allows you to view or Enter Data into the Cooking Ingredients Database"); 

    tabbedPane.addTab("Customers Database", icon, displayCustomersPanel, 
      "Allows you to view or Enter Data into the Customers Database"); 

    tabbedPane.addTab("Employees Database", icon, displayEmployeesPanel, 
      "Allows you to view or Enter Data into the Employees Database"); 

    tabbedPane.addTab("Provisions Database", icon, displayProvisionsPanel, 
      "Allows you to view or Enter Data into the Provisions Database"); 

    //Enable scrolling in tabs. 
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 
} 

public static ImageIcon createImageIcon(String path) { 
    java.net.URL imgURL = Project_SalesDatabase.class.getResource(path); 
    if (imgURL != null) { 
     return new ImageIcon(imgURL); 
    } else { 
     System.err.println("Couldn't find file: " + path); 
     return null; 
    } 
} 

public final void biscuitsTable() { 
    displayBiscuitsPanel.setLayout(new BorderLayout()); 
    displayBiscuitsPanel.add(displayContent, BorderLayout.CENTER); 
    displayBiscuitsPanel.add(displayButton, BorderLayout.SOUTH); 
    displayBiscuitsPanel.add(displayTable, BorderLayout.NORTH); 

    displayContent.add(new JLabel("Biscuit Name")); 
    displayContent.add(biscuitName); 
    displayContent.add(new JLabel("Biscuit Price")); 
    displayContent.add(biscuitPrice); 
    displayContent.add(new JLabel("Biscuit Company")); 
    displayContent.add(biscuitCompany); 
    displayContent.add(new JLabel("Quantity Of Biscuits Bought")); 
    displayContent.add(quantityOfBiscuitsBought); 
    displayContent.add(new JLabel("Quantity Of Biscuits Sold")); 
    displayContent.add(quantityOfBiscuitsSold); 
    displayContent.add(new JLabel("Quantity In Stock")); 
    displayContent.add(quantityInStock); 

    displayButton.add(addNew); 
    displayButton.add(save); 
    displayButton.add(view); 
    displayButton.add(exit); 

    view.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      String db_url = "jdbc:odbc:ProjectSalesDatabase"; 
      String username = ""; 
      String password = ""; 
      Connection con = null; 

      try { 
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
       con = DriverManager.getConnection(db_url, username, password); 
      } catch (ClassNotFoundException | SQLException f) { 
       JOptionPane.showMessageDialog(rootPane, f.getMessage()); 
      } 

      Statement state = null; 
      ResultSet set = null; 

      try { 

       String Query = "SELECT * FROM Biscuits"; 
       state = con.createStatement(); 
       set = state.executeQuery(Query); 

       boolean nextrec = set.next(); 
       if (!nextrec) { 
        JOptionPane.showMessageDialog(rootPane, "No Record"); 
       } else { 
        Vector col = new Vector(); 
        Vector row = new Vector(); 

        ResultSetMetaData rsm = set.getMetaData(); 

        for (int x = 1; x <= rsm.getColumnCount(); x++) { 
         col.addElement(rsm.getColumnName(x)); 
        } 
        do { 
         row.addElement(getNextRow(set, rsm)); 
        } while (set.next()); 

        JTable tab = new JTable(row, col); 
        displayContent.removeAll(); 
        displayTable.removeAll(); 
        displayTable.add(new JScrollPane(tab), BorderLayout.CENTER); 

        validate(); 
       } 
       state.close(); 
      } catch (SQLException sql) { 
       JOptionPane.showMessageDialog(rootPane, sql.getMessage()); 
      } 
     } 
    }); 

    save.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 
       String Query = "INSERT INTO Biscuits VALUES ('" + biscuitName.getText() + "'," 
         + "   '" + biscuitPrice.getText() + "'," 
         + "   '" + biscuitCompany.getText() + "'," 
         + "   '" + quantityOfBiscuitsBought.getText() + "'," 
         + "   '" + quantityOfBiscuitsSold.getText() + "'," 
         + "   '" + quantityInStock.getText() + "')"; 

       String db_url = "jdbc:odbc:ProjectSalesDatabase"; 
       String username = ""; 
       String password = ""; 
       Connection con = null; 
       try { 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        con = DriverManager.getConnection(db_url, username, password); 
       } catch (ClassNotFoundException | SQLException f) { 

        JOptionPane.showMessageDialog(rootPane, f.getMessage()); 
       } 
       Statement state = con.createStatement(); 
       int rep = state.executeUpdate(Query); 
       if (rep == 0) { 
        JOptionPane.showMessageDialog(rootPane, "No Data Saved"); 
       } else { 
        JOptionPane.showMessageDialog(rootPane, "Data Saved"); 
       } 
      } catch (SQLException sqle) { 
       sqle.getMessage(); 
      } 
     } 
    }); 

    addNew.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 

       biscuitName.setText(""); 
       biscuitPrice.setText(""); 
       biscuitCompany.setText(""); 
       quantityOfBiscuitsBought.setText(""); 
       quantityOfBiscuitsSold.setText(""); 
       quantityInStock.setText(""); 

       displayTable.removeAll(); 
       displayContent.removeAll(); 

       displayContent.add(new JLabel("Biscuit Name")); 
       displayContent.add(biscuitName); 
       displayContent.add(new JLabel("Biscuit Price")); 
       displayContent.add(biscuitPrice); 
       displayContent.add(new JLabel("Biscuit Company")); 
       displayContent.add(biscuitCompany); 
       displayContent.add(new JLabel("Quantity Of Biscuits Bought")); 
       displayContent.add(quantityOfBiscuitsBought); 
       displayContent.add(new JLabel("Quantity Of Biscuits Sold")); 
       displayContent.add(quantityOfBiscuitsSold); 
       displayContent.add(new JLabel("Quantity In Stock")); 
       displayContent.add(quantityInStock); 

       validate(); 

      } catch (Exception f) { 
       f.getMessage(); 
      } 
     } 
    }); 

    exit.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
    }); 
} 

public final void cookingIngredientsTable() { 
    displayCookingPanel.setLayout(new BorderLayout()); 
    displayCookingPanel.add(displayContent, BorderLayout.CENTER); 
    displayCookingPanel.add(displayButton, BorderLayout.SOUTH); 
    displayCookingPanel.add(displayTable, BorderLayout.NORTH); 

    displayContent.add(new JLabel("Item Name")); 
    displayContent.add(itemName); 
    displayContent.add(new JLabel("Item Price")); 
    displayContent.add(itemPrice); 
    displayContent.add(new JLabel("Item Type")); 
    displayContent.add(itemType); 
    displayContent.add(new JLabel("Quantity Of Items Bought")); 
    displayContent.add(quantityOfitemsBought); 
    displayContent.add(new JLabel("Quantity Of Items Sold")); 
    displayContent.add(quantityOfitemsSold); 
    displayContent.add(new JLabel("Quantity In Stock")); 
    displayContent.add(quantityInStock); 

    displayButton.add(addNew); 
    displayButton.add(save); 
    displayButton.add(view); 
    displayButton.add(exit); 

    view.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      String db_url = "jdbc:odbc:ProjectSalesDatabase"; 
      String username = ""; 
      String password = ""; 
      Connection con = null; 
      try { 
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
       con = DriverManager.getConnection(db_url, username, password); 
      } catch (ClassNotFoundException | SQLException f) { 
       JOptionPane.showMessageDialog(rootPane, f.getMessage()); 
      } 

      Statement state = null; 
      ResultSet set = null; 

      try { 

       String Query = "SELECT * FROM CookingIngredients"; 
       state = con.createStatement(); 
       set = state.executeQuery(Query); 

       boolean nextrec = set.next(); 
       if (!nextrec) { 
        JOptionPane.showMessageDialog(rootPane, "No Record"); 
       } else { 
        Vector col = new Vector(); 
        Vector row = new Vector(); 

        ResultSetMetaData rsm = set.getMetaData(); 

        for (int x = 1; x <= rsm.getColumnCount(); x++) { 
         col.addElement(rsm.getColumnName(x)); 
        } 
        do { 
         row.addElement(getNextRow(set, rsm)); 
        } while (set.next()); 
        JTable tab = new JTable(row, col); 
        displayContent.removeAll(); 
        displayTable.removeAll(); 
        displayTable.add(new JScrollPane(tab), BorderLayout.CENTER); 
        validate(); 
       } 
       state.close(); 

      } catch (SQLException sql) { 
       JOptionPane.showMessageDialog(rootPane, sql.getMessage()); 
      } 
     } 
    }); 

    save.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 
       String Query = "INSERT INTO CookingIngredients VALUES ('" + itemName.getText() + "'," 
         + "   '" + itemPrice.getText() + "'," 
         + "   '" + itemType.getText() + "'," 
         + "   '" + quantityOfitemsBought.getText() + "'," 
         + "   '" + quantityOfitemsSold.getText() + "'," 
         + "   '" + quantityInStock.getText() + "')"; 

       String db_url = "jdbc:odbc:ProjectSalesDatabase"; 
       String username = ""; 
       String password = ""; 
       Connection con = null; 
       try { 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        con = DriverManager.getConnection(db_url, username, password); 
       } catch (ClassNotFoundException | SQLException f) { 
        JOptionPane.showMessageDialog(rootPane, f.getMessage()); 
       } 
       Statement state = con.createStatement(); 
       int rep = state.executeUpdate(Query); 
       if (rep == 0) { 
        JOptionPane.showMessageDialog(rootPane, "No Data Saved"); 
       } else { 
        JOptionPane.showMessageDialog(rootPane, "Data Saved"); 
       } 
      } catch (SQLException sqle) { 
       sqle.getMessage(); 
      } 
     } 
    }); 

    addNew.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 

       itemName.setText(""); 
       itemPrice.setText(""); 
       itemType.setText(""); 
       quantityOfitemsBought.setText(""); 
       quantityOfBiscuitsSold.setText(""); 
       quantityOfitemsSold.setText(""); 
       quantityInStock.setText(""); 

       displayTable.removeAll(); 
       displayContent.removeAll(); 

       displayContent.add(new JLabel("Item Name")); 
       displayContent.add(itemName); 
       displayContent.add(new JLabel("Item Price")); 
       displayContent.add(itemPrice); 
       displayContent.add(new JLabel("Item Type")); 
       displayContent.add(itemType); 
       displayContent.add(new JLabel("Quantity Of Items Bought")); 
       displayContent.add(quantityOfitemsBought); 
       displayContent.add(new JLabel("Quantity Of Items Sold")); 
       displayContent.add(quantityOfitemsSold); 
       displayContent.add(new JLabel("Quantity In Stock")); 
       displayContent.add(quantityInStock); 

       validate(); 

      } catch (Exception f) { 
       f.getMessage(); 
      } 
     } 
    }); 
} 

public final void customersTable() { 
    displayCustomersPanel.setLayout(new BorderLayout()); 
    displayCustomersPanel.add(displayContent, BorderLayout.CENTER); 
    displayCustomersPanel.add(displayButton, BorderLayout.SOUTH); 
    displayCustomersPanel.add(displayTable, BorderLayout.NORTH); 

    displayContent.add(new JLabel("First Name")); 
    displayContent.add(firstName); 
    displayContent.add(new JLabel("Last Name")); 
    displayContent.add(lastName); 
    displayContent.add(new JLabel("Customer Attendant")); 
    displayContent.add(customerAttendant); 
    displayContent.add(new JLabel("Customer Attendant's Position")); 
    displayContent.add(customerAttendantPosition); 
    displayContent.add(new JLabel("Is This Customer A Debtor?")); 
    displayContent.add(isCustomerADebtor); 
    displayContent.add(new JLabel("Order Number")); 
    displayContent.add(orderNumber); 
    displayContent.add(new JLabel("Debt Amount")); 
    displayContent.add(debtAmount); 
    displayContent.add(new JLabel("Customer's Address")); 
    displayContent.add(address); 
    displayContent.add(new JLabel("Customer's Phone Number")); 
    displayContent.add(phoneNumber); 

    displayButton.add(addNew); 
    displayButton.add(save); 
    displayButton.add(view); 
    displayButton.add(exit); 

    //button actions same as other methods. Removed it because of body character limits 
} 

public final void employeesTable() { 
    displayEmployeesPanel.setLayout(new BorderLayout()); 
    displayEmployeesPanel.add(displayContent, BorderLayout.CENTER); 
    displayEmployeesPanel.add(displayButton, BorderLayout.SOUTH); 
    displayEmployeesPanel.add(displayTable, BorderLayout.NORTH); 

    displayContent.add(new JLabel("First Name")); 
    displayContent.add(firstName); 
    displayContent.add(new JLabel("Last Name")); 
    displayContent.add(lastName); 
    displayContent.add(new JLabel("Position")); 
    displayContent.add(position); 
    displayContent.add(new JLabel("Age")); 
    displayContent.add(age); 
    displayContent.add(new JLabel("Salary")); 
    displayContent.add(salary); 
    displayContent.add(new JLabel("Employees's Address")); 
    displayContent.add(address); 
    displayContent.add(new JLabel("Employees's Phone Number(s)")); 
    displayContent.add(phoneNumber); 
    displayContent.add(new JLabel("Next of Kin")); 
    displayContent.add(nextOfKin); 
    displayContent.add(new JLabel("Relationship With Next of Kin")); 
    displayContent.add(relationshipWithNextOfKin); 
    displayContent.add(new JLabel("Next of Kin's Phone Number(s)")); 
    displayContent.add(nextOfKinPhoneNumber); 

    displayButton.add(addNew); 
    displayButton.add(save); 
    displayButton.add(view); 
    displayButton.add(exit); 

    //button actions same as other methods. Removed it because of body character limits 
} 

public final void provisionsTable() { 
    displayProvisionsPanel.setLayout(new BorderLayout()); 
    displayProvisionsPanel.add(displayContent, BorderLayout.CENTER); 
    displayProvisionsPanel.add(displayButton, BorderLayout.SOUTH); 
    displayProvisionsPanel.add(displayTable, BorderLayout.NORTH); 

    displayContent.add(new JLabel("Item Name")); 
    displayContent.add(itemName); 
    displayContent.add(new JLabel("Item Price")); 
    displayContent.add(itemPrice); 
    displayContent.add(new JLabel("Item Company")); 
    displayContent.add(itemCompany); 
    displayContent.add(new JLabel("Quantity Of Items Bought")); 
    displayContent.add(quantityOfitemsBought); 
    displayContent.add(new JLabel("Quantity Of Items Sold")); 
    displayContent.add(quantityOfitemsSold); 
    displayContent.add(new JLabel("Quantity In Stock")); 
    displayContent.add(quantityInStock); 

    displayButton.add(addNew); 
    displayButton.add(save); 
    displayButton.add(view); 
    displayButton.add(exit); 

    view.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      String db_url = "jdbc:odbc:ProjectSalesDatabase"; 
      String username = ""; 
      String password = ""; 
      Connection con = null; 
      try { 
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
       con = DriverManager.getConnection(db_url, username, password); 
      } catch (ClassNotFoundException | SQLException f) { 
       JOptionPane.showMessageDialog(rootPane, f.getMessage()); 
      } 

      Statement state = null; 
      ResultSet set = null; 

      try { 

       String Query = "SELECT * FROM Provisions"; 
       state = con.createStatement(); 
       set = state.executeQuery(Query); 

       boolean nextrec = set.next(); 
       if (!nextrec) { 
        JOptionPane.showMessageDialog(rootPane, "No Record"); 
       } else { 
        Vector col = new Vector(); 
        Vector row = new Vector(); 

        ResultSetMetaData rsm = set.getMetaData(); 

        for (int x = 1; x <= rsm.getColumnCount(); x++) { 
         col.addElement(rsm.getColumnName(x)); 
        } 
        do { 
         row.addElement(getNextRow(set, rsm)); 
        } while (set.next()); 

        JTable tab = new JTable(row, col); 
        displayContent.removeAll(); 
        displayTable.removeAll(); 
        displayTable.add(new JScrollPane(tab), BorderLayout.CENTER); 

        validate(); 
       } 
       state.close(); 
      } catch (SQLException sql) { 
       JOptionPane.showMessageDialog(rootPane, sql.getMessage()); 
      } 
     } 
    }); 

    save.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 
       String Query = "INSERT INTO Provisions VALUES ('" + itemName.getText() + "'," 
         + "   '" + itemPrice.getText() + "'," 
         + "   '" + itemCompany.getText() + "'," 
         + "   '" + quantityOfitemsBought.getText() + "'," 
         + "   '" + quantityOfitemsSold.getText() + "'," 
         + "   '" + quantityInStock.getText() + "')"; 

       String db_url = "jdbc:odbc:ProjectSalesDatabase"; 
       String username = ""; 
       String password = ""; 
       Connection con = null; 
       try { 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        con = DriverManager.getConnection(db_url, username, password); 
       } catch (ClassNotFoundException | SQLException f) { 
        JOptionPane.showMessageDialog(rootPane, f.getMessage()); 
       } 
       Statement state = con.createStatement(); 
       int rep = state.executeUpdate(Query); 
       if (rep == 0) { 
        JOptionPane.showMessageDialog(rootPane, "No Data Saved"); 
       } else { 
        JOptionPane.showMessageDialog(rootPane, "Data Saved"); 
       } 
      } catch (SQLException sqle) { 
       sqle.getMessage(); 
      } 
     } 
    }); 

    addNew.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 

       itemName.setText(""); 
       itemPrice.setText(""); 
       itemCompany.setText(""); 
       quantityOfitemsBought.setText(""); 
       quantityOfitemsSold.setText(""); 
       quantityInStock.setText(""); 

       displayTable.removeAll(); 
       displayContent.removeAll(); 

       displayContent.add(new JLabel("Item Name")); 
       displayContent.add(itemName); 
       displayContent.add(new JLabel("Item Price")); 
       displayContent.add(itemPrice); 
       displayContent.add(new JLabel("Item Company")); 
       displayContent.add(itemCompany); 
       displayContent.add(new JLabel("Quantity Of Items Bought")); 
       displayContent.add(quantityOfitemsBought); 
       displayContent.add(new JLabel("Quantity Of Items Sold")); 
       displayContent.add(quantityOfitemsSold); 
       displayContent.add(new JLabel("Quantity In Stock")); 
       displayContent.add(quantityInStock); 

       validate(); 

      } catch (Exception f) { 
       f.getMessage(); 
      } 
     } 
    }); 
} 

Vector getNextRow(ResultSet set, ResultSetMetaData rsm) { 
    Vector currentRow = new Vector(); 
    try { 
     for (int x = 1; x <= rsm.getColumnCount(); x++) { 
      switch (rsm.getColumnType(x)) { 
       case Types.VARCHAR: 
        currentRow.addElement(set.getString(x)); 
        break; 
       case Types.INTEGER: 
        currentRow.addElement(new Long(set.getLong(x))); 
        break; 
       default: 
        System.out.println("No column type known"); 
        break; 
      } 
     } 
    } catch (SQLException sqle) { 
     sqle.getMessage(); 
    } 
    return currentRow; 
} 
} 

我不知道爲什麼它不能正常工作。如果有人可以嘗試編譯/運行它,看看我在說什麼。謝謝。

+0

OMG,這麼多的Jtextfields和麪板......你認爲放在列表中? –

+0

@MaximShoustin請看看我的回答:) –

回答

0

I am trying to create a gui app using JTabbedPane to create 5 tabs. Each tab contains a panel with 3 panels in it

由於是Swing組件的性質,可以嵌入一個JComponent內另一除了JFrame
所以,你需要做的是創建一個JPanel。確保它有3列和1列的GridLayout(或者其他方式,如果這是您的需要)。
然後,將三個JPanels添加到具有網格佈局的此基礎JPanel。

最後,將基地JPanel添加到您的JTabbedPane。看看JPanel教程:http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html

+0

我將面板更改爲GridLayout,但它仍然有同樣的問題。我編輯了代碼以包含所有其他方法,以便您可以編譯並運行(如果您願意)看看我正在嘗試執行的操作。真的很感激。 –

+0

@JimNoah總是使用'SwingUtilities.invokeLater()'啓動一個Swing項目。您無法在主線程上啓動JFrame :) –

+0

您可以發佈您期望的內容和獲得的內容的截圖嗎? :) –

0

您可能需要使用getTabComponentAt(index)方法將組件添加到特定的標籤

相關問題