Java的问题,表格无法显示
小弟是新手,请各位大神看看,哪里出了问题
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.lang.Object.*;
import java.util.*;
import java.sql.*;
public class ZhuJieMian extends JFrame{
JButton b1=new JButton("商品管理");
JButton b2=new JButton("采购管理");
JButton b3=new JButton("销售管理");
JButton b4=new JButton("用户管理");
JButton b5=new JButton("权限管理");
JLabel l1=new JLabel("商品编号");
JLabel l2=new JLabel("商品名称");
JLabel l3=new JLabel("商品价格");
JLabel l4=new JLabel("生产日期");
JLabel l5=new JLabel("生产地");
JLabel l6=new JLabel("库存量");
public int screanWidth2,screanHeight2;
AbstractTableModel tm;
Vector vector;
String title[]={"商品编号","商品名称","商品价格","生产日期","生产地","库存量"};
public static void main(String[] args){
new ZhuJieMian();
}
public ZhuJieMian()
{
Toolkit tool=getToolkit();
Dimension dim=tool.getScreenSize();
screanWidth2=dim.width;
screanHeight2=dim.height;
createTable();
setTitle("商品管理");
setLayout(null);
Component add = add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
b1.setBounds(15, 535, 100, 20);
b2.setBounds(130, 535, 100, 20);
b3.setBounds(245, 535, 100, 20);
b4.setBounds(360, 535, 100, 20);
b5.setBounds(475, 535, 100, 20);
setBounds(360,20,600,600);
setVisible(true);
}
void createTable()
{
JTable table;
JScrollPane scroll;
vector=new Vector();
tm=new AbstractTableModel()
{
public int getColumnCount()
{
return title.length;
}
public int getRowCount()
{
return vector.size();
}
public Object getValueAt(int row,int column)
{
if(!vector.isEmpty())
return ((Vector)vector.elementAt(row)).elementAt(column);
else
return null;
}
public void setValueAt(Object value,int row,int column)
{
}
public String getcoulumnName(int column)
{
return title[column];
}
public Class getColumnClass(int c)
{
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row,int column)
{
return false;
}
};
table=new JTable(tm);
table.setToolTipText("Display Query Result");
table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
table.setCellSelectionEnabled(false);
table.setShowVerticalLines(true);
scroll=new JScrollPane(table);
scroll.setPreferredSize(new Dimension(400,200));
add(scroll);
}
} java
[解决办法]
好久不见 swing 了。Java UI 编程,先不要嵌套那么多,眼见为实,步步为营吧,这一步成功了再加下一个 feature。
[解决办法]
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
/**
* TableDemo is just like SimpleTableDemo, except that it
* uses a custom TableModel.
*/
public class TableDemo extends JPanel {
private boolean DEBUG = false;
public TableDemo() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), 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];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
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("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
public Test4(){
JFrame f=new JFrame();
DefaultTableModel myTable=new DefaultTableModel();
JTable table=new JTable(myTable);
table.setPreferredScrollableViewportSize(new Dimension(550,30));
myTable.addColumn("姓名");
myTable.addColumn("MM");
myTable.addColumn("NN");
int number=3;
int k=0;
while(k++!=4){
Vector<Object> newRow = new Vector<Object>();
for (int i = 1; i <= number; i++) {
newRow.add(i);
}
myTable.addRow(newRow);
}
table.setPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane scrollPane=new JScrollPane(table);
f.getContentPane().add(scrollPane,BorderLayout.CENTER);
f.setTitle("Simple Table");
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args){
Test4 b=new Test4();
}
}