0

我正在尝试打开一个窗口并让它显示文件中的文本。但是,当我运行它时,我只是得到一个空白窗口。这是窗口的代码 - 我完全没有从 eclipse 中得到任何错误。

package presentation;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import domain.Items;
import services.exceptions.ItemNotFoundException;
import services.itemservice.*;

public class ShowAllInventory extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -4498395613773129897L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ShowAllInventory frame = new ShowAllInventory();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the window frame.
     */
    public ShowAllInventory() throws ItemNotFoundException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        /**
         * Load the inventory
         */

        IItemsService service = new ItemsServiceImpl();
        try {
            Items items = service.getItems();
        } catch (ItemNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println ("Items Not Found");
        }

    }

}

这是代码正在调用的接口的代码 -

    public Items getItems () throws ItemNotFoundException;

}

这是实现...

/**
 * Getting Items from the database
 * @return 
 * @throws ItemNotFoundException
 */
@Override
public Items getItems() throws ItemNotFoundException {

    Items items = (Items) null;
    try {
        ObjectInputStream input = new
                ObjectInputStream (new FileInputStream("itemdatabase"));
        items = (Items)input.readObject();
        input.close();
    } catch (IOException ioe) {
        System.out.println ("IOException");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }       
    return items;       
}
4

1 回答 1

3

我不知道您是否IItemsService可以正确加载文件。但很可能您会得到一个空白窗口,因为在您的代码中,我认为您忘记添加一个组件来显示项目。如果您的Items作品是List. 您可以使用 aJList来显示它们。

Item[] items = ...// use your Items to populate Item to an array
JList itemList = new JList(items);

JScrollPane scrollPane = new JScrollPane(itemList);
scrollPane.setPreferredSize(new Dimension(300, 100)); //put your preferred size here

contentPane.add(scrollPane);

恕我直言,我认为您不应该扔ItemNotFoundException,因为找不到任何物品应该是正常情况。FileNotFoundException应该是被扔到这里的那个。


更新 如果你想使用JTextField或者JTextArea你必须这样:

...
Items items = service.getItems();
...

String itemContent = items.toString(); // you have to put meaningful information from class Items to this String
JTextArea itemDisplay = new JTextArea(itemContent);
// JTextField itemDisplay = new JTextField(itemContent);

JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300, 100)); //put your preferred size here

contentPane.add(scrollPane);
于 2012-06-23T19:40:52.240 回答