类文件:
package haha;
import javax.swing.*;
import javax.swing.table.*;
/**该类保存表格的数组,定义员工总分的计算方法与排序方法*/
public class PerformanceCaculation {
//定义表格的行和列的数组
Double zero = new Double(0);
Object[][] data = {
{"李环青","男", zero, zero, zero, zero, zero, zero},
{"张东红","女", zero, zero, zero, zero, zero, zero},
{"吴贞","女", zero, zero, zero, zero, zero, zero},
{"张能求","男", zero, zero, zero, zero, zero, zero},
{"李凡到","男", zero, zero, zero, zero, zero, zero},
{"吴同凡","女", zero, zero, zero, zero, zero, zero},
{"张宽","女", zero, zero, zero, zero, zero, zero},
{"李一","男", zero, zero, zero, zero, zero, zero}
};
//定义表格的标题数组
String[] columnNames = {"姓名","性别","团队合作的分数","客户关系发展的分数",
"应付工作压力的分数","工作效率的分数","知识技能的分数","总分"};
//根据表格的行、列数组和标题数组创建表格
JTable table = new JTable(data, columnNames);
//通过表格table的模式创建表格的模式类,使用该类取得表格的数据
TableModel tableModel = table.getModel();
//定义每个员工总分的数组和分数的权重数组
double[] records = new double[8];
int[] weight = {3, 3, 1, 2, 1};
/**类的构造方法*/
public PerformanceCaculation(){
//定义表格的选择框为一个单元格
table.setColumnSelectionAllowed(true);
//设置表格的行高度
table.setRowHeight(20);
}
/**计算员工总分的方法*/
public void caculateTotal(){
for(int i = 0; i < 8; ++i) {
//清空员工总分的数值
records[i] = 0;
//取得员工的每项分数乘上每项分数的权重,计算总分数
try{
for(int j = 0; j < 5; ++j){
records[i] += Double.parseDouble(String.valueOf(tableModel.getValueAt(i,j+2)))
* weight[j];
//将各项分数放入表格数组
data[i][j+2] = tableModel.getValueAt(i,j+2);
}
//将总分放入表格数组
data[i][7] = new Double(records[i]);
//在第7列填上员工的总分数
tableModel.setValueAt(new Double(records[i]), i, 7);
}catch(Exception e){
//引入异常处理代码,如果在空格输入文字,那么总数的数值是0
records[i] = 0;
}
}
}
/**对绩效进行排序的方法*/
public void sortPerformance() {
quickSort(records, 0, records.length - 1);
//重新显示表格的内容
for(int i = 0; i < 8; ++i){
for(int j = 0; j < 8; ++j){
tableModel.setValueAt(data[i][j], i, j);
}
}
//显示前5名员工的对话框
String fiveGoodEmployee = "前5名的员工名字如下:\n";
for(int i = 7; i > 2 ; --i){
fiveGoodEmployee += data[i][0] + "\n";
}
JOptionPane.showMessageDialog(null,fiveGoodEmployee);
}
/**快速排序方法*/
public void quickSort(double[] a, int lo0, int hi0) {
int lo = lo0;
int hi = hi0;
if (lo >= hi)
return;
//确定指针方向的逻辑变量
boolean transfer=true;
while (lo != hi) {
if (a[lo] > a[hi]) {
//交换数字
double temp = a[lo];
a[lo] = a[hi];
a[hi] = temp;
//交换表格数组的记录
for(int i = 0; i < 8; ++i){
Object tempObj = new Object();
tempObj = data[lo][i];
data[lo][i] =data[hi][i];
data[hi][i] = tempObj;
}
//决定下标移动,还是上标移动
transfer = (transfer == true) ? false : true;
}
//将指针向前或者向后移动
if(transfer)
hi--;
else
lo++;
}
//将数组分开两半,确定每个数字的正确位置
lo--;
hi++;
quickSort(a, lo0, lo);
quickSort(a, hi, hi0);
}
}
-----------------------------------
执行文件:
package yk412;
import javax.swing.UIManager;
import java.awt.*;
import haha.*;
public class AppraisalPro {
private boolean packFrame = false;
/**构造方法*/
public AppraisalPro() {
AppraisalFrame frame = new AppraisalFrame();
//使窗口居中对齐
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
//使窗口可视
frame.setVisible(true);
}
/**主方法*/
public static void main(String[] args) {
new AppraisalPro();
}
}
-------------------
frame文件:
package yk412;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*; //取得表格模式类
import javax.swing.event.*; //取得表格事件接收器
import haha.*;
public class AppraisalFrame extends JFrame {
//创建可视化窗口的各个控件
private JPanel contentPane;
private JPanel btnPanel = new JPanel();
private JButton caculateBtn = new JButton();
private JButton sortBtn = new JButton();
private JScrollPane tableScrollPane = new JScrollPane();
private JButton exitBtn = new JButton();
private JPanel labelPanel = new JPanel();
private JLabel inforLabel = new JLabel();
//创建员工绩效计算类
PerformanceCaculation performanceCaculation = new PerformanceCaculation();
//将员工绩效计算类的table控件赋给窗口的employeeTable控件
private JTable employeeTable =performanceCaculation.table;
//构造方法
public AppraisalFrame() {
try {jbInit();}catch(Exception e) {e.printStackTrace();}
}
/**窗口初始化方法*/
private void jbInit() throws Exception {
//取得窗口的面板,设置窗口的大小与标题
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(645, 300));
this.setTitle("员工绩效评估窗口");
//分别设置按钮的标题和加入动作接收器
caculateBtn.setText("计算");
caculateBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
caculateBtn_actionPerformed(e);
}
});
sortBtn.setText("排序");
sortBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
sortBtn_actionPerformed(e);
}
});
exitBtn.setText("退出");
exitBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
exitBtn_actionPerformed(e);
}
});
//设置分数权重的信息标签
inforLabel.setText("团队合作的权重是3 客户关系发展的权重是3 应付工作压力的权重是1 " +
" 工作效率的权重是2 知识技能的权重是1");
//向标签面板加入标签
labelPanel.add(inforLabel, null);
//向表格面板加入表格
tableScrollPane.getViewport().add(employeeTable, null);
//向按钮面板加入按钮
btnPanel.add(caculateBtn, null);
btnPanel.add(sortBtn, null);
btnPanel.add(exitBtn, null);
//向底层面板加入各个控件
contentPane.add(btnPanel, BorderLayout.SOUTH);
contentPane.add(tableScrollPane, BorderLayout.CENTER);
contentPane.add(labelPanel, BorderLayout.NORTH);
}
/**退出程序时清空内存*/
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
/**退出按钮的处理代码*/
void exitBtn_actionPerformed(ActionEvent e) {
System.exit(0);
}
/**计算按钮的处理代码*/
void caculateBtn_actionPerformed(ActionEvent e) {
performanceCaculation.caculateTotal();
}
/**排序按钮的处理代码*/
void sortBtn_actionPerformed(ActionEvent e) {
//先运行计算方法,再运行排序方法
performanceCaculation.caculateTotal();
performanceCaculation.sortPerformance();
}
}
在处发生 ::table is not public in haha.PerformanceCaculation; cannot be accessed from outside package at line 25 (25:55)
发生这样错误是为什么啊?》??????
求求你们帮帮哈,,,,
[此贴子已经被作者于2006-10-22 22:26:43编辑过]
----------------解决方案--------------------------------------------------------
table is not public in haha.PerformanceCaculation; cannot be accessed from outside package at line 25 (25:55)
table并不是公共的在haha.PerformanceCaculation包里面,所以不能被外面的包所访问
----------------解决方案--------------------------------------------------------