jdialog关闭不了

fladier 2008-01-06 08:05:13
class searchdlg extends JDialog implements ItemListener,ActionListener
{
Button btn2=new Button("取消");

public searchdlg(Frame df)
{
super(df,"查找",true);

df.add(btn2);
df.setSize(300,100);
df.setVisible(true);
this.setResizable(false);
this.setModal(true);
btn2.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="取消")searchdlg.this.dispose();
}
}
我在这个jdialog中点击“取消”却怎么也不能关闭,点击右上角的“关闭”时可以关闭。
怎样才能点取消时也能关闭呢?
...全文
283 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fladier 2008-01-08
  • 打赏
  • 举报
回复
可以了,谢谢
fladier 2008-01-08
  • 打赏
  • 举报
回复
可是怎样该才能点取消关闭JDialog呢?
fladier 2008-01-08
  • 打赏
  • 举报
回复
这个是我的一个记事本程序中的查找对话框,由源程序调用。
没想到找出这么多不好的地方来,汗一个。
多谢多谢!
lzmailcn 2008-01-06
  • 打赏
  • 举报
回复
不怪楼上说你,你这程序写得...。
第一个错,df.add(btn2);
df.setSize(300,100);
df.setVisible(true); 所有的设置都是针对“所有者”,而不是对话框。按钮都加到“所有者”中了。把df改为this。
第二个错,ae.getActionCommand()=="取消"编译没有提示吗?改为ae.getActionCommand().equals("取消")。这可以原则错误。
第三个问题,this.add(btn2);
this.setSize(300,100);
this.setVisible(true);
this.setResizable(false);
this.setModal(true);
btn2.addActionListener(this);你不觉得逻辑有问题吗?最好是这样:
btn2.addActionListener(this);
df.add(btn2);
df.setSize(300,100);
this.setResizable(false);
this.setModal(true);
df.setVisible(true);
第四个问题,把df.setVisible(true); 放在构造函数里是一个糟糕的想法,把它拿掉。如果要测试,写一个main方法:
public static void main(String[] args){
searchdlg sd=new searchdlg(new javax.swing.JFrame());
sd.setVisible(true);
}


daimojingdeyu 2008-01-06
  • 打赏
  • 举报
回复
汗,这个程序写得生猛,取消按钮是在Frame上的,对话框是模态的,而且是没有显示出来的一个对话框。多亏没有显示出来,如果出来的话,真的不知道怎样才能点到那个取消按钮。
3gp视频的分割格式转换视频的分割 import java.awt.Dimension; import java.awt.Frame; import java.awt.Rectangle; import javax.swing.JDialog; import javax.swing.JTextField; public class About extends JDialog { public About(Frame owner, String title, boolean modal) { // 构造方法 super(owner, title, modal); // 继承 this.setLocationRelativeTo(this); this.setSize(240, 260); // 设置大小 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 关闭处理 initUI(); } private void initUI() { this.getContentPane().setLayout(null); jTextField1.setText("我们的口号: 只有想不到,没有做不到"); // 加入文本内容 jTextField1.setEditable(false); jTextField1.setBounds(new Rectangle(1, 29, 239, 41)); // 设置大小 jTextField2.setText("本组成员: 王榆升(组长) 曾佳林 钱彦"); // 加入文本内容 jTextField2.setBounds(new Rectangle(0, 69, 240, 35)); // 设置大小 jTextField2.setEditable(false); jTextField3.setMinimumSize(new Dimension(10, 20)); jTextField3.setEditable(false); jTextField3.setText("版本: 2.3.0"); jTextField3.setBounds(new Rectangle(0, 102, 240, 37)); // 设置大小 jTextField4.setText("最后修定时间: 2008-08-21"); // 加入文本内容 jTextField4.setBounds(new Rectangle(0, 139, 240, 30)); // 设置大小 jTextField4.setEditable(false); jTextField5.setText(""); // 加入文本内容 jTextField5.setBounds(new Rectangle(1, 168, 240, 38)); // 设置大小 jTextField5.setEditable(false); this.getContentPane().add(jTextField1); // 加文本框到容器 this.getContentPane().add(jTextField2); // 加文本框到容器 this.getContentPane().add(jTextField3); // 加文本框到容器 this.getContentPane().add(jTextField4); // 加文本框到容器 this.getContentPane().add(jTextField5); // 加文本框到容器 } JTextField jTextField1 = new JTextField(); // 产生文本录入组件 JTextField jTextField2 = new JTextField(); JTextField jTextField3 = new JTextField(); // 产生文本录入组件 JTextField jTextField4 = new JTextField(); JTextField jTextField5 = new JTextField(); // 产生文本录入组件 }
用于java程序的设计和开发使用import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.*;class Student implements java.io.Serializable{String number,name,specialty,grade,borth,sex;public Student(){};public void setNumber(String number){ this.number=number;}public String getNumber(){ return number;}public void setName(String name){ this.name=name;}public String getName(){ return name;}public void setSex(String sex){ this.sex=sex;}public String getSex(){ return sex;}public void setSpecialty(String specialty){ this.specialty=specialty;}public String getSpecialty(){ return specialty;}public void setGrade(String grade){ this.grade=grade;}public String getGrade(){ return grade;}public void setBorth(String borth){ this.borth=borth;}public String getBorth(){ return borth;}}public class StudentManager extends JFrame{JLabel lb=new JLabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询" +"内容改后的保存!");static JTextField Jnumber,Jname,Jspecialty,Jgrade,Jborth;static JRadioButton boy,girl;static ButtonGroup group=null;static JButton Login,select,delete,update,show;static JPanel p1,p2,p3,p4,p5,p6,pv,ph;static Student student=null;static Hashtable Shashtable=null;static File file=null;static FileInputStream inOne=null;static ObjectInputStream inTwo=null;static FileOutputStream outOne=null;static ObjectOutputStream outTwo=null;public StudentManager(){super("学生基本信息管理系统");Jnumber=new JTextField(10);Jname =new JTextField(10);Jspecialty=new JTextField(10);Jgrade=new JTextField(10);Jborth=new JTextField(10);group=new ButtonGroup();boy=new JRadioButton("男",true);girl=new JRadioButton("女",false);group.add(boy);group.add(girl);Login=new JButton("录入");select=new JButton("查询");delete=new JButton("删除");update=new JButton("修改");show=new JButton("显示");Login.addActionListener(new InputAct());select.addActionListener(new InquestAct());update.addActionListener(new ModifyAct());delete.addActionListener(new DeleteAct());show.addActionListener(new ShowAct());update.setEnabled(false);p1=new JPanel();p1.add(new JLabel("学号:",JLabel.CENTER));p1.add(Jnumber);p2=new JPanel();p2.add(new JLabel("姓名:",JLabel.CENTER));p2.add(Jname);p3=new JPanel();p3.add(new JLabel("性别:",JLabel.CENTER));p3.add(boy);p3.add(girl);p4=new JPanel();p4.add(new JLabel("专业:",JLabel.CENTER));p4.add(Jspecialty);p5=new JPanel();p5.add(new JLabel("年级:",JLabel.CENTER));p5.add(Jgrade);p6=new JPanel();p6.add(new JLabel("出生:",JLabel.CENTER));p6.add(Jborth);pv=new JPanel();pv.setLayout(new GridLayout(6,1));pv.add(p1);pv.add(p2);pv.add(p3);pv.add(p4);pv.add(p5);pv.add(p6);ph=new JPanel();ph.add(Login);ph.add(select);ph.add(update);ph.add(delete);ph.add(show);file=new File("学生信息.txt");Shashtable=new Hashtable();if(!file.exists()){try{FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out);objectOut.writeObject(Shashtable);objectOut.close();out.close();}catch(IOException e){}}Container con=getContentPane();con.setLayout(new BorderLayout());con.add(lb, BorderLayout.NORTH);con.add(pv, BorderLayout.CENTER);con.add(ph, BorderLayout.SOUTH);setDefaultCloseOperation(EXIT_ON_CLOSE);setBounds(100,100,600,300);setVisible(true);}public static void main(String[] args) {new StudentManager();}class InputAct implements ActionListener{public void actionPerformed(ActionEvent e){update.setEnabled(false);String number="";number=Jnumber.getText();if(number.length()>0){try{inOne=new FileInputStream(file);inTwo=new ObjectInputStream(inOne);Shashtable=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){System.out.println("创建散列表出现问题!");}if(Shashtable.containsKey(number)){String warning="该生信息已存在,请到修改页面修改!";JOptionPane.showMessageDialog(null,warning,"警告",JOptionPane.WARNING_MESSAGE);}//end if1else{String m="该生信息将被录入!";int ok=JOptionPane.showConfirmDialog(null,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);if(ok==JOptionPane.YES_OPTION){String name=Jname.getText();String specialty=Jspecialty.getText();String grade=Jgrade.getText();String borth=Jborth.getText();String sex=null;if(boy.isSelected()){sex=boy.getText();}else{sex=girl.getText();}student=new Student();student.setNumber(number);student.setName(name);student.setSpecialty(specialty);student.setGrade(grade);student.setBorth(borth);student.setSex(sex);try{outOne=new FileOutputStream(file);outTwo=new ObjectOutputStream(outOne);Shashtable.put(number,student);outTwo.writeObject(Shashtable);outTwo.close();outOne.close();}catch(Exception ee){System.out.println("输出散列表出现问题!");}Jnumber.setText(null);Jname.setText(null);Jspecialty.setText(null);Jgrade.setText(null);Jborth.setText(null);}}//end else1}//end if0else{String warning="必须输入学号!";JOptionPane.showMessageDialog(null,warning,"警告",JOptionPane.WARNING_MESSAGE);}//end else0}//end actionPerformed}//end classclass InquestAct implements ActionListener{public void actionPerformed(ActionEvent e){String number="";number=Jnumber.getText();if(number.length()>0){try{inOne=new FileInputStream(file);inTwo=new ObjectInputStream(inOne);Shashtable=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){System.out.println("散列表有问题!");}if(Shashtable.containsKey(number)){update.setEnabled(true);Student stu=(Student)Shashtable.get(number);Jname.setText(stu.getName());Jspecialty.setText(stu.getSpecialty());Jgrade.setText(stu.getGrade());Jborth.setText(stu.getBorth());if(stu.getSex().equals("男")){boy.setSelected(true);}else{girl.setSelected(true);}}else{update.setEnabled(false);String warning="该学号不存在!";JOptionPane.showMessageDialog(null,warning,"警告",JOptionPane.WARNING_MESSAGE);}}else{update.setEnabled(false);String warning="必须输入学号!";JOptionPane.showMessageDialog(null,warning,"警告",JOptionPane.WARNING_MESSAGE);}}}class ModifyAct implements ActionListener{public void actionPerformed(ActionEvent e){String number=Jnumber.getText();String name=Jname.getText();String specialty=Jspecialty.getText();String grade=Jgrade.getText();String borth=Jborth.getText();String sex=null;if(boy.isSelected()){sex=boy.getText();}else{sex=girl.getText();}Student 学生=new Student();学生.setNumber(number);学生.setName(name);学生.setSpecialty(specialty);学生.setGrade(grade);学生.setBorth(borth);学生.setSex(sex);try{outOne=new FileOutputStream(file);outTwo=new ObjectOutputStream(outOne);Shashtable.put(number, 学生);outTwo.writeObject(Shashtable);outTwo.close();outOne.close();Jnumber.setText(null);Jname.setText(null);Jspecialty.setText(null);Jgrade.setText(null);Jborth.setText(null);}catch(Exception ee){System.out.println("录入修改出现异常!");update.setEnabled(false);}}}class DeleteAct implements ActionListener{public void actionPerformed(ActionEvent e){update.setEnabled(false);String number=Jnumber.getText();if(number.length()>0){try{inOne=new FileInputStream(file);inTwo=new ObjectInputStream(inOne);Shashtable=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){}if(Shashtable.containsKey(number)){Student stu=(Student)Shashtable.get(number);Jname.setText(stu.getName());Jspecialty.setText(stu.getSpecialty());Jgrade.setText(stu.getGrade());Jborth.setText(stu.getBorth());if(stu.getSex().equals("男")){boy.setSelected(true);}else{girl.setSelected(true);}}String m="确定要删除该学生的记录吗?";int ok=JOptionPane.showConfirmDialog(null,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if(ok==JOptionPane.YES_OPTION){Shashtable.remove(number);try{outOne=new FileOutputStream(file);outTwo=new ObjectOutputStream(outOne);outTwo.writeObject(Shashtable);outTwo.close();outOne.close();Jnumber.setText(null);Jname.setText(null);Jspecialty.setText(null);Jgrade.setText(null);Jborth.setText(null);}catch(Exception ee){System.out.println(ee);}}else if(ok==JOptionPane.NO_OPTION){Jnumber.setText(null);Jname.setText(null);Jspecialty.setText(null);Jgrade.setText(null);Jborth.setText(null);}else{String warning="该学号不存在!";JOptionPane.showMessageDialog(null,warning,"警告",JOptionPane.WARNING_MESSAGE);}}else{String warning="必须输入学号!";JOptionPane.showMessageDialog(null,warning,"警告",JOptionPane.WARNING_MESSAGE);}}}class ShowAct implements ActionListener{public void actionPerformed(ActionEvent e){new StudentShow(file);}}class StudentShow extends JDialog{Hashtable 学生散列表= null;JTextArea 显示=null;FileInputStream inOne=null;ObjectInputStream inTwo=null;File file=null;public StudentShow(File file){super(new JFrame(),"显示对话框");this.file=file;显示=new JTextArea(16,30);try{inOne=new FileInputStream(file);inTwo=new ObjectInputStream(inOne);学生散列表=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){}if(学生散列表.isEmpty())显示.append("目前还没有学生的信息记录!\n");else{显示.setText("学号 姓名 性别 专业 年级 出生\n");for(Enumeration enm=学生散列表.elements();enm.hasMoreElements();){Student stu=(Student)enm.nextElement();String sex="";if(stu.getSex().equals("男"))sex="男";else sex="女";String str=stu.getNumber()+","+stu.getName()+","+sex+","+stu.getSpecialty()+","+stu.getGrade()+","+stu.getBorth()+"\n";显示.append(str);}}JScrollPane scroll=new JScrollPane(显示);Container con=getContentPane();con.add("Center",scroll);con.validate();setVisible(true);setBounds(200,200,400,300);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){setVisible(false);}});}}}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧