Java 单击按钮时,我的Jframe不会保持打开状态
我有和编辑按钮,它应该打开一个不同的Jframe,但由于某种原因,它会在屏幕上闪烁并消失。我想不出来也许你们可以。“我的删除”按钮将删除选定行上方的行。帧位于250左右,按下的按钮位于第323行Java 单击按钮时,我的Jframe不会保持打开状态,java,swing,user-interface,jframe,jbutton,Java,Swing,User Interface,Jframe,Jbutton,我有和编辑按钮,它应该打开一个不同的Jframe,但由于某种原因,它会在屏幕上闪烁并消失。我想不出来也许你们可以。“我的删除”按钮将删除选定行上方的行。帧位于250左右,按下的按钮位于第323行 Button declaration: btnAdd = new JButton("Add Student"); btnAdd.addActionListener(bh); btnEdit = new JButton("EDIT"); btnEdit.ad
Button declaration:
btnAdd = new JButton("Add Student");
btnAdd.addActionListener(bh);
btnEdit = new JButton("EDIT");
btnEdit.addActionListener(bh);
btnEdit.setEnabled(false);
btnDelete = new JButton("DELETE");
btnDelete.addActionListener(bh);
btnDelete.setEnabled(false);
btnSort = new JButton("Update");
btnSort.addActionListener(bh);
btnSave = new JButton("SAVE");
btnSave.addActionListener(bh);
btnSave.setActionCommand("Save");
btnAddInput = new JButton("Add Student");
btnAddInput.addActionListener(bh);
btnAddInput.setActionCommand("AddInput");
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(bh);
框架声明:
frame1 = new JFrame("Edit Student");
frame1.setVisible(false);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame1.add(addPanel, BorderLayout.CENTER);
frame1.add(buttonPanel2, BorderLayout.PAGE_END);
frame1.pack();
按钮处理程序:类ButtonHandler实现ActionListener{ 已执行的公共无效操作(操作事件e){
if(例如getActionCommand().equals(“添加学生”)){
txtID.setText(“”);
txtName.setText(“”);
txtMajor.setText(“”);
txtGPA.setText(“”);
txtcumpum.setText(“”);
txtAddress.setText(“”);
txtPhone.setText(“”);
txtEmail.setText(“”);
txtCurrent.setText(“”);
txtposs.setText(“”);
txtFuture.setText(“”);
txtNotes.setText(“”);
frame1.setTitle(“添加学生数据”);//添加的标题栏名称
frame1.setVisible(true);
学生=新学生(txtID.getText(),txtName.getName(),txtmarent.getText(),txtGPA.getText(),txtcumpump.getText(),txtdAddress.getText(),txtPhone.getText(),txtmail.getText(),txtCurrent.getText(),txtPast.getText(),txtFuture.getText(),txtNotes.getText());
加上(学生);
试一试{
saveSerialized(Student,txtID.getText());
}捕获(IOEX异常){
Logger.getLogger(IAdvise.class.getName()).log(Level.SEVERE,null,ex);
}
}else if(例如getActionCommand().equals(“编辑”)){
frame1.setVisible(true);
setText(数据[rowIndex][0]+“”);
setText(数据[rowIndex][1]+“”);
setText(数据[rowIndex][2]+“”);
setText(数据[rowIndex][3]+“”);
setText(数据[rowIndex][4]+“”);
setText(数据[rowIndex][5]+“”);
setText(数据[rowIndex][6]+“”);
setText(数据[rowIndex][7]+“”);
txtCurrent.setText(数据[行索引][8]+“”);
setText(数据[rowIndex][9]+“”);
setText(数据[rowIndex][10]+“”);
setText(数据[rowIndex][11]+”);
txtID.setEditable(false);
框架1.设置标题(“输入学生数据”);
btnAddInput.setActionCommand(“Edit2”);
btnAddInput.setText(“接受”);
}else if(例如getActionCommand().equals(“删除”)){
int confirm=JOptionPane.showConfirmDialog(框架“确定吗?”,“确认”,
JOptionPane.YES\u NO\u选项);
如果(确认==0){
rowIndex=table.getSelectedRow();
行数=0;
noOfStudents——;
对于(int i=0;i基本上,当您在帧上调用setVisible
时,代码将继续运行而不停止
这导致的是
frame1.setVisible(true);
.
.
.
frame1.dispose();
基本上,您可以使框架可见,但稍后在代码中,您将处理它
您真正想要的是一个模态对话框,当它变得可见时,将阻止代码执行,直到关闭为止
请查看以了解更多详细信息
回顾…
不要扩展PlainDocument
来执行字段过滤,而是使用DocumentFilter
不要在文本字段上使用KeyListener
执行筛选,而是使用DocumentFilter
不要调用JTable.updateUI
。这与UI内容更改时更新UI无关,用于在UI内容更改时更新外观。相反,请依赖TableModel
,并引发适当的事件来通知该表自行更新
降低actionPerformed
方法的复杂性。尝试将逻辑分解为单独的方法,甚至可能是单独的ActionListener
s,或者如果您真的想尝试模块化和高级的方法,请看一看我做了类似于您的事情,结果与您相同,因此我可以为您提供一种方法,
您可以获得一个方法来打印外观,并且frame.setVible(true)可以由一个方法调用,
创建更多的框架,当您想要更新或重新绘制它时,您可以调用一个方法。
我希望我所说的能对你有所帮助。请删减你的代码示例。你也指的是巫婆按钮。“编辑”按钮和对不起,我是新的+1,你键入的速度比我快得多。我正忙着键入所有这些点。@camickr我已经有一段时间没有打败你了;)
frame1.setVisible(true);
.
.
.
frame1.dispose();