0

ウィンドウを開いて、ファイルのテキストを表示させようとしています。ただし、実行すると、空白のウィンドウが表示されます。これがウィンドウのコードです-日食からのエラーはまったくありません。

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。を使用しJListてそれらを表示できます。

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 に答える