Java 不在类之间传递的变量
我已经放弃了旧的问题,因为它是不清楚的,有很多错误,我找到了一个更简单的“我认为”的解决方案,虽然不完全有效。 修订过的:::::::: LognL类Java 不在类之间传递的变量,java,class,methods,user-input,Java,Class,Methods,User Input,我已经放弃了旧的问题,因为它是不清楚的,有很多错误,我找到了一个更简单的“我认为”的解决方案,虽然不完全有效。 修订过的:::::::: LognL类 public final class LogInL extends JFrame { Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; String Username; String ID; public LogInL() { initCo
public final class LogInL extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
String Username;
String ID;
public LogInL() {
initComponents();
ButtonGroup();
}
private void backBActionPerformed(ActionEvent e) {
LoginMain login = new LoginMain();
login.setVisible(true);
this.dispose();
}
private void LoginBActionPerformed(ActionEvent e) {
if(prosecutorCB.isSelected())
{
try {
conn = SQLConnect.ConnectDb();
String sql = "SELECT prosecutors.username, criminalrecords.ID FROM prosecutors, criminalrecords "
+ "WHERE username = ? and ID = ?";
pst = conn.prepareStatement(sql);
pst.setString(1, usernameF.getText());
pst.setString(2, criminalIdF.getText());
Username = usernameF.getText();
System.out.println("Username is " + Username);
ID = criminalIdF.getText();
System.out.println("Criminal ID is " + ID);
rs = pst.executeQuery();
if(rs.next())
{
ProsecutorMain Frame = new ProsecutorMain(); // call Policemain class //display it
Frame.pack();
Frame.setVisible(true); // make it visible
System.out.println("Welcome to prosecutors");
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"<html>wrong username or criminal ID<br>"
+ "Criminal may not longer be in database");
}
}
catch (SQLException ex)
{
Logger.getLogger(LogInL.class.getName()).log(Level.SEVERE, null, ex);
}
}//end if proseutor
}//end logination
private void ButtonGroup()
{
ButtonGroup bg = new ButtonGroup();
bg.add(prosecutorCB);
bg.add(criminaldefenceCB);
}
public final类LogInL扩展了JFrame{
连接conn=null;
结果集rs=null;
PreparedStatement pst=null;
字符串用户名;
字符串ID;
公共登录{
初始化组件();
按钮组();
}
已执行私有操作(操作事件e){
LoginMain login=新建LoginMain();
login.setVisible(true);
这个。dispose();
}
私有无效登录ActionPerformed(ActionEvent e){
if(起诉人或起诉人B.isSelected())
{
试一试{
conn=SQLConnect.ConnectDb();
String sql=“从检察官,刑事记录中选择检察官.username,criminalrecords.ID”
+“其中用户名=?和ID=?”;
pst=conn.prepareStatement(sql);
setString(1,usernameF.getText());
setString(2,df.getText());
Username=usernameF.getText();
System.out.println(“用户名为”+用户名);
ID=df.getText();
System.out.println(“罪犯ID为”+ID);
rs=pst.executeQuery();
如果(rs.next())
{
检控器主框架=新检控器主框架();//调用policymain类//显示它
Frame.pack();
Frame.setVisible(true);//使其可见
System.out.println(“欢迎检察官”);
这个。dispose();
}
其他的
{
JOptionPane.showMessageDialog(null,“错误的用户名或罪犯ID
”
+“罪犯可能不再在数据库中”);
}
}
catch(SQLException-ex)
{
Logger.getLogger(LogInL.class.getName()).log(Level.SEVERE,null,ex);
}
}//如果是前置执行器,则结束
}//结束登录
私有void按钮组()
{
ButtonGroup bg=新建ButtonGroup();
背景资料增补(刑事检控科);
bg.add(刑事辩护);
}
修订版:
在课堂上
public class ProsecutorMain extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
LogInL id = new LogInL();
String UserName;
public ProsecutorMain() throws SQLException
{
initComponents();
UserName = id.Username;
username.setText(UserName);
firstname.setText("blablabla");
incidentlocation.setText("kupa");
System.out.println(UserName);
}
private void logOutActionPerformed(ActionEvent e) {
int response = JOptionPane.showConfirmDialog(null, "<html> Are you sure you want to log out?",
"Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION)
{
}
if (response == JOptionPane.YES_OPTION)
{
this.dispose();
LogInL login = new LogInL();
login.setVisible(true);
JOptionPane.showMessageDialog(null,"You have been sucessfully logged out");
}
}
扩展JFrame中的公共类{
连接conn=null;
结果集rs=null;
PreparedStatement pst=null;
LogInL id=new LogInL();
字符串用户名;
公诉人在()抛出SQLException
{
初始化组件();
UserName=id.UserName;
username.setText(用户名);
firstname.setText(“blabla”);
incidentlocation.setText(“kupa”);
System.out.println(用户名);
}
已执行私有void LogoutAction(ActionEvent e){
int response=JOptionPane.showConfirmDialog(null,“您确定要注销吗?”,
“确认”,JOptionPane.YES\u NO\u选项,JOptionPane.QUESTION\u消息);
if(response==JOptionPane.NO_选项)
{
}
if(response==JOptionPane.YES\u选项)
{
这个。dispose();
LogInL login=new LogInL();
login.setVisible(true);
showMessageDialog(null,“您已成功注销”);
}
}
问题中的变量是Username当您创建
LogInL
的新实例时,变量Username
在LogInL
中是null
,而该JFrame
的原始(和显示的)实例没有数据库中的值
要修复此问题,您必须传入设置了Username
的实例:
public ProsecutorMain(LogInL id) {
this.id = id;
...
}
并在LogInL
中创建:
new ProsecutorMain(this);
当您从查询中获得结果时,您可以使用必要的
用户名
和id
字段创建一个自定义法律记录
,而不是创建一个新的检控码
。然后您可以将其传递给检控码
您可以使用模态JDialog
来接受搜索条件,而不是在此处使用2JFrames
链接:
我想帮助我重新组织代码我必须完成我的requirementHello Reimeus你能看一下修改后的问题现在谢谢你不能确定什么可能或可能不设置如果你没有太多的代码,请张贴,否则考虑发布一个问题已经修改了所有相关的代码到问题I已经按照您的建议执行了操作,但是在LogInL类中的“ColumorMain Frame=new-ColumorMain();”下出现了错误,我已将其替换为ColumorMain Frame=new-ColumorMain(LogInL id);但是仍然出现了错误。