表头没有显示在Java Swing上

时间:2014-08-22 05:59:51

标签: java swing jtable jtableheader

我在这里添加完整代码。检查以下用于创建表的代码。 执行程序时,值显示清晰,但标题未显示。

public class Table extends JFrame implements ActionListener {

    JFrame f;
    JPanel p1;
    JPanel jPanel1;
    JTable jTable1;

    Table() {

        f=new JFrame("Home123");
        f.setSize(getMaximumSize());
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);

        p1=new JPanel();
        p1.setBackground(Color.red);
        p1.setVisible(true);
        p1.setLayout(null);
        f.add(p1);

        String[] headers = {"First Name","Last Name","Age"};
        Object[][] data = {
        {"Kathy", "Smith",new Integer(25)},
        };  

         jTable1 =new JTable(data, headers);
         JScrollPane scrollPane = new JScrollPane(jTable1);

         p1.add(jTable1);

         p1.setBounds(200, 100, 500, 500);
         jTable1.setBounds(70,250, 375,80);
    }

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

4 个答案:

答案 0 :(得分:4)

public class TableExample extends JFrame implements ActionListener {

        JFrame f;
        JPanel p1;
        JPanel jPanel1;
        JTable jTable1;

        TableExample() {

            JFrame f;
        JPanel p1;
        JPanel jPanel1;
        JTable jTable1;

        f = new JFrame("Home123");
        f.setSize(getMaximumSize());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p1 = new JPanel();
        p1.setBackground(Color.red);
        p1.setLayout(new BorderLayout());
        f.add(p1);

        String[] headers = {"First Name", "Last Name", "Age"};
        Object[][] data = {
            {"Kathy", "Smith", new Integer(25)},};

        jTable1 = new JTable(data, headers);
        JScrollPane scrollPane = new JScrollPane(jTable1);
        scrollPane.setSize(500,500);
        jTable1.setSize(500,500);
        p1.add(scrollPane);
        f.pack();
        f.setVisible(true);
        }

    public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableExample();
            }
        });
        }

        @Override
            public void actionPerformed(ActionEvent e) {
            try {
                System.out.println("");

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

尝试将其工作并显示您需要的所有内容。enter image description here

答案 1 :(得分:4)

至@Sergiy Medvynskyy和@Krishna约1. f.setVisible(true); 2. Initial Thread 3. p1.setVisible(true);

e.g。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Table /*extends JFrame implements ActionListener*/ {

    private JFrame f;
    private JPanel p1;
    private JPanel jPanel1;
    private JTable jTable1;

    public Table() {
        p1 = new JPanel();
        p1.setBackground(Color.red);
        p1.setLayout(new BorderLayout(5, 5));        
        String[] headers = {"First Name", "Last Name", "Age"};
        Object[][] data = {
            {"Kathy", "Smith", new Integer(25)},};        
        jTable1 = new JTable(data, headers);
        JScrollPane scrollPane = new JScrollPane(jTable1);
        jTable1.setPreferredScrollableViewportSize(jTable1.getPreferredSize());
        p1.add(scrollPane);        
        f = new JFrame("Home123");
        //f.setSize(getMaximumSize());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p1);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Table();
            }
        });
    }
}

答案 2 :(得分:1)

您将表格直接添加到框架的面板,而不是添加包裹表格的滚动窗格,并显示标题。

更改

p1.add(jTable1);

p1.add(scrollPane);

绝对停止使用null布局和设置边界。学习使用布局管理器。

答案 3 :(得分:1)

您应该将滚动窗格添加到面板

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Table extends JFrame {
    JFrame f;
    JPanel p1;
    JPanel jPanel1;
    JTable jTable1;

    Table() {

        p1=new JPanel();
        p1.setBackground(Color.red);
        p1.setVisible(true);
        p1.setLayout(new BorderLayout());

        final String[] headers = {"First Name","Last Name","Age"};
        final Object[][] data = {
                {"Kathy", "Smith",new Integer(25)},
        };

        jTable1 =new JTable(data, headers);
        final JScrollPane scrollPane = new JScrollPane(jTable1);

        p1.add(scrollPane);

        f=new JFrame("Home123");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p1);
        f.pack();
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new Table();
    }
}

P.S。永远不要使用null-layout。