Domanda

Whenever I run this code the JFrame will show up, but none of my components will appear. I've done a few GUI projects before, but never had this problem before.

public class LibraryGUI {

    int x;
    Library library;
    ActionListener AL = null;
    BorderLayout mainLayout = new BorderLayout();
    GridLayout buttonLayout = new GridLayout(1, 4, 30, 30);
    GridLayout entryLayout = new GridLayout(2, 2, 1, 30);
    // below items will be in the main JFrame, where the library will be
    // displayed
    static JFrame mainLibrary;
    static JButton addButton = new JButton("Add an Item");
    static JButton checkInButton = new JButton("Check in an Item");
    static JButton checkOutButton = new JButton("Check out an Item");
    static JButton saveButton = new JButton("Save current library");
    static JList<Item> list;
    // below items will be in the "Add Item" JFrame
    static JFrame newItemFrame;
    static JButton addButton1 = new JButton("Add this Item");
    static JTextField titleJTF = new JTextField("Title of media");
    static JTextField formatJTF = new JTextField("Format of media");
    static JLabel titleJL = new JLabel("Title:");
    static JLabel formatJL = new JLabel("Format:");
    // below items will be in the "Check Out" JFrame
    static JFrame checkOutFrame;
    static JButton checkOutButton1 = new JButton("Check Out");
    static JTextField nameJTF = new JTextField("Name of Person");
    static JTextField dateJTF = new JTextField("Date of Check out");
    static JLabel nameJL = new JLabel("Name:");
    static JLabel dateJL = new JLabel("Date:");
    static JLabel titleJL1 = new JLabel("");
    // below items will be in the "Delete Confirmation" JFrame
    static JFrame deleteFrame;
    static JButton confirmButton = new JButton("Delete this Item");
    static JButton cancelDeleteButton = new JButton("Don't delete this Item");
    static JLabel confirmationJL = new JLabel();

    private void mainLibraryFrame() {

        mainLibrary = new JFrame("Lending Library");
        mainLibrary.setLayout(mainLayout);
        mainLibrary.setSize(500, 600);
        addButton.setActionCommand("open add");
        addButton.addActionListener(AL);
        checkInButton.setActionCommand("check in");
        checkInButton.addActionListener(AL);
        checkOutButton.setActionCommand("open check out");
        checkOutButton.addActionListener(AL);
        saveButton.setActionCommand("save");
        saveButton.addActionListener(AL);
        JPanel buttonHolder = new JPanel(buttonLayout);
        buttonHolder.add(addButton);
        buttonHolder.add(checkInButton);
        buttonHolder.add(checkOutButton);
        buttonHolder.add(saveButton);
        JPanel overViewHolder = new JPanel(mainLayout);
        mainLibrary.add(list, BorderLayout.NORTH);
        mainLibrary.add(buttonHolder, BorderLayout.SOUTH);
        mainLibrary.setLocation(400, 200);
    }

    private void newItemFrame() {

        newItemFrame = new JFrame("Add Item");
        newItemFrame.setSize(200, 200);
        newItemFrame.setLocation(500, 300);
        addButton1.setActionCommand("add");
        addButton1.setSize(50, 30);
        addButton1.addActionListener(AL);
        JPanel newItemPanel = new JPanel(mainLayout);
        addButton1.setActionCommand("create");
        addButton1.setSize(50, 30);
        addButton1.addActionListener(AL);
        JPanel entryPanel = new JPanel(entryLayout);
        entryPanel.add(titleJL);
        entryPanel.add(titleJTF);
        entryPanel.add(formatJL);
        entryPanel.add(formatJTF);
        newItemPanel.add(entryPanel, BorderLayout.NORTH);
        newItemPanel.add(addButton1, BorderLayout.SOUTH);
        newItemFrame.add(newItemPanel);
    }

    private void checkOutFrame() {

        checkOutFrame = new JFrame("Check Out");
        checkOutFrame.setSize(200, 200);
        checkOutFrame.setLocation(500, 300);
        JPanel checkOutEntryPanel = new JPanel(entryLayout);
        checkOutEntryPanel.add(nameJL);
        checkOutEntryPanel.add(nameJTF);
        checkOutEntryPanel.add(dateJL);
        checkOutEntryPanel.add(dateJTF);
        JPanel checkOutMainPanel = new JPanel(mainLayout);
        checkOutMainPanel.add(checkOutEntryPanel, BorderLayout.NORTH);
        checkOutMainPanel.add(checkOutButton1, BorderLayout.SOUTH);
    }

    private void deleteFrame() {

        JPanel deletePanel = new JPanel(mainLayout);
        deletePanel.add(confirmationJL, BorderLayout.NORTH);
        deletePanel.add(confirmButton, BorderLayout.SOUTH);
    }

    public LibraryGUI(Library library) {

        this.library = library;
        list = new JList(this.library.container.toArray());
        mainLibraryFrame();
        newItemFrame();
        checkOutFrame();
        deleteFrame();
        mainLibrary.setVisible(true);
    }
}

I've been going over this code and cannot find anything that would .setVisible(false) and I've tried doing .setVisible(true) on everything but nothing seems to work.

È stato utile?

Soluzione

Nothing shows up because you do not specify a CENTER component for BorderLayout in your main frame. BorderLayout lays the side components relative to the center one, and if you don't specify the center component you won't see anything.

Having said that, many things of what you are doing don't look good (both visually and code-wise). Start by making a sketch (even in Paint) of what you want to get with sections clearly divided and labeled with the components they need to contain. Then work by that and bit by bit we can make it work better.

As for the mountain of fields you're creating: only declare a field if you are going to need to keep the reference for it for later uses. Most of your labels, for example, probably do not need to be fields, just local variables.

Also, as mentioned in the comments, you don't need so many frames, but before I understand what you're even trying to achieve I can't say what you need to change.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top