如何使后退按钮工作从第二帧到第一帧

时间:2016-03-12 16:32:19

标签: java swing jdbc jframe

我有一个任务,我在制作这个后退按钮时得到了堆栈。我可以从第一帧到第二帧,但不能从第二帧到第一帧。

第一帧

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Test extends JFrame implements ActionListener{
    JLabel label;
    JButton Add,Delete,Update,Display,Exit;
    public Test() {
        setVisible(true);
        setSize(500, 500);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("JDBC");
        label = new JLabel("Database");
        Add = new JButton("Add Data");
        Delete = new JButton("Delete Data");
        Update = new JButton("Update Data");
        Display = new JButton("Display Data");
        Exit = new JButton("Exit");
        //setBounds
        Add.addActionListener(this);
        Delete.addActionListener(this);
        Update.addActionListener(this);
        Display.addActionListener(this);
        Exit.addActionListener(this);
        //button added
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Add Data")){
            this.setVisible(false); //the first frame is still open
            new add().setVisible(true); //this one is work
        }
        else if(e.getActionCommand().equals("Delete")){}
        else if(e.getActionCommand().equals("Update")){}
        else if(e.getActionCommand().equals("Display")){}
        else if(e.getActionCommand().equals("Exit")){
            System.exit(0);
        }
    }
    public static void main(String args[]) { //Launch the Application
        new Test();
    }
}

第二帧

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class add extends JFrame{
    JButton button, button2;
    JTextField tf1, tf2, tf3, tf4, tf5;
    JLabel l1, l2, l3, l4, l5;
    Connection conn = null;
    Statement stmt = null;
    public add() {
            JFrame frm = new JFrame();
            frm.setVisible(true);
            frm.setSize(500, 500);
            frm.setLayout(null);
            frm.setTitle("JDBC");
            button = new JButton("Insert");
            button = new JButton("Back");
            l1 = new JLabel("ID : ");
            l2 = new JLabel("Name : ");
            l3 = new JLabel("Adress : ");
            l4 = new JLabel("Gender : ");
            l5 = new JLabel("IP : ");       
            tf1 = new JTextField();
            tf2 = new JTextField();
            tf3 = new JTextField();
            tf4 = new JTextField();
            tf5 = new JTextField();
            //setBounds
            //frm added
    }
        public void actionPerformed(ActionEvent e) {
            if(e.getActionCommand().equals("Insert")){
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection("jdbc:mysql://localhost/academic", "root", "abc"); 
                    stmt = conn.createStatement();
                    String sql;
                    sql = "INSERT INTO student VALUES(" +
                          "'" + tf1.getText() + "'," + 
                          "'" + tf2.getText() + "'," +
                          "'" + tf3.getText() + "'," +
                          "'" + tf4.getText() + "'," +
                          tf5.getText() + ")";
                    stmt.executeUpdate(sql);
                    stmt.close();
                    conn.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }catch(Exception es){
                    es.printStackTrace();
                }
            }
            else if(e.getActionCommand().equals("Back")){
                this.setVisible(false);
                Test().setVisible(true);//Test() cannot finded on cmd 
            }
        }
}

任何想法的人?请帮助我

2 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一目标。一种方法是将两个框架单片并用它们的实例进行操作。

第一帧:

public class First {

    public static First mInstance;
    private JFrame frame;


    public static First getInstance() {
        if(mInstance == null) 
            mInstance = new First();
        return mInstance;
    }

    private First() {
        frame = new JFrame("First");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton back = new JButton("SWITCH");
        frame.add(back);

        frame.pack();

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        back.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                frame.setVisible(false);
                Second.getInstance().getFrameInstance().setVisible(true);
            }
        });
    }

    public JFrame getFrameInstance() {
        return frame;
    }

}

第二帧:

public class Second {

    public static Second mInstance;
    private JFrame frame;


    public static Second getInstance() {
        if(mInstance == null) 
            mInstance = new Second();
        return mInstance;
    }

    private Second() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton back = new JButton("SWITCH");
        frame.add(back);

        frame.pack();
        frame.setLocationRelativeTo(First.getInstance().getFrameInstance());
        frame.setVisible(true);

        back.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                frame.setVisible(false);
                First.getInstance().getFrameInstance().setVisible(true);
            }
        });
    }


    public JFrame getFrameInstance() {
        return frame;
    }
}

答案 1 :(得分:0)

将此示例代码添加到第二个JFrame的后退按钮单击事件监听器中

new Main().setVisible(true);
this.dispose();

newMain()中是第一个JFrame的对象。