JFrame保持在后台而不是关闭

时间:2012-11-06 14:10:04

标签: java swing jframe

我的JFrame有问题。 当我单击“返回”按钮时,会出现“Classroomdetails”菜单,但是教室输入JFrame表单(addclassroom)保留在后台而不是关闭 请帮我纠正一下。

这是我的代码::

public class addclassroom extends JFrame{

    public JTextField txt_classno,txt_bname,txt_size,txt_resources;
    public JComboBox cmbfloor;
    public JLabel label1, label2, label3, label4, label5, label6;
    public JButton Save;
    public JButton Reset;
    public JButton Back;

    public addclassroom(){


        label1 = new javax.swing.JLabel();
        label1.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
        label1.setForeground(Color.BLACK);
        label1.setText("Classroom Records Input");


        label2 = new javax.swing.JLabel("Classroom_No:");
        txt_classno = new javax.swing.JTextField(8);

        label3 = new javax.swing.JLabel("Building Name:");
        txt_bname = new javax.swing.JTextField(15);

        label4 = new javax.swing.JLabel("Building Floor:");
        String floor[] = {"Ground floor","1st floor","2nd floor","3rd floor","4th floor", "5th floor", "6th floor", "7th floor", "8th floor"};
        cmbfloor = new javax.swing.JComboBox(floor);

        label5 = new javax.swing.JLabel("Size Capacity:");
        txt_size = new javax.swing.JTextField(5);


        label6 = new javax.swing.JLabel("Resources");
        txt_resources = new javax.swing.JTextField(20);




        Save = new javax.swing.JButton();
        Save.setText("Save");

        Reset = new javax.swing.JButton();
        Reset.setText("Reset");

        Back = new javax.swing.JButton();
        Back.setText("Back");


        label1.setBounds(100, 25, 200, 50);

        label2.setBounds(50, 100, 100, 50);
        txt_classno.setBounds(180, 100, 100, 30);



        label3.setBounds(50, 150, 100, 50);
        txt_bname.setBounds(180, 150, 140, 30);

        label4.setBounds(50, 200, 100, 50);
        cmbfloor.setBounds(180, 200, 115, 30);



        label5.setBounds(50, 250, 100, 50);
        txt_size.setBounds(180, 250, 65, 30);



        label6.setBounds(50, 300, 100, 50);
        txt_resources.setBounds(180, 300, 175, 30);



        Save.setBounds(100, 400, 75, 20);
        Reset.setBounds(200, 400, 75, 20);
        Back.setBounds(300,400,75,20);

        JFrame ciFrame = new JFrame("Classroom Records Input Form");

        ciFrame.add(label1);
        ciFrame.add(label2);
        ciFrame.add(txt_classno);
        ciFrame.add(label3);
        ciFrame.add(txt_bname);
        ciFrame.add(label4);
        ciFrame.add(cmbfloor);
        ciFrame.add(label5);
        ciFrame.add(txt_size);
        ciFrame.add(label6);
        ciFrame.add(txt_resources);

        ciFrame.add(Save);

        ciFrame.add(Reset);
        ciFrame.add(Back);


        ciFrame.setLayout(null);

        ciFrame.setVisible(true);

        ciFrame.setSize(500, 500);

        ciFrame.setResizable(false);

        ciFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 










        Save.setMnemonic('S');
        Save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                Save.setEnabled(true);
                }
        });



        Reset.setMnemonic('R');
        Back.setMnemonic('B');


        Handleraddclassroom h = new Handleraddclassroom();
        Save.addActionListener(h);
        Reset.addActionListener(h);
        Back.addActionListener(h);




    }


    public class Handleraddclassroom implements ActionListener{
        public void actionPerformed(ActionEvent event){

            String name = ((JButton)event.getSource()).getText();


            if (name.equals("Back")){
                dispose();

                Classroomdetails cm = new Classroomdetails();


            }


            if (name.equals("Reset")){

                txt_classno.setText("");
                txt_bname.setText("");
                txt_size.setText("");
                txt_resources.setText("");




            }



            if (name.equals("Save")){
                String filename = "C:/JAVA/schedule.accdb";
                String database = "jdbc:odbc:scheduling";




                //Character & Presence checks

                String nsize = txt_size.getText();
                String rsc = txt_resources.getText();

                Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
                Matcher Cnsize = p.matcher(nsize);

                Pattern q = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
                Matcher Rsc = q.matcher(rsc);


                if(txt_classno.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Classroom No field should not be blank");

                }

                if(txt_bname.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Building Name field should not be blank");

                }

                if(txt_size.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Size Capacity field should not be blank");

                }else if(Cnsize.find()){
                    JOptionPane.showMessageDialog(addclassroom.this,"Invalid value for Size capacity entered. Please check again");

                }




                if(txt_resources.getText().equals("")){
                    JOptionPane.showMessageDialog(addclassroom.this,"Resources field should not be blank");

                }else if(!Rsc.find()){
                    JOptionPane.showMessageDialog(addclassroom.this,"Invalid value for Resources entered. Please check again");
                }









            else{
                try {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


                    Connection con = DriverManager.getConnection(database ,"","");  //username &password not being used

                    String flr="";
                    if(cmbfloor.getSelectedIndex()==0){
                        flr="Ground floor"; }
                        else if(cmbfloor.getSelectedIndex()==1){
                            flr="1st floor";}

                        else if(cmbfloor.getSelectedIndex()==2){
                            flr="2nd floor";}
                        else if(cmbfloor.getSelectedIndex()==3){
                            flr="3rd floor";}
                        else if(cmbfloor.getSelectedIndex()==4){
                            flr="4th floor";}
                        else if(cmbfloor.getSelectedIndex()==5){
                            flr="5th floor";}
                        else if(cmbfloor.getSelectedIndex()==6){
                            flr="6th floor";}
                        else if(cmbfloor.getSelectedIndex()==7){
                            flr="7th floor";}
                        else if(cmbfloor.getSelectedIndex()==8){
                            flr="8th floor";}







                Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //The Result set can now scroll forward and back



                String sql = "INSERT INTO classroom VALUES('" 

                    + txt_classno.getText()+ "','"
                    + txt_bname.getText() + "','"

                    + flr + "','" 
                    + txt_size.getText() + "','"
                    + txt_resources.getText()+ "' )";

                JOptionPane.showMessageDialog(addclassroom.this,"Classroom Records Saved.");
                dispose();

                addclassroom ac = new addclassroom();


                s.execute(sql);



                s.close();
                con.close();

                } catch (Exception e) {System.out.print("Error: " + e);}
                }
            }
        }   


    }



    }

2 个答案:

答案 0 :(得分:2)

您的代码令人困惑,因为您的主要类扩展了JFrame,但您还创建了一个名为JFrame的内部ciFrame,这是实际显示的内容。{/ p>

快速解决方法是移动ciFrame旁边的JButton Back声明(不要将其设为public并将其称为back,而不是Back 1}})。然后,而不是dispose()ciFrame.dispose();

长期来说,请更改您的代码,例如扩展JFrame并仅使用this的{​​{1}}实例或创建一个作为变量,并且不要扩展{{1 }}

我不喜欢发布100行代码,但是在这里,你的代码被修改为可以工作:

JFrame

答案 1 :(得分:2)

public class addclassroom 
{
public JTextField txt_classno,txt_bname,txt_size,txt_resources;
public JComboBox cmbfloor;
public JLabel label1, label2, label3, label4, label5, label6;
public JButton Save;
public JButton Reset;
public JButton Back;

public addclassroom(){


    label1 = new javax.swing.JLabel();
    label1.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
    label1.setForeground(Color.BLACK);
    label1.setText("Classroom Records Input");


    label2 = new javax.swing.JLabel("Classroom_No:");
    txt_classno = new javax.swing.JTextField(8);

    label3 = new javax.swing.JLabel("Building Name:");
    txt_bname = new javax.swing.JTextField(15);

    label4 = new javax.swing.JLabel("Building Floor:");
    String floor[] = {"Ground floor","1st floor","2nd floor","3rd floor","4th floor", "5th floor", "6th floor", "7th floor", "8th floor"};
    cmbfloor = new javax.swing.JComboBox(floor);

    label5 = new javax.swing.JLabel("Size Capacity:");
    txt_size = new javax.swing.JTextField(5);


    label6 = new javax.swing.JLabel("Resources");
    txt_resources = new javax.swing.JTextField(20);




    Save = new javax.swing.JButton();
    Save.setText("Save");

    Reset = new javax.swing.JButton();
    Reset.setText("Reset");

    Back = new javax.swing.JButton();
    Back.setText("Back");


    label1.setBounds(100, 25, 200, 50);

    label2.setBounds(50, 100, 100, 50);
    txt_classno.setBounds(180, 100, 100, 30);



    label3.setBounds(50, 150, 100, 50);
    txt_bname.setBounds(180, 150, 140, 30);

    label4.setBounds(50, 200, 100, 50);
    cmbfloor.setBounds(180, 200, 115, 30);



    label5.setBounds(50, 250, 100, 50);
    txt_size.setBounds(180, 250, 65, 30);



    label6.setBounds(50, 300, 100, 50);
    txt_resources.setBounds(180, 300, 175, 30);



    Save.setBounds(100, 400, 75, 20);
    Reset.setBounds(200, 400, 75, 20);
    Back.setBounds(300,400,75,20);

    JFrame ciFrame = new JFrame("Classroom Records Input Form");

    ciFrame.add(label1);
    ciFrame.add(label2);
    ciFrame.add(txt_classno);
    ciFrame.add(label3);
    ciFrame.add(txt_bname);
    ciFrame.add(label4);
    ciFrame.add(cmbfloor);
    ciFrame.add(label5);
    ciFrame.add(txt_size);
    ciFrame.add(label6);
    ciFrame.add(txt_resources);

    ciFrame.add(Save);

    ciFrame.add(Reset);
    ciFrame.add(Back);


    ciFrame.setLayout(null);

    ciFrame.setVisible(true);

    ciFrame.setSize(500, 500);

    ciFrame.setResizable(false);

    ciFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  










    Save.setMnemonic('S');
    Save.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            Save.setEnabled(true);
            }
    });



    Reset.setMnemonic('R');
    Back.setMnemonic('B');


    Handleraddclassroom h = new Handleraddclassroom();
    Save.addActionListener(h);
    Reset.addActionListener(h);
    Back.addActionListener(h);




}


public class Handleraddclassroom implements ActionListener{

    public void actionPerformed(ActionEvent event){

        String name = ((JButton)event.getSource()).getText();


        if (name.equals("Back")){


            Classroomdetails cm = new Classroomdetails();
            dispose();  

        }


        if (name.equals("Reset")){

            txt_classno.setText("");
            txt_bname.setText("");
            txt_size.setText("");
            txt_resources.setText("");




        }



        if (name.equals("Save")){
            String filename = "C:/JAVA/schedule.accdb";
            String database = "jdbc:odbc:scheduling";




            //Character & Presence checks

            String nsize = txt_size.getText();
            String rsc = txt_resources.getText();

            Pattern p = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
            Matcher Cnsize = p.matcher(nsize);

            Pattern q = Pattern.compile("[A-Z,a-z,&%$@!()*^]");
            Matcher Rsc = q.matcher(rsc);


            if(txt_classno.getText().equals("")){
                JOptionPane.showMessageDialog(null,"Classroom No field should not be blank");

            }

            if(txt_bname.getText().equals("")){
                JOptionPane.showMessageDialog(null,"Building Name field should not be blank");

            }

            if(txt_size.getText().equals("")){
                JOptionPane.showMessageDialog(null,"Size Capacity field should not be blank");

            }else if(Cnsize.find()){
                JOptionPane.showMessageDialog(null,"Invalid value for Size capacity entered. Please check again");

            }




            if(txt_resources.getText().equals("")){
                JOptionPane.showMessageDialog(null,"Resources field should not be blank");

            }else if(!Rsc.find()){
                JOptionPane.showMessageDialog(null,"Invalid value for Resources entered. Please check again");
            }









        else{
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


                Connection con = DriverManager.getConnection(database ,"","");  //username &password not being used

                String flr="";
                if(cmbfloor.getSelectedIndex()==0){
                    flr="Ground floor"; }
                    else if(cmbfloor.getSelectedIndex()==1){
                        flr="1st floor";}

                    else if(cmbfloor.getSelectedIndex()==2){
                        flr="2nd floor";}
                    else if(cmbfloor.getSelectedIndex()==3){
                        flr="3rd floor";}
                    else if(cmbfloor.getSelectedIndex()==4){
                        flr="4th floor";}
                    else if(cmbfloor.getSelectedIndex()==5){
                        flr="5th floor";}
                    else if(cmbfloor.getSelectedIndex()==6){
                        flr="6th floor";}
                    else if(cmbfloor.getSelectedIndex()==7){
                        flr="7th floor";}
                    else if(cmbfloor.getSelectedIndex()==8){
                        flr="8th floor";}







            Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //The Result set can now scroll forward and back



            String sql = "INSERT INTO classroom VALUES('" 

                + txt_classno.getText()+ "','"
                + txt_bname.getText() + "','"

                + flr + "','" 
                + txt_size.getText() + "','"
                + txt_resources.getText()+ "' )";

            JOptionPane.showMessageDialog(null,"Classroom Records Saved.");



            s.execute(sql);



            s.close();
            con.close();

            } catch (Exception e) {System.out.print("Error: " + e);}
            }
        }
    }   


}


private void dispose() {


}

}