【问题标题】:Why the JTable is not showing?为什么 JTable 没有显示?
【发布时间】:2014-11-30 16:36:46
【问题描述】:

我有这段代码(是的,我是新手),但 JTable 没有显示。 在创建面板之前,我可以看到它(一个洋红色框,因为我还没有制作 DataModel)。

但添加面板后,它消失了。谁能告诉我为什么?提前致谢。

public Ost() {
    super();
    JTable table;
    OstBridge bridge;
    JButton btn;
    JPanel p1=new JPanel(new FlowLayout());
    JPanel p2=new JPanel(new FlowLayout());
    Container cp=getContentPane();
    cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));




    p1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
    p2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setSize(320, 240);
    this.setTitle("OST - Downloader");
    //this.setLayout(new FlowLayout());

    cp.add(p1);
    cp.add(p2);

    table=new JTable();
    table.setBackground(new Color(0xFF00FF));
    table.setSize(100, 100);
    table.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    p2.add(table);

    bridge=new OstBridge();

    btn=new JButton("Prueba");
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("Action event");
            System.out.println(e);
            System.out.println(bridge);
            new Ost();

        }
    });

    p1.add(btn);
    p1.setVisible(true);
    this.pack();

    this.setVisible(true);
}

【问题讨论】:

    标签: java swing layout jtable jpanel


    【解决方案1】:

    它需要数据和/或数据模型才能让您看到有趣的东西。

    如果您像这样在 JScrollPane 中添加表格:

    p2.add(new JScrollPane(table));
    

    你至少可以看到组件的轮廓。

    【讨论】:

      猜你喜欢
      • 2015-07-03
      • 2012-10-28
      • 2014-02-01
      • 2020-06-13
      • 1970-01-01
      • 2012-05-13
      • 2023-03-07
      • 2017-06-05
      • 1970-01-01
      相关资源
      最近更新 更多