组件未显示在JFrame中

时间:2014-05-04 16:28:01

标签: java swing user-interface jframe

每当我运行此代码时,JFrame都会显示,但我的组件都不会显示。我之前做过一些GUI项目,但之前从未遇到过这个问题。

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);
    }
}

我一直在查看这段代码,找不到任何会.setVisible(false)和我尝试.setVisible(true)做任何事情的事情,但似乎没什么用。

1 个答案:

答案 0 :(得分:1)

由于您未在主框架中为CENTER指定BorderLayout组件,因此未显示任何内容。 BorderLayout放置相对于中心组件的侧面组件,如果您没有指定中心组件,则您将看不到任何内容。

话虽如此,许多你正在做的事情并不好看(无论是视觉还是代码方面)。首先制作一个草图(即使在油漆中)你想要得到的部分清楚划分并标记他们需要包含的组件。然后通过它逐步工作,我们可以使它更好地工作。

至于您正在创建的大量山脉:如果您需要保留其中的参考以供以后使用,则仅声明一个字段。例如,大多数标签可能不需要是字段,只需要局部变量。

另外,正如评论中所提到的,你不需要那么多帧,但在我了解你甚至想要实现的目标之前,我无法说出你需要改变什么。< / p>