

我给默认设置了false,运行之后确实不能编辑,可我添加了一个验证,输入密码正确就可以编辑,但是正确了还不能编辑,应该是对话框关闭之后,对话框到大面板之间交互的问题,这怎么解决~
部分代码:
public ScoreSort3() {
JDialog pswDlg = new JDialog();// 创建一个输入密码对话框
Container pswCon = pswDlg.getContentPane();// 给密码对话框创建容器
pswCon.setLayout(new GridLayout(4, 1));// 设置布局
pswDlg.setVisible(true);
pswDlg.setTitle("输入密码");
pswDlg.setBounds(200, 200, 300, 200);
JLabel pswLable = new JLabel();// 创建一个标签来提示"输入密码"
pswLable.setText("请输入管理员密码");
JPasswordField pswFd = new JPasswordField();// 新建密码文本框
JButton ok = new JButton("确定");
JLabel pswRightLb = new JLabel();
pswCon.add(pswLable);
pswCon.add(pswFd);
pswCon.add(ok);
pswCon.add(pswRightLb);
pswDlg.toFront();
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
char[] ch = pswFd.getPassword(); // getPassword()获得密码文本框的密码,返回值是char[]
String psw = new String(ch); // 将char[]字符数组转化为String字符串
if (psw.equals("123456")) { // 判断密码是否正确
pswRightLb.setText("登陆成功,关闭此对话框进行编辑");// 密码输入成功之后提示
// // 将学生信息恢复可编辑状态
JTable infoTable = new JTable(tableModel) {
public boolean isCellEditable(int row, int clm) {
return true;
}
};
}else{
pswRightLb.setText("密码错误,很抱歉,您没有权限对信息进行编辑");
}
}
});
setTitle("学生成绩管理信息系统"); // 给窗体命名
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
setBounds(100, 100, 913, 513);// 移动组件并调整其大小 (x,y,width,height) ==> x ,
// y指离左上角的x,y坐标
infoPanel = new JPanel(new BorderLayout());
btnPanel = new JPanel(new GridLayout(6, 2));
Container bigCon = this.getContentPane();
bigCon.setLayout(new BorderLayout());
// panel.add(infoPanel);
psdLabel=new JLabel("请输入管理员密码");
btnPanel.add(psdLabel);
psdField=new JPasswordField();
btnPanel.add(psdField);
psdBtn=new JButton("确定");
btnPanel.add(psdBtn);
JLabel label = new JLabel();
btnPanel.add(label); // 无实际功效 只是用来占位 占GridLayout位置
operateLabel = new JLabel("请选择您要进行的操作");
operateLabel.setBounds(43, 251, 138, 15);
btnPanel.add(operateLabel);
JLabel label2 = new JLabel();
btnPanel.add(label2); // 无实际功效 只是用来占位 占GridLayout左下角位置
bigCon.add(infoPanel, BorderLayout.WEST);
JPanel sumAvgPanel=new JPanel();
JButton sum=new JButton("计算总分");
JButton avg=new JButton("计算平均分");
sumAvgPanel.add(sum);
sumAvgPanel.add(avg);
infoPanel.add(sumAvgPanel,BorderLayout.SOUTH);
insertBtn = new JRadioButton("插入");
insertBtn.setBounds(43, 276, 54, 23);
btnPanel.add(insertBtn);
deleteBtn = new JRadioButton("删除");
deleteBtn.setBounds(140, 276, 54, 23);
btnPanel.add(deleteBtn);
updateBtn = new JRadioButton("修改");
updateBtn.setBounds(237, 276, 54, 23);
btnPanel.add(updateBtn);
//
sortBtnGroup = new ButtonGroup(); // 创建一个单选组来选择要按照哪个方式排序,实现一组btn只能选择一个按钮
sortBtnGroup.add(totalBtn); // 将总分排序按钮添加到组
sortBtnGroup.add(snoBtn); // 将学号排序按钮添加到组
sortBtnGroup.add(avgBtn); // 将平均分排序按钮添加到组
searchBtn = new JRadioButton("查询");
searchBtn.setBounds(334, 276, 54, 23);
btnPanel.add(searchBtn);
opBtnGroup = new ButtonGroup();// 创建一个单选按钮组,将增删改查添加到组,实现单选
opBtnGroup.add(insertBtn);// 将插入按钮添加到组
opBtnGroup.add(deleteBtn);// 将删除按钮添加到组
opBtnGroup.add(updateBtn);// 将修改按钮添加到组
opBtnGroup.add(searchBtn);// 将查询按钮添加到组
String[] columnNames = { "姓名", "学号", "班级", "数学", "英语", "语文", "总分", "平均分(近似值)" };// 8列
// 列名
Object[][] data = { { "Tom", "0001", "软件1714", "77", "70", "95", "", "" },
{ "Jenny", "0012", "软件1714", "78", "60", "92", "", "" },
{ "Mary", "0009", "软件1714", "88", "70", "92", "", "" },
{ "Lily", "0004", "软件1714", "70", "40", "60", "", "" },
{ "May", "0002", "软件1713", "88", "80", "58", "", "" },
{ "Cindy", "0003", "软件1715", "69", "70", "88", "", "" },
{ "Haha", "0005", "软件1715", "91", "55", "54", "", "" },
{ "Peter", "0008", "软件1715", "69", "55", "48", "", "" },
{ "Jack", "0006", "软件1714", "78", "60", "80", "", "" },
{ "Alice", "0007", "软件1715", "97", "87", "90", "", "" },
{ "David", "0010", "软件1715", "78", "59", "77", "", "" },
{ "Paul", "0011", "软件1713", "78", "80", "74", "", "" } };// 12行 每行数据 先不填总分和平均分数据,通过按钮触发事件自动写入数据
sum.addActionListener(new ActionListener() { //调用总分按钮 计算总分
@Override
public void actionPerformed(ActionEvent arg0) {
for(int rowIndex=0;rowIndex<infoTable.getRowCount();rowIndex++){ //利用for循环 rowIndex指行索引,第一行 rowIndex==0,getRowCount()获得JTable行数
//将单科成绩 Object data[][].toString,在parseInt()转化为int,最后相加,赋值给data[rowIndex][6],
data[rowIndex][6]=Integer.parseInt(data[rowIndex][4].toString())+Integer.parseInt(data[rowIndex][3].toString())+Integer.parseInt(data[rowIndex][5].toString());
String sumScore=data[rowIndex][6].toString();//将int data[][].toString(),赋值定义String变量sumScore,
data[rowIndex][6]=sumScore;
infoTable.setValueAt(data[rowIndex][6], rowIndex, 6);
}
}
});
avg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int rowIndex=0;rowIndex<infoTable.getRowCount();rowIndex++){ //利用for循环 rowIndex指行索引,第一行 rowIndex==0,getRowCount()获得JTable行数
//将单科成绩 Object data[][].toString,在parseInt()转化为int,最后相加,赋值给data[rowIndex][6],
data[rowIndex][7]=(Double.parseDouble(data[rowIndex][4].toString())+Double.parseDouble(data[rowIndex][5].toString())+Double.parseDouble(data[rowIndex][3].toString()))/3;
DecimalFormat decimalFormat=new DecimalFormat(".00"); //.00表示保留两位小数
String sumScore=decimalFormat.format(data[rowIndex][7]);//将data[][]格式化为两位小数赋值给String类型sumScore
data[rowIndex][7]=sumScore;//将转化后的sumScore值又赋给data[][]
infoTable.setValueAt(data[rowIndex][7], rowIndex, 7);
//System.out.println(data[rowIndex][7]);
}
}
});
/**
*
* 警告: DefaultTableModel 返回 Object 的一个列类。 DefaultTableModel 与
* TableRowSorter 一起使用时将导致大量使用 toString,这对于非 String 数据类型代价昂贵。 如果与
* TableRowSorter 一起使用 DefaultTableModel,建议最好重写 getColumnClass 以返回适当的类型。
*
* 知识点:java反射
*/
tableModel = new DefaultTableModel(data, columnNames) { // 声明tableModel对象
// TODO 未完成 (重写方法之后排序还是不正确)
public Class getColumnClass(int i) { // 重写getColumnClass()方法 (int
// columnIndex)
Class returnValue;
if ((i >= 0) && (i < getColumnCount())) { // 如果索引存在
returnValue = getValueAt(0, i).getClass(); // 获取Object(第一行,第i+1列)值的类,赋值给新类
} else {
returnValue = Object.class;
}
return returnValue; // 返回值
}
};
infoTable = new JTable(tableModel) {
public boolean isCellEditable(int row, int clm) { // 重写isCellEditable()方法,默认为true,当程序刚运行,设置为不可编辑
//return false;
return false;
}
};
infoTable.setGridColor(Color.pink); // 网格的颜色
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel); // table.setRowSorter(new
// TableRowSorter(model))
// 排序
infoTable.setRowSorter(sorter);
infoTable.setFillsViewportHeight(true);// 如果该表的首选高度小于视口,则将会延伸该表以填充视口,确保该表永远不会小于该视口,默认值false
scrollPane = new JScrollPane(infoTable);
infoPanel.add(scrollPane);// 给学生信息面板添加滚动条
infoPanel.setVisible(true);
infoTable.setShowGrid(true);// 绘制单元格周围的网格线
filterPanel = new JPanel(new GridLayout(3, 1));
filterLabel = new JLabel("下方输入过滤条件(区分大小写)");
filterTxtField = new JTextField();
filterTxtField.setForeground(Color.gray);
filterBtn = new JButton("过滤");
filterPanel.add(filterLabel);// 将过滤提示label放在过滤面板
filterPanel.add(filterTxtField);// 将过滤文本框添加到过滤面板
filterPanel.add(filterBtn); // 将过滤按钮添加到过滤面板
column = new TableColumn();
column = infoTable.getColumnModel().getColumn(2);// 第三列(班级)
JComboBox cBox = new JComboBox();
cBox.addItem("软件1714");
cBox.addItem("软件1713");
cBox.addItem("软件1715");
column.setCellEditor(new DefaultCellEditor(cBox));
/**
* 给指定列设置宽度
*/
for (int col = 0; col <= 7; col++) {
if (col == 7) {
column = infoTable.getColumnModel().getColumn(7);// 获得第8列
column.setPreferredWidth(165); // 设置第8列为150
} else {
column.setPreferredWidth(90); // 设置其他列宽度为50
}
}
filterBtn.addActionListener(new ActionListener() { // 给过滤按钮添加监听事件
@Override
public void actionPerformed(ActionEvent e) {
String text = filterTxtField.getText(); // 获得文本框中的文本赋值给text
// TODO Auto-generated method stub
if (text.length() == 0) { // 如果没有文本
sorter.setRowFilter(null); // 则不过滤
} else {
sorter.setRowFilter(RowFilter.regexFilter(text)); // regexFilter()只包含至少有一个匹配值的条目
}
}
});
bigCon.add(filterPanel, BorderLayout.CENTER);
// infoPanel.add(filterBtn);
Container littleCon = searchDialog.getContentPane();// 给查询对话框创建一个容器
littleCon.setLayout(new BorderLayout());// 给查询对话框设置布局
maxminPanel = new JPanel(new GridLayout(1, 2, 10, 10)); // 将最高分最低分按钮添加到一个panel里,设置布局为GridLayout
// (1行,2列,水平间距10,垂直间距10)
littleCon.add(maxminPanel, BorderLayout.CENTER);// 将最高分最低分panel添加到查询对话框
maxBtn = new JRadioButton("最高分");
minBtn = new JRadioButton("最低分");
maxAndmin = new ButtonGroup();
maxAndmin.add(maxBtn);
maxAndmin.add(minBtn);
maxminPanel.add(maxBtn); // 将maxBtn放在panel
maxminPanel.add(minBtn); // 将minBtn放在panel
bigCon.add(btnPanel, BorderLayout.EAST);
searchBtn.addActionListener(this);// 调用searchBtn的监听事件方法
filterTxtField.addActionListener(this);
insertBtn.addActionListener(this);
}
还有下面这个问题 我设置dialog.toFront(),运行之后对话框在大面板前面闪一下,就又到大面板后面了,,谢谢们:)
