【问题标题】:how to make back button work from the second frame to the first frame如何使后退按钮从第二帧工作到第一帧
【发布时间】:2016-03-12 16:32:18
【问题描述】:

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

第一帧

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 
            }
        }
}

有什么想法吗?请帮帮我

【问题讨论】:

  • 为什么不使用card layout
  • 我不知道,还有其他方法吗?喜欢我的吗?
  • 它允许您拥有一个包含可交换内容的容器,我认为您正在尝试使用两个容器来实现这一点。稍微简化了解决方案,而且您还可以获得使用布局管理器的额外好处,而不必重新发明轮子。
  • 你能给我举个例子吗?还是直接在我的代码中使用?请:)
  • 我在上面链接你的那个页面底部有一个例子。

标签: java swing jdbc jframe


【解决方案1】:

有很多方法可以实现这一点。一种是将两个框架都设为单例并使用它们的实例进行操作。

第一帧:

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;
    }
}

【讨论】:

  • 首先,这不是您在Java中制作单例的方式,其次,这大大减少了类的重用,更好(更通用的解决方案),可能是使用MVC的一种形式,其中控制器在堆栈上“推入”/“弹出”视图,并(通过模型)确定可以做什么/不可以做什么
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2022-08-17
    相关资源
    最近更新 更多