?? tablegrid.java
字號:
package TableGrid;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import java.awt.*;
import java.awt.event.*;
public class TableGrid extends JApplet
{
private boolean DEBUG = true;
public void init()
{
MyTableModel myModel=new MyTableModel();
JTable table = new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(500,700));
JScrollPane scrollPane=new JScrollPane(table);
setContentPane(scrollPane);
}
class MyTableModel extends AbstractTableModel
{
final String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
final Object[][] data={
{"Mary","Campione","Snowboarding",new Integer(5),new Boolean(false)},
{"Alison","Huml","Rowing",new Integer(3),new Boolean(true)},
{"Kathy","Walrath","Chasing toddlers",new Integer(5),new Boolean(false)}
};
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row,int col)
{
return data[row][col];
}
public Class getColumnClass(int c)
{
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row,int col)
{
/* if (col<2)
{
return false;
}else
{
return true;
}*/
return false;
}
public void setValueAt(Object value,int row,int col)
{
if (DEBUG)
{
System.out.println("Setting Value at "+row+","+col+"to"+value+"(an instance of"+value.getClass()+")");
}
data[row][col]=value;
fireTableCellUpdated(row,col);
if (DEBUG)
{
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData()
{
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0;i<numRows ;i++ )
{
System.out.print(" Row"+i+":");
for (int j=0;j<numCols ;j++ )
{
System.out.print(" "+data[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
}
}
public static void main(String[] args)
{
TableGrid applet = new TableGrid();
JFrame aFrame = new JFrame("TableGrid");
aFrame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
aFrame.getContentPane().add(applet,BorderLayout.CENTER);
aFrame.setSize(500,150);
applet.init();
applet.start();
aFrame.setVisible(true);
// System.out.println("Hello World!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -