gpt4 book ai didi

java - java中如何链接多个框架

转载 作者:行者123 更新时间:2023-11-29 15:58:49 25 4
gpt4 key购买 nike

我创建了分开的登录帧和注册帧,它们都连接到 MySQL。如果我只运行注册框架,我可以将新用户插入数据库。但是,如果我运行内部包含注册框架的登录框架并按注册,则即使填写了文本字段,在注册框架中添加的按钮也不起作用。

//this is Login frame class

package YASAR;

import java.awt.BorderLayout;
import java.sql.*;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GirisEkrani extends JFrame {
static JButton btnKullancEkle = new JButton("Kullan\u0131c\u0131 Ekle");
public static JPanel contentPane;
private static JTextField textField;
private static JComboBox comboBox = new JComboBox();
private static JButton btnBalan = new JButton("Ba\u011Flan");
private static JLabel lblParola = new JLabel("Parola :");
private static JLabel lblXaampeBalanlamad = new JLabel("Xaampe Ba\u011Flan\u0131lamad\u0131");
//private static KullaniciEkle ke=new KullaniciEkle();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
lblXaampeBalanlamad.setVisible(false);
try {

GirisEkrani frame = new GirisEkrani();
frame.setVisible(true);

try {
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yasar","ozkan","******");

btnKullancEkle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
KullaniciEkle ke=new KullaniciEkle();
ke.setVisible(true);



}
});


btnBalan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);

deneme abc=new deneme();
abc.setVisible(true);

}
});


con.close();
}

catch(SQLException e) {


textField.setEditable(false);
btnKullancEkle.setEnabled(false);
comboBox.setEnabled(false);
btnBalan.setEnabled(false);
lblParola.setVisible(false);
lblXaampeBalanlamad.setVisible(true);


e.printStackTrace();
}

} catch (Exception e) {
e.printStackTrace();
}
}
});

}

/**
* Create the frame.
*/
public GirisEkrani() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 286, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();
textField.setBounds(74, 129, 124, 19);
contentPane.add(textField);
textField.setColumns(10);


comboBox.setBounds(74, 42, 124, 21);
contentPane.add(comboBox);


btnBalan.setBounds(74, 184, 124, 33);
contentPane.add(btnBalan);

btnKullancEkle.setBounds(74, 227, 124, 26);
contentPane.add(btnKullancEkle);


lblParola.setBounds(10, 132, 45, 13);
contentPane.add(lblParola);
lblXaampeBalanlamad.setBounds(74, 90, 162, 29);

contentPane.add(lblXaampeBalanlamad);
}

}
// And this is register frame class where I add user 

package YASAR;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.sql.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class KullaniciEkle extends JFrame {

public static JPanel contentPane;
public static JTextField textField;
public static JTextField textField_1;
public static JTextField textField_2;
public static JLabel lblIsim;
public static JLabel lblSoyisim;
public static JButton btnNewButton = new JButton("Ekle");

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



try {
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yasar","ozkan","******");

btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

String input="INSERT INTO kullanicilar (TC,name,surname) VALUES(?,?,?)";
try {


PreparedStatement ps=con.prepareStatement(input);

ps.setInt(1, Integer.parseInt(textField.getText()));
ps.setString(2, textField_1.getText());
ps.setString(3, textField_2.getText());


//ps.execute();
if(ps.executeUpdate()>0) {
JOptionPane.showMessageDialog(null, "user added","user add",JOptionPane.OK_CANCEL_OPTION);
}


}catch(SQLException f) {
f.printStackTrace();
}

finally {System.out.println("pressed");}

frame.setVisible(false);
new GirisEkrani().setVisible(true);

}
});

}
catch(SQLException e) {
e.printStackTrace();
}


} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public KullaniciEkle() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 285, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();
textField.setBounds(87, 67, 96, 19);
contentPane.add(textField);
textField.setColumns(10);

textField_1 = new JTextField();
textField_1.setBounds(87, 112, 96, 19);
contentPane.add(textField_1);
textField_1.setColumns(10);

textField_2 = new JTextField();
textField_2.setBounds(87, 160, 96, 19);
contentPane.add(textField_2);
textField_2.setColumns(10);
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 10));



btnNewButton.setBounds(93, 232, 85, 21);
contentPane.add(btnNewButton);

JLabel lblTc = new JLabel("TC");
lblTc.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblTc.setBounds(10, 70, 45, 13);
contentPane.add(lblTc);

lblIsim = new JLabel("\u0130sim");
lblIsim.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblIsim.setBounds(10, 115, 45, 13);
contentPane.add(lblIsim);

lblSoyisim = new JLabel("Soyisim");
lblSoyisim.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblSoyisim.setBounds(10, 163, 45, 13);
contentPane.add(lblSoyisim);
}

}


最佳答案

您需要将此代码放入您的构造函数中。

btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

String input="INSERT INTO kullanicilar (TC,name,surname) VALUES(?,?,?)";
try {


PreparedStatement ps=con.prepareStatement(input);

ps.setInt(1, Integer.parseInt(textField.getText()));
ps.setString(2, textField_1.getText());
ps.setString(3, textField_2.getText());


//ps.execute();
if(ps.executeUpdate()>0) {
JOptionPane.showMessageDialog(null, "user added","user add",JOptionPane.OK_CANCEL_OPTION);
}


}catch(SQLException f) {
f.printStackTrace();
}

finally {System.out.println("pressed");}

frame.setVisible(false);
new GirisEkrani().setVisible(true);

}
});

最后,它看起来像这样,

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.sql.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class KullaniciEkle extends JFrame {

public static JPanel contentPane;
public static JTextField textField;
public static JTextField textField_1;
public static JTextField textField_2;
public static JLabel lblIsim;
public static JLabel lblSoyisim;
public static JButton btnNewButton = new JButton("Ekle");

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

/**
* Create the frame.
*/
public KullaniciEkle() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 285, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();
textField.setBounds(87, 67, 96, 19);
contentPane.add(textField);
textField.setColumns(10);

textField_1 = new JTextField();
textField_1.setBounds(87, 112, 96, 19);
contentPane.add(textField_1);
textField_1.setColumns(10);

textField_2 = new JTextField();
textField_2.setBounds(87, 160, 96, 19);
contentPane.add(textField_2);
textField_2.setColumns(10);
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 10));

btnNewButton.setBounds(93, 232, 85, 21);
contentPane.add(btnNewButton);

JLabel lblTc = new JLabel("TC");
lblTc.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblTc.setBounds(10, 70, 45, 13);
contentPane.add(lblTc);

lblIsim = new JLabel("\u0130sim");
lblIsim.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblIsim.setBounds(10, 115, 45, 13);
contentPane.add(lblIsim);

lblSoyisim = new JLabel("Soyisim");
lblSoyisim.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblSoyisim.setBounds(10, 163, 45, 13);
contentPane.add(lblSoyisim);

btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButtonActionPerformed(e);
}
});

}

private void btnNewButtonActionPerformed(ActionEvent evt) {
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yasar", "ozkan", "******");

String input = "INSERT INTO kullanicilar (TC,name,surname) VALUES(?,?,?)";
try {

PreparedStatement ps = con.prepareStatement(input);

ps.setInt(1, Integer.parseInt(textField.getText()));
ps.setString(2, textField_1.getText());
ps.setString(3, textField_2.getText());

//ps.execute();
if (ps.executeUpdate() > 0) {
JOptionPane.showMessageDialog(null, "user added", "user add", JOptionPane.OK_CANCEL_OPTION);
}

} catch (SQLException f) {
f.printStackTrace();
} finally {
System.out.println("pressed");
}

//Instead of frame use this keyword
this.setVisible(false);
new GirisEkrani().setVisible(true);

} catch (SQLException e) {
e.printStackTrace();
}
}
}

注意:-这不是与数据库通信的最佳实践。

关于java - java中如何链接多个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56323502/

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