加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JTable不显示列标题

发布时间:2020-12-15 04:57:10 所属栏目:Java 来源:网络整理
导读:这是我第一次使用GUI,所以我不确定导致问题的原因.我的Uni任务.该项目旨在为各种目的制定一种“产品管理”计划.除了GUI之外,整个过程都已完成,这就是我不知道为什么这个JTable不会显示列标题的原因.这是代码(顺便说一句,使用MIGLayout) package sepm.s2012.e
这是我第一次使用GUI,所以我不确定导致问题的原因.我的Uni任务.该项目旨在为各种目的制定一种“产品管理”计划.除了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",tabInvoices(),"Overview of invoices");
        tab.addTab("Cart",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",{"3","Item 3","Type 3",{"4","Item 4","Type 4","0"}};
        JTable table = new JTable(tableData,tableTitle);

        panel.add(label,"wrap,span");
        panel.add(table,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;
    }
}

解决方法

首先将表添加到JScrollPane:

JScrollPane scrollpane = new JScrollPane(table);

然后将滚动窗格添加到您的布局:

panel.add(scrollpane,span");

其次,将所有组件添加到框架中,并在方法结束时将其显示为:

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

frame.validate();
frame.setVisible(true);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读