简体   繁体   中英

Adding a JPanel to another JPanel

I want to add multiple JPanels to one JPanel . Then I want to take that JPanel and display it in a tab of a JTabbedPane . I attempted to do it a few times but eclipse won't let me run the program. It doesn't show any errors but the program always runs in debug mode for some reason. Is it something wrong with the code? (The code is a bit long sorry)

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class GuiApp {
    static class shelf implements Serializable {

        // declare books and variables
        static shelf[] book = new shelf[1000];
        Boolean overdue;
        Boolean checkedOut;
        int bookNum;
        String personName;
        String dueDate;
        int month;
        int date;
        int year;
        String dateCheckedOut;
        String bookName;
    }

    public static void main(String args[]) {
        // set L&F
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {

        }
        JFrame main = new JFrame();
        JTabbedPane tabs = new JTabbedPane();
        JPanel checkOutPanel = new JPanel(new GridBagLayout());
        JPanel checkInPanel = new JPanel(new GridBagLayout());
        JPanel checkedOutOverduePanel = new JPanel(new BorderLayout());
        JPanel assignNamesPanel = new JPanel(new BorderLayout());
        JPanel refreshSavePanel = new JPanel(new GridBagLayout());

        //misc
        Font f = new Font("Header", Font.BOLD, 24);

        GridBagConstraints gbc = new GridBagConstraints();

        //check out
        JLabel checkOutLabel = new JLabel("CheckOut");
        JLabel bookNumLabel = new JLabel("Book Number");
        JLabel personNameLabel = new JLabel("Person Name");

        final JTextField bookNumEntry = new JTextField(20);
        final JTextField personNameEntry = new JTextField(20);
        JButton checkOutButton = new JButton("Check out");

        checkOutLabel.setFont(f);

        gbc.gridx = 1;
        checkOutPanel.add(checkOutLabel,gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        checkOutPanel.add(bookNumLabel,gbc);
        gbc.gridx = 1;
        checkOutPanel.add(bookNumEntry,gbc);
        gbc.gridx = 0;
        gbc.gridy = 2;
        checkOutPanel.add(personNameLabel,gbc);
        gbc.gridx = 1;
        checkOutPanel.add(personNameEntry,gbc);
        gbc.gridx = 2;
        checkOutPanel.add(checkOutPanel,gbc);
        tabs.addTab("Check Out", checkOutPanel);


        //check in
        JLabel checkInLabel = new JLabel("Check In");
        JLabel bookNumCheckInLabel = new JLabel("Book Number");
        final JTextField bookNumCheckIn = new JTextField(20);
        JButton checkInButton = new JButton("Check In");

        checkInLabel.setFont(f);

        gbc.gridx = 1;
        gbc.gridy = 0;
        checkInPanel.add(checkInLabel,gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        checkInPanel.add(bookNumCheckInLabel,gbc);
        gbc.gridx = 1;
        checkInPanel.add(bookNumCheckIn,gbc);
        gbc.gridy = 2;
        checkInPanel.add(checkInButton,gbc);
        tabs.addTab("Check In",checkInPanel);


        //checked out overdue
        JPanel co = new JPanel(new GridBagLayout());
        JPanel co2 = new JPanel(new GridBagLayout());
        JPanel co3 = new JPanel();

        JLabel booksOutOverdueLabel = new JLabel("BOOKS OUT/OVERDUE");
        JLabel checkedOutLabel = new JLabel("Checked out");
        JLabel overdueLabel = new JLabel("Overdue");

        booksOutOverdueLabel.setFont(f);

        JTextArea checkedOutBooks = new JTextArea(50,53);
        JTextArea overdueBooks = new JTextArea(50,48);
        JScrollPane checkedOutTA = new JScrollPane(checkedOutBooks);
        JScrollPane overdueTA = new JScrollPane(overdueBooks);
        checkedOutTA.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        overdueTA.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        checkedOutBooks.setEditable(false);
        overdueBooks.setEditable(false);

        gbc.gridy = 0;
        co.add(checkedOutLabel,gbc);
        gbc.gridy = 1;
        co.add(checkedOutTA,gbc);
        gbc.gridy = 0;
        co2.add(overdueLabel,gbc);
        gbc.gridy = 1;
        co2.add(overdueTA,gbc);

        co3.add(booksOutOverdueLabel);

        checkedOutOverduePanel.add(co3,BorderLayout.NORTH);
        checkedOutOverduePanel.add(co,BorderLayout.WEST);
        checkedOutOverduePanel.add(co2,BorderLayout.EAST);

        tabs.addTab("Checked Out/Overdue",checkedOutOverduePanel);


        //assign book names
        JPanel an = new JPanel(new GridBagLayout());
        JPanel an2 = new JPanel();

        JLabel assignNamesLabel = new JLabel("Assign Book Names");
        JLabel bookNumberLabel = new JLabel("Book Number");
        JLabel nameOfBookLabel = new JLabel("Book Name");
        final JTextField bookNumber = new JTextField(20);
        final JTextField bookName = new JTextField(20);
        JButton assignName = new JButton("Assign");

        assignNamesLabel.setFont(f);

        gbc.gridy = 0;
        an.add(bookNumberLabel,gbc);
        an.add(bookNumber,gbc);
        gbc.gridy = 1;
        an.add(nameOfBookLabel,gbc);
        an.add(bookName,gbc);
        gbc.gridy = 2;
        gbc.gridx = 1;
        an.add(assignName,gbc);

        an2.add(assignNamesLabel);

        assignNamesPanel.add(an2,BorderLayout.NORTH);
        assignNamesPanel.add(an,BorderLayout.CENTER);

        tabs.addTab("Assign Book Names",assignNamesPanel);

        //refresh and save
        JButton saveButton = new JButton("Save");
        JButton refreshButton = new JButton("Refresh");
        refreshSavePanel.add(saveButton);
        refreshSavePanel.add(refreshButton);
        tabs.addTab("Refresh/save",refreshSavePanel);

        tabs.setTabPlacement(JTabbedPane.LEFT);

        main.add(tabs);
        main.setSize(1300,1100);
        main.setVisible(true);

    }
}

here is the problem you are adding panel to itself . adding container's parent to itself

checkOutPanel.add(checkOutPanel,gbc);//error
tabs.addTab("Check Out", checkOutPanel);

you should add component to container.but you are adding container to container .probably you are trying to add a button .

checkOutPanel.add(checkOutButton,gbc);
tabs.addTab("Check Out", checkOutPanel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM