没有在JFrames中显示的组件



每当我运行这段代码时,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)上的一切,但似乎没有工作。

没有显示任何内容,因为您没有在主框架中为BorderLayout指定CENTER组件。BorderLayout将侧边组件相对于中心组件放置,如果你不指定中心组件,你将看不到任何东西。

话虽如此,你所做的很多事情看起来都不太好(无论是视觉上还是代码方面)。首先绘制一个草图(即使是在Paint中),明确划分各个部分,并标记它们需要包含的组件。那么就这样一点一点地努力,我们可以使它更好地工作。

对于您正在创建的大量字段:只有在您需要为其保留引用以供以后使用时才声明字段。例如,您的大多数标签可能不需要是字段,只需要是局部变量。

同样,正如评论中提到的,你不需要这么多帧,但在我理解你想要达到的目的之前,我不能说你需要改变什么。

相关内容

  • 没有找到相关文章

最新更新