GUI显示为空?

时间:2014-12-10 21:56:24

标签: java user-interface jframe jpanel visible

我正在制作一个包含媒体项目的图书馆列表。我想开始测试按钮但是当我加载程序时没有出现任何内容。透过我无法找到任何问题,但必须有一个。

这是我的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);
    }

2 个答案:

答案 0 :(得分:0)

只需在代码末尾写下add(panel);,就像您希望JPanel中显示JFrame一样。

答案 1 :(得分:0)

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

add(panel);

在致电setVisible(true);

之前添加