为什么我的JTable标题不显示

时间:2016-01-26 11:39:17

标签: java swing

我的JTable似乎有困难,因为它没有显示标题; ""姓名"," ID","国籍","语言","条件",& #34; Ward"," Food Preference"," Blood Type"," Allergy"。

这是我的代码:

package searchengine;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JList;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class SearchResults extends JFrame {

private JPanel contentPane;
private JTable table;
private JTable SearchTable;
private Container frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SearchResults frame = new SearchResults();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public SearchResults() {
    setTitle("Search Results");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 750, 830);
    contentPane = new JPanel();
    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    SearchTable = new JTable();
    SearchTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
    SearchTable.setBackground(Color.WHITE);
    SearchTable.setModel(new DefaultTableModel(
        new Object[][] {
            {"Mary Johnson", "31195585", "USA", "English/Spanish", "Tubercolosis", "12A", "N.A.", "A", "Peanuts"},
            {"Peng Ding", "E67112009", "China", "Mandarin", "Pneumonia", "55E", "N.A.", "B", "Dairy"},
            {"J\u00E9an-Claude", "01DA67411", "France", "French", "Stroke", "14R", "Vegeterian", "B", "Seafood"},
            {"James Raj", "F6700241", "India", "Tamil", "3rd Degree Burns", "66F", "Vegeterian", "O+", "Eggs"},
            {"Harry Kretschmann", "H784510073", "Germany", "German", "3rd Degree Burns", "4B", "Vegeterian", "O+", "N.A."},
            {"John Lee", "E6784511G", "Singapore", "English/Mandarin", "Pneumonia", "22C", "N.A.", "O-", "N.A."},
            {"Benny Ho", "A56773455", "Malaysia", "Mandarin", "Cancer", "6D", "Meaterian", "AB", "Seafood"},
            {"Peter Abbot", "N6793561", "Austrailia", "English", "Low Blood Pressure", "8G", "Meaterian", "B", "Wheat"},
        },
        new String[] {
            "Name", "ID", "Nationality", "Language", "Condition", "Ward", "Food Preference", "Blood Type", "Allergy"
        }
    ) {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        Class[] columnTypes = new Class[] {
            String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class
        };
        public Class getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
        boolean[] columnEditables = new boolean[] {
            false, false, false, false, false, false, false, false, false
        };
        public boolean isCellEditable(int row, int column) {
            return columnEditables[column];
        }
    });
    SearchTable.setCellSelectionEnabled(true);
    SearchTable.setColumnSelectionAllowed(true);
    SearchTable.setBounds(0, 194, 728, 128);
    contentPane.add(SearchTable);

    final JButton btnBack = new JButton("Back to Search");
    btnBack.setBounds(225, 418, 267, 112);
    contentPane.add(btnBack);
    btnBack.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) {
            this.setVisible(false);
            new Search().setVisible(true); // Main Form to show after the Login Form..
        }

        private void setVisible(boolean b) {
            // TODO Auto-generated method stub

        }
    });


}
}

1 个答案:

答案 0 :(得分:0)

JTable需要正确显示“JScrollPane”。

只需更换此

即可修复
SearchTable.setBounds(0, 194, 728, 128);
contentPane.add(SearchTable);

使用

JScrollPane sp = new JScrollPane(SearchTable);
sp.setSize(new Dimension(728, 128));
sp.setLocation(0, 194);
contentPane.add(sp);