>>
The JTable has a method, setAutoResizeMode(int), that defines the resize policy based on 5 constants, also defined in JTable :AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS,
AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNSAUTO_RESIZE_OFF will disable all resizing and enable the horizontal scroll bar of the parent JScrollPane.
The JTable has a method, setAutoResizeMode(int), that defines the resize policy based on 5 constants, also defined in JTable :AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS,
AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNSAUTO_RESIZE_OFF will disable all resizing and enable the horizontal scroll bar of the parent JScrollPane.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*; public class DataFileTable extends JPanel {
public DataFileTable(String dataFilePath) {
JTable table;
DataFileTableModel model;
Font f; f = new Font("SanSerif",Font.PLAIN,24);
setFont(f);
setLayout(new BorderLayout()); model = new DataFileTableModel(dataFilePath); table = new JTable();
table.setModel(model);
table.createDefaultColumnsFromModel(); JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
} public Dimension getPreferredSize(){
return new Dimension(400, 300);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Data File Table");
DataFileTable panel;
panel = new DataFileTable("customers.dat"); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
} class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}
我try 一下!经常看见skyyoung(路人甲)兄的回复,也代别的兄弟感谢你!!每叠必给分
你可以试一试table类的方法:
table.setShowVerticalLines(true);
table.setShowHorizontalLines(true);
问题已解决,
必须设置:setLayout(new BorderLayout());can not use default Layout FlowLayout !!