我有兩個jframes,並希望從jpanel從第一個jframe傳遞值到第二個jframe中的一個sql查詢。從jframe傳遞值到另一個
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標籤的價值,並把它放在一個sql查詢在第二JFrame中:
String = "select * from sales where cusotmerid='"+id+"'";
並執行它並在點擊按鈕銷售時自動顯示錶格。
Java的圖形用戶界面必須在DI加工不同的操作系統',屏幕大小,屏幕分辨率等使用不同的地區不同的PLAF。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –