在學習JTable時發現我的表格會隨著JAVA窗口的變化而變化,想知道如何不讓它
變化,也就是說如何讓它固定化?
代碼如下:
package com.view;
import java.awt.Color;
import javax.swing.*;
public class NewFrame extends JFrame
{
private String[] columnNames=
{
"Country","Capital","Population in Millions","Democracy"
};
private Object[][] data=
{
{"USA","Washington DC",280,true},
{"Canada","Ottawa",32,true},
{"China","Beijing",1000,true},
{"Japan","Tokyo",50,true},
};
private JTable jTable1=new JTable(data,columnNames);
public NewFrame()
{
jTable1.setBackground(Color.RED);
add(new JScrollPane(jTable1));
}
public static void main(String[]args)
{
NewFrame frame=new NewFrame();
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
frame.setLayout(null);//frame不使用布局管理器
jTable1.setPreferredSize(new Dimension(400, 400));//設置table的固定大小