我第一次试用Applet运行这个面板得到的是一个灰色的面板``没有实现表格的显示``
我已经放在同一个目录下,也用过appletviewer Student.html运行```都是一样的结果
希望大家能给我点意见。
Student.html文件
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width=600
height=420
align=center>
<param name="code" value="StudentSystem.class">
</OBJECT>
StudentSystem.java文件
/**
* 菜单栏框架
*/
import javax.swing.*;
import java.util.Vector;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentSystem extends JApplet
{
public static void main(String[] args)
{
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//加载数据表格
Object col[] = {"ID", "姓名", "学号", "性别", "出生年月"};
new DataTable(frame, null, col);
frame.pack(); //调整此窗口的大小,以适合其子组件的首选大小和布局。
frame.setVisible(true);
}
}
class MenuFrame extends JFrame
{
private static final int WIDTH = 600;
private static final int HEIGHT = 500;
private JMenuItem studentdataInit;
private JMenuItem teacherdataInit;
private JMenuItem stuData;
private JMenuItem stuAdd;
private JMenuItem stuDel;
private JMenuItem stuEdt;
private JMenuItem stuMsg;
private JMenuItem teachData;
private JMenuItem teachAdd;
private JMenuItem teachDel;
private JMenuItem teachEdt;
private JMenuItem teachMsg;
public MenuFrame()
{
Container con = getContentPane(); //得到内容窗格
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
int x = (width - WIDTH)/2;
int y = (height - HEIGHT)/2;
setBounds(x, y, WIDTH, HEIGHT);
Image image = kit.getImage("stu.gif");
setIconImage(image);
setResizable(false);
setTitle("师生档案管理系统");
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//菜单
JMenu enterMenu = new JMenu("登入设置");
JMenu studentMenu = new JMenu("学生管理");
JMenu teacherMenu = new JMenu("教师管理");
menuBar.add(enterMenu);
menuBar.add(studentMenu);
menuBar.add(teacherMenu);
//菜单项
JMenuItem enterWin = new JMenuItem("登入窗口");
JMenuItem enterFix = new JMenuItem("登入修改");
studentdataInit = new JMenuItem("学生库初始化");
teacherdataInit = new JMenuItem("教师库初始化");
JMenuItem exitStu = new JMenuItem("退出");
stuData = new JMenuItem("学生数据");
stuAdd = new JMenuItem("插入数据");
stuDel = new JMenuItem("删除数据");
stuEdt = new JMenuItem("修改数据");
stuMsg = new JMenuItem("详细资料");
teachData = new JMenuItem("教师数据");
teachAdd = new JMenuItem("插入数据");
teachDel = new JMenuItem("删除数据");
teachEdt = new JMenuItem("修改数据");
teachMsg = new JMenuItem("详细资料");
//登入设置菜单项
enterMenu.add(enterWin);
enterMenu.add(enterFix);
enterMenu.addSeparator();
enterMenu.add(studentdataInit);
enterMenu.add(teacherdataInit);
enterMenu.addSeparator();
enterMenu.add(exitStu);
//学生管理菜单项
studentMenu.add(stuData);
studentMenu.add(stuAdd);
studentMenu.add(stuDel);
studentMenu.add(stuEdt);
studentMenu.add(stuMsg);
//教师管理菜单项
teacherMenu.add(teachData);
teacherMenu.add(teachAdd);
teacherMenu.add(teachDel);
teacherMenu.add(teachEdt);
teacherMenu.add(teachMsg);
exitStu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
teachData.addActionListener(new TeachDataAction(this));
stuData.addActionListener(new StuDataAction(this));
}
//触发事件-----------------------------------------------------------
//弹出教师数据在表格上
private class TeachDataAction implements ActionListener
{
private JFrame parFrame;
public TeachDataAction(JFrame frame)
{
parFrame = frame;
}
public void actionPerformed(ActionEvent event)
{
String str = "Teacher";
Object col[] = {"ID", "教师姓名", "职工号", "性别", "出生年月"};
new DataTable(parFrame, str, col);
parFrame.setVisible(true);
stuAdd.setEnabled(false);
stuDel.setEnabled(false);
stuEdt.setEnabled(false);
stuMsg.setEnabled(false);
teachAdd.setEnabled(true);
teachDel.setEnabled(true);
teachEdt.setEnabled(true);
teachMsg.setEnabled(true);
}
}
//弹出学生数据在表格上
private class StuDataAction implements ActionListener
{
private JFrame parFrame;
public StuDataAction(JFrame frame)
{
parFrame = frame;
}
public void actionPerformed(ActionEvent event)
{
String str = "Student";
Object col[] = {"ID", "学生姓名", "学号", "性别", "出生年月"};
new DataTable(parFrame, str, col);
parFrame.setVisible(true);
stuAdd.setEnabled(true);
stuDel.setEnabled(true);
stuEdt.setEnabled(true);
stuMsg.setEnabled(true);
teachAdd.setEnabled(false);
teachDel.setEnabled(false);
teachEdt.setEnabled(false);
teachMsg.setEnabled(false);
}
}
}
////////////////DataTable.java文件
/**
* 显示数据的表格
*/
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableColumn;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.table.AbstractTableModel;
public class DataTable extends JPanel
{
private JFrame parFrame;
private static Object col[];
private static Vector row;
private JTable table;
private MyTableModel tableModel;
private JScrollPane scrollPane;
private static int count = 0;
private static int sel = 0;
public DataTable(){}
public DataTable(JFrame frame, String str, Object column[])
{
super(new GridLayout(1,0)); //缩小放大时重装大小,必须放在语句第一行
col = column;
parFrame = frame;
tableModel = new MyTableModel();
table = new JTable(tableModel);
tableModel.setColumnIdentifiers(col);
if(str == null)
{
tableModel.setRowCount(30);
}
//设置第一列宽度
TableColumnModel columnModel = table.getColumnModel();
TableColumn tableColumn = columnModel.getColumn(0);
tableColumn.setMinWidth(5);
int min = tableColumn.getMinWidth();
tableColumn.setPreferredWidth(min);
//设置此表视口的首选大小
table.setPreferredScrollableViewportSize(new Dimension(600, 500));
scrollPane = new JScrollPane(table);
add(scrollPane);
frame.setContentPane(this); //替换内容窗格
}
//所有行都不能编辑(重写DefaultTableModel中的isCellEditable)
static class MyTableModel extends DefaultTableModel
{
public boolean isCellEditable(int row, int col)
{
return false;
}
}
}