gpt4 book ai didi

java - 将值从 jframe 传递到另一个 jframe

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:56 25 4
gpt4 key购买 nike

我有两个 jframe,并且想要将 jpanel 中的值从第一个 jframe 传递到第二个 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);
}

}

所以我希望当用户单击“销售”按钮时,我们获取 id 标签的值并将其放入第二个 jframe 中的 sql 查询中:

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

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

最佳答案

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

 class Frame1 extends JFrame {
private JLabel idLabel;
public Frame1() {
.........
gotoSecondFrame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idLabel.getText());// just get id value from jlabel
JFrame2 frame = new JFram2(id);// then pass to second jframe
frame.setVisible(true);// show second jframe
}
});
........
}
}

class Frame2 extends JFrame {
private void query(int id) {
.......
}
public Frame2(int id) {
.......
query(id);// your query with id which passed from first jframe
.......
}
}

关于java - 将值从 jframe 传递到另一个 jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36221314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com