将值从jframe传递到另一个

时间:2016-03-25 13:54:24

标签: java swing jlabel

我有两个jframe,并希望将jpanel中的值从第一个jframe传递到sql jframe中的sql查询。

package gestioner;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;

import net.proteanit.sql.DbUtils;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Customers extends JFrame {

private JPanel contentPane;
private JLabel lblFullName;
private JTextField funame;
private JLabel lblEmail;
private JTextField email;
private JLabel lblAdresse;
private JTextField adresse;
private JLabel lblTel;
private JTextField tel;
private JButton button;
private JButton button_1;
private JButton button_2;
private JButton btnShowAll;
private JLabel label_4;
private JTextField search;
private JTable table;
private JScrollPane scrollPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Customers frame = new Customers();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
public JLabel id;
private JButton btnSales;

public String getLab() {
    // String z =funame.getText();
    return funame.getText();
}

public void fillTable() {

    try {
        String query = "select * from customers";
        pst = con.prepareStatement(query);
        rs = pst.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));

        // center allignment
        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                .setHorizontalAlignment((int) JLabel.CENTER_ALIGNMENT);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
        for (int x = 0; x < table.getColumnCount(); x++) {
            table.getColumnModel().getColumn(x).setCellRenderer(centerRenderer);
        }

    } catch (Exception ex) {

        JOptionPane.showMessageDialog(this, ex.getMessage());
    }

}

public Customers() {

    con = Connectdb.connect();

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 867, 492);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    lblFullName = new JLabel("Full Name");
    lblFullName.setBounds(20, 92, 78, 14);
    contentPane.add(lblFullName);

    funame = new JTextField();
    funame.setColumns(10);
    funame.setBounds(116, 89, 125, 20);
    contentPane.add(funame);

    lblEmail = new JLabel("Email");
    lblEmail.setBounds(20, 120, 78, 14);
    contentPane.add(lblEmail);

    email = new JTextField();
    email.setColumns(10);
    email.setBounds(116, 117, 125, 20);
    contentPane.add(email);

    lblAdresse = new JLabel("Adresse");
    lblAdresse.setBounds(20, 148, 78, 14);
    contentPane.add(lblAdresse);

    adresse = new JTextField();
    adresse.setColumns(10);
    adresse.setBounds(116, 145, 125, 20);
    contentPane.add(adresse);

    lblTel = new JLabel("Tel");
    lblTel.setBounds(20, 176, 78, 14);
    contentPane.add(lblTel);

    tel = new JTextField();
    tel.setColumns(10);
    tel.setBounds(116, 173, 125, 20);
    contentPane.add(tel);

    button = new JButton("Add");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {

                String query = "Insert into customers (fullname,email,adress,tele) values (?,?,?,?)";
                pst = con.prepareStatement(query);
                pst.setString(1, funame.getText());
                pst.setString(2, email.getText());
                pst.setString(3, adresse.getText());
                pst.setString(4, tel.getText());
                pst.execute();
                fillTable();
                funame.setText("");
                email.setText("");
                adresse.setText("");
                tel.setText("");
                id.setText("");

            } catch (Exception ex) {

                JOptionPane.showMessageDialog(null, ex.getMessage());
            }

        }
    });
    button.setBounds(20, 213, 89, 23);
    contentPane.add(button);

    button_1 = new JButton("Edit");
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {

                String query = "update customers set fullname= '" + funame.getText() + "' , email= '"
                        + email.getText() + "' , adress= '" + adresse.getText() + "', tele= '" + tel.getText()
                        + "' where id= '" + id.getText() + "'  ";
                pst = con.prepareStatement(query);

                // pst.setString(1, funame.getText());
                // pst.setString(2, email.getText());
                // pst.setString(3, adresse.getText());
                // pst.setString(4, tel.getText());
                pst.execute();
                fillTable();

                funame.setText("");
                email.setText("");
                adresse.setText("");
                tel.setText("");
                id.setText("");

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }

        }
    });
    button_1.setBounds(152, 213, 89, 23);
    contentPane.add(button_1);

    button_2 = new JButton("Delete");
    button_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {

                String query = "delete from customers where id='" + id.getText() + "'";
                pst = con.prepareStatement(query);
                pst.execute();
                fillTable();
                funame.setText("");
                adresse.setText("");
                tel.setText("");
                email.setText("");
                id.setText("");

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }

        }
    });
    button_2.setBounds(20, 247, 89, 23);
    contentPane.add(button_2);

    btnShowAll = new JButton("Show All");
    btnShowAll.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {

                String query = "select * from customers";
                pst = con.prepareStatement(query);
                rs = pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));

                ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                        .setHorizontalAlignment((int) JLabel.CENTER_ALIGNMENT);
                DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
                centerRenderer.setHorizontalAlignment(JLabel.CENTER);
                table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
                for (int x = 0; x < table.getColumnCount(); x++) {
                    table.getColumnModel().getColumn(x).setCellRenderer(centerRenderer);
                }

                pst.close();
                rs.close();

            } catch (Exception ex) {

                JOptionPane.showMessageDialog(null, ex.getMessage());

            }

        }
    });
    btnShowAll.setBounds(733, 386, 89, 23);
    contentPane.add(btnShowAll);

    label_4 = new JLabel("Search");
    label_4.setBounds(647, 35, 61, 14);
    contentPane.add(label_4);

    search = new JTextField();
    search.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent arg0) {

            try {

                if (search.getText().equals("")) {
                    fillTable();
                } else {
                    String query = "select * from customers where id like ? or fullname like ? or tele like ? ";
                    pst = con.prepareStatement(query);
                    pst.setString(1, search.getText() + "%");
                    pst.setString(2, search.getText() + "%");
                    pst.setString(3, search.getText() + "%");
                    rs = pst.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }

        }
    });
    search.setColumns(10);
    search.setBounds(699, 32, 123, 20);
    contentPane.add(search);

    scrollPane = new JScrollPane();
    scrollPane.setBounds(271, 62, 551, 309);
    contentPane.add(scrollPane);

    table = new JTable();
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {

            try {

                int row = table.getSelectedRow();
                String table_click = table.getModel().getValueAt(row, 0).toString();
                String query = "select * from customers where id= '" + table_click + "'";
                pst = con.prepareStatement(query);
                rs = pst.executeQuery();

                if (rs.next()) {
                    String add1 = rs.getString("id");
                    id.setText(add1);
                    String add2 = rs.getString("fullname");
                    funame.setText(add2);
                    String add3 = rs.getString("email");
                    email.setText(add3);
                    String add4 = rs.getString("adress");
                    adresse.setText(add4);
                    String add5 = rs.getString("tele");
                    tel.setText(add5);

                }

            } catch (Exception ex) {

                JOptionPane.showMessageDialog(null, ex.getMessage());
            }

        }
    });
    scrollPane.setViewportView(table);

    id = new JLabel("");
    id.setBounds(116, 64, 125, 14);
    contentPane.add(id);

    btnSales = new JButton("Sales");
    btnSales.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            new Showsales().setVisible(true);

        }
    });
    btnSales.setBounds(152, 247, 89, 23);
    contentPane.add(btnSales);
}

}

第二个jframe

package gestioner;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;

import net.proteanit.sql.DbUtils;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class Showsales extends JFrame {

private JPanel contentPane;
private JTable table;
private JScrollPane scrollPane;
private JButton btnShowAll;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Showsales frame = new Showsales();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
private JLabel lblSearch;
private JTextField search;

public void fillTable() {

    try {

        String query = "select * from sales where customerid=";
        pst = con.prepareStatement(query);
        rs = pst.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                .setHorizontalAlignment((int) JLabel.CENTER_ALIGNMENT);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
        for (int x = 0; x < table.getColumnCount(); x++) {
            table.getColumnModel().getColumn(x).setCellRenderer(centerRenderer);
        }

        pst.close();
        rs.close();

    } catch (Exception ex) {

        JOptionPane.showMessageDialog(null, ex.getMessage());

    }

}

public Showsales() {

    con = Connectdb.connect();

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 868, 495);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    scrollPane = new JScrollPane();
    scrollPane.setBounds(232, 40, 610, 363);
    contentPane.add(scrollPane);

    table = new JTable();
    scrollPane.setViewportView(table);

    btnShowAll = new JButton("Show All");
    btnShowAll.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {

                String query = "select * from sales";
                pst = con.prepareStatement(query);
                rs = pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));

                ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                        .setHorizontalAlignment((int) JLabel.CENTER_ALIGNMENT);
                DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
                centerRenderer.setHorizontalAlignment(JLabel.CENTER);
                table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
                for (int x = 0; x < table.getColumnCount(); x++) {
                    table.getColumnModel().getColumn(x).setCellRenderer(centerRenderer);
                }

                pst.close();
                rs.close();

            } catch (Exception ex) {

                JOptionPane.showMessageDialog(null, ex.getMessage());

            }

        }
    });
    btnShowAll.setBounds(717, 423, 125, 23);
    contentPane.add(btnShowAll);

    lblSearch = new JLabel("Search");
    lblSearch.setBounds(609, 15, 65, 14);
    contentPane.add(lblSearch);

    search = new JTextField();
    search.setBounds(684, 12, 158, 20);
    contentPane.add(search);
    search.setColumns(10);
}

}

所以我想当用户点击按钮sales时,我们取id标签的值并将其放在第二个jframe的sql查询中:

String = "select * from sales where cusotmerid='"+id+"'";

并执行它并在点击按钮销售时自动显示表格。

2 个答案:

答案 0 :(得分:0)

你有两个申请吗?我在示例代码中看到了两个主要函数,那么您是否希望将第一个应用程序中第一个jframe的值传递给另一个应用程序中的第二个jframe? 如果您只想将值从jframe传递到应用程序中的其他值,则可以这样做:

public function updateSalary($emp_id, $salary) {
    try {    
        // Note that any of the following statements could trigger exceptions:
        $employee = $this->$model->getEmployee($emp_id);
        $employee->setSalary($salary);
        $employee->save();
    } catch(Exception $e) {
        $this->$model->logError('salary', $e->getMessage());
    }
}

答案 1 :(得分:0)

您可以使用SecondJFrame的构造函数将值从FirstJFrame传递到SecondJFrame

FirstJFrame.java

public class FirstJFrame extends javax.swing.JFrame {

    JButton btn = new JButton("Send Value");

    public FirstJFrame() {

         getContentPane().add(btn);

    }
    private void btnActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        SecondJFrame secondFrame = new SecondJFrame(13);
// 13 is the value 

       secondFrame.setVisible(true);

    }
}

SecondJFrame.java

public class SecondJFrame extends javax.swing.JFrame {

    int id = 0;

    public SecondJFrame (int idValue) {

          int id = idValue;

          System.out.println(id);
    }

}