3

GUI を使用するのはこれが初めてなので、何が問題を引き起こしているのか正確にはわかりません。大学からの課題があります。このプロジェクトは、さまざまな目的のために一種の「製品管理」プログラムを作成することです。GUI 以外はすべて完了していますが、ここでJTable列のタイトルが表示されない理由がわかりません。コードは次のとおりです(ところで、MIGLayoutを使用)

package sepm.s2012.e0727422.gui;

import sepm.s2012.e0727422.service.*;

import java.awt.*;
import javax.swing.*;

import net.miginfocom.swing.MigLayout;


public class MainFrame {

    /** Start all services **/
//  IProductService ps = new ProductService();
//  IInvoiceService is = new InvoiceService();
//  IOrderService os = new OrderService();

    /** Frame **/
    private JFrame frame;
    private JTabbedPane tab;

    public static void main(String[] args) {
        new MainFrame();
    }


    public MainFrame() {

        frame = new JFrame("Product control and management system");
        frame.setVisible(true);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /** TABBED PANE options and parameters **/
        tab = new JTabbedPane();
        ImageIcon icon = null; // TODO
        tab.addTab("Products", icon, tabProducts(), "Add/Update/Delete products");
        tab.addTab("Invoices", icon, tabInvoices(), "Overview of invoices");
        tab.addTab("Cart", icon, tabCart(), "Order new products");
        tab.setSelectedIndex(0);

        frame.add(tab, BorderLayout.CENTER);

    }       
    /**
     * Products Panel tab
     * @return panel
     */
    public JPanel tabProducts() {
        JPanel panel = new JPanel(new MigLayout("","20 [] 20", "10 [] 10 [] 10 [] 10"));

        JLabel label = new JLabel("List of all available products");
        JButton add = new JButton("Add product");
        JButton update = new JButton("Update product");
        // Below is a test table, will be replace by products in DB
        String[] tableTitle = new String[] {"ID", "Name", "Type", "Price", "In stock"};
        String[][] tableData = new String[][] {{"1", "Item 1", "Type 1", "0.00", "0"}, {"2", "Item 2", "Type 2", "0.00", "0"},
                                                {"3", "Item 3", "Type 3", "0.00", "0"}, {"4", "Item 4", "Type 4", "0.00", "0"}};
        JTable table = new JTable(tableData, tableTitle);

        panel.add(label, "wrap, span");
        panel.add(table, "wrap, span");
        panel.add(add);
        panel.add(update);

        return panel;
    }

    public JPanel tabInvoices() {
        JPanel panel = new JPanel(new MigLayout());
        return panel;
    }

    public JPanel tabCart() {
        JPanel panel = new JPanel(new MigLayout());
        return panel;
    }
}
4

3 に答える 3

8

テーブルをJScrollPane

状態のドキュメントJTable:

.. をJTableスタンドアロン ビュー( の外部JScrollPane) で使用し、ヘッダーを表示したい場合は、を使用して取得しgetTableHeader()、個別に表示できます。

于 2012-03-19T11:37:41.507 に答える
4

まず、テーブルを JScrollPane に追加します。

JScrollPane scrollpane = new JScrollPane(table);

次に、スクロールペインをレイアウトに追加します。

panel.add(scrollpane, "wrap, span");

次に、すべてのコンポーネントをフレームに追加し、メソッドの最後に表示できるようにします。

//...
frame.add(tab, BorderLayout.CENTER);

frame.validate();
frame.setVisible(true);
于 2012-03-19T11:45:18.300 に答える