GUI 显示为空



我正在制作一个包含媒体项目的库列表。我想开始测试按钮,但是当我加载程序时,什么都没有出现。翻看我找不到任何问题,但必须有一个。

这是我的JFrame类:

public class LibraryGUI extends JFrame{
    private static final long serialVersionUID = 1L;
    @SuppressWarnings("unchecked")
    public LibraryGUI() {
        Library lib = new Library();
        setTitle("Library");
        setSize(500, 750);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font f = new Font("Monospaced", Font.BOLD, 20);
        JLabel listLab = new JLabel("Library List");
        listLab.setFont(f);
        @SuppressWarnings("rawtypes")
        JList list = new JList();
        list.setFont(f);
        list.setListData(lib.listAllItems());
        JPanel buttons = new JPanel();
        buttons.setFont(f);
        JButton plus = new JButton("Add Item");
        buttons.add(plus);
        JButton loan = new JButton("Loan Item");
        buttons.add(loan);
        JButton retur = new JButton("Return Item");
        buttons.add(retur);
        JButton delete = new JButton("Delete Item");
        buttons.add(delete);
        @SuppressWarnings("resource")
        Scanner dog = new Scanner(System.in);
        JPanel panel = new JPanel();
        panel.add(listLab, BorderLayout.NORTH);
        panel.add(list, BorderLayout.CENTER);
        panel.add(buttons, BorderLayout.SOUTH);
        plus.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showInputDialog("What is the title of the item?");
                String title = dog.nextLine();
                JOptionPane.showInputDialog("What is the format of the item?");
                String format = dog.nextLine();
                addNewItem(title, format);
            }
        });
        retur.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JOptionPane.showInputDialog("What is the title you are returning?");
                String title2 = dog.nextLine();
                markItemReturned(title2);
            }
        });
        loan.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                @SuppressWarnings("resource")
                Scanner bob = new Scanner(System.in);
                JOptionPane.showInputDialog("What is the title?");
                String title1 = bob.nextLine();
                JOptionPane.showInputDialog("What is your name?");
                String name = bob.nextLine();
                JOptionPane.showInputDialog("What is the date?");
                String date = bob.nextLine();
                markItemOnLoan(title1, name, date);
            }
        });
        delete.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JOptionPane.showInputDialog("What is the title?");
                String title = dog.nextLine();
                delete(title);
            }
        }); 
        setVisible(true);
    }

只需在代码末尾编写add(panel);,因为您希望JPanel显示在JFrame中。

您永远不会将JPanel "panel"添加到框架中。这样做:

add(panel);

在呼叫setVisible(true);之前添加它

最新更新