- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在java中进行了以下登录,它有2个Jframe,登录JFrame和另一个我称为Main的JFrame。我想做的是:当我成功登录时,我想从我用来登录的用户名的第二个形式解析到我的标签。
因此,如果我使用 eddye 登录,我希望将“eddye”解析为我的第二种形式
第一个 JFrame 的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mysqltryouts;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author ExtremeSwat
*/
public class login_panel extends javax.swing.JFrame {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception
{
String username = jTextField1.getText();
String password = jPasswordField1.getText();
// System.out.println(username);
// System.out.println(password);
// System.out.println("test");
//---------------------
// String databaseUsername = "";
// String databasePassword = "";
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/cards?"
+ "user=root&password=password");
//String sqlQuery = "select count(*) > 0 as match_found FROM username WHERE username = ? and password = MD5(?)";
String sqlQuery = "SELECT * FROM username WHERE username = ? and password = MD5(?)";
// String sqlQuery = "select count(*) > 0 as cnt FROM username WHERE username = ? and password = MD5(?)";
PreparedStatement pst = connect.prepareStatement( sqlQuery );
pst.setString( 1, username );
pst.setString( 2, password );
ResultSet rs = pst.executeQuery();
if( rs.next() )
{
JOptionPane.showMessageDialog(null, "Succesfully Logged In", "Success", JOptionPane.INFORMATION_MESSAGE);
// int width = this.getWidth();
// int height = this.getHeight();
//
// System.out.println(width);
// System.out.print(height);
this.setVisible(false);
new FrmMain().setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Failed to log in", "Failure", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void close() {
close(resultSet);
close(statement);
close(connect);
}
private void close(AutoCloseable c) throws UnsupportedOperationException {
try {
if (c != null) {
c.close();
}
} catch (Exception e) {
// don't throw now as it might leave following closables in undefined state
}
}
/**
* Creates new form login_panel
*/
public login_panel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
jLabel1.setText("Username");
jLabel2.setText("Password");
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("eXIT");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Old English Text MT", 1, 36)); // NOI18N
jLabel3.setText("Login B0$$");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(66, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(53, 53, 53))))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1))
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3)
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
.addComponent(jButton2))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
// login_panel dao = new login_panel();
//
//
// dao.readDataBase();
this.readDataBase();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "Exit? ", "Information",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Thanks for the stay","Confirm",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}else
{
JOptionPane.showMessageDialog(null, "Remaining...","Remaining....",JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//new login_panel().setVisible(true);
login_panel frame = new login_panel();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
第二个 JFrame 的代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mysqltryouts;
import java.awt.Dimension;
import java.awt.Toolkit;
/**
*
* @author ExtremeSwat
*/
public class FrmMain extends javax.swing.JFrame {
/**
* Creates new form FrmMain
*/
public FrmMain() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Back");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(297, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 231, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// int width = this.getWidth();
// int height = this.getHeight();
//
// System.out.println(width);
// System.out.print(height);
this.setVisible(false);
new login_panel().setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new FrmMain().setVisible(true);
FrmMain frame = new FrmMain();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
我真的需要一些帮助来解决这个问题。我想到的是 VB.NET 的代码
将用户名变暗为 String = "abc"Form2.label1.text = 用户名;
最佳答案
如果您的用户成功登录,您可以调用第二个 JFrame 的构造函数,您可以这样做
new FrmMain(username).setVisible(true);
并且您必须添加另一个字段,例如 private String userName;
更改 FrmMain()
类的构造函数
// ...
private String userName;
public FrmMain(String username) {
this.userName = username;
initComponents();
}
// ...
并在 initComponents() 方法中将 userName 设置为 jLabel
jLabel1.setText(userName);
就是这样。使用多个 JFrame 并不是最好的解决方案,也许您可以阅读一些有关 MVC 的文章并在一个 JFrame
中工作,并且仅更改 JPanels
!
更新:
这是您的两个类(class),我按照我的建议更改了它们。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author ExtremeSwat
*/
public class LoginPanel extends javax.swing.JFrame {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception
{
String username = jTextField1.getText();
String password = jPasswordField1.getText();
// System.out.println(username);
// System.out.println(password);
// System.out.println("test");
//---------------------
// String databaseUsername = "";
// String databasePassword = "";
/*
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/cards?"
+ "user=root&password=password");
//String sqlQuery = "select count(*) > 0 as match_found FROM username WHERE username = ? and password = MD5(?)";
String sqlQuery = "SELECT * FROM username WHERE username = ? and password = MD5(?)";
// String sqlQuery = "select count(*) > 0 as cnt FROM username WHERE username = ? and password = MD5(?)";
PreparedStatement pst = connect.prepareStatement( sqlQuery );
pst.setString( 1, username );
pst.setString( 2, password );
ResultSet rs = pst.executeQuery();
*/
if( username.equalsIgnoreCase("patrick") && password.equalsIgnoreCase("1234") )
{
JOptionPane.showMessageDialog(null, "Succesfully Logged In", "Success", JOptionPane.INFORMATION_MESSAGE);
// int width = this.getWidth();
// int height = this.getHeight();
//
// System.out.println(width);
// System.out.print(height);
this.setVisible(false);
new FrmMain(username).setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Failed to log in", "Failure", JOptionPane.WARNING_MESSAGE);
}
/*
} catch (Exception e) {
throw e;
} finally {
close();
}
*/
}
private void close() {
close(resultSet);
close(statement);
close(connect);
}
private void close(AutoCloseable c) throws UnsupportedOperationException {
try {
if (c != null) {
c.close();
}
} catch (Exception e) {
// don't throw now as it might leave following closables in undefined state
}
}
/**
* Creates new form login_panel
*/
public LoginPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
jLabel1.setText("Username");
jLabel2.setText("Password");
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("eXIT");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Old English Text MT", 1, 36)); // NOI18N
jLabel3.setText("Login B0$$");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(66, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(53, 53, 53))))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1))
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3)
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
.addComponent(jButton2))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
// LoginPanel dao = new LoginPanel();
//
//
// dao.readDataBase();
this.readDataBase();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "Exit? ", "Information",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Thanks for the stay","Confirm",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}else
{
JOptionPane.showMessageDialog(null, "Remaining...","Remaining....",JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LoginPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoginPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoginPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoginPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//new LoginPanel().setVisible(true);
LoginPanel frame = new LoginPanel();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
和 FrmMain()
import java.awt.Dimension;
import java.awt.Toolkit;
/**
*
* @author ExtremeSwat
*/
public class FrmMain extends javax.swing.JFrame {
private String userName;
/**
* Creates new form FrmMain
*/
public FrmMain(final String userName) {
this.userName = userName;
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel1.setText(userName);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Back");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(297, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 231, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// int width = this.getWidth();
// int height = this.getHeight();
//
// System.out.println(width);
// System.out.print(height);
this.setVisible(false);
new LoginPanel().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
我必须评论数据库部分,我将其替换为字符串等于检查,因此复制并粘贴这两个类并使用 Patrick/1234 进行测试,如果您已登录,您应该在新 JFrame 上看到 Patrick 作为用户名成功了。
关于java - 将值从文本框传递到另一个 JFrame 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23300485/
我正在尝试将 JFrame (bar) 添加到 JFrame (foo),这会强制 bar 在之前关闭foo 可以再次集成。 例如,在我可以再次编写代码之前,必须关闭“About Eclipse”框架
我有一个名为 User 的 JFrame,在其中声明一个名为 id 的变量,并根据某些条件将其设置为特定值。 我需要在称为输出的第二个 JFrame 中使用此变量。 这是我的代码 class Inpu
我有这个问题:我有一个 ArrayList,其中包含一些我想在特定框架中使用的项目,问题是数组列表通过在主类中初始化而变满我的项目。在本类(class)中,我还启动了与其他框架链接的起始框架(登录框架
如何在隐藏一个 Swing 程序的同时将一个 javax.Swing 程序移动到另一个 Swing 程序,以及如何制作可滚动的 JFrame。请帮助我。 最佳答案 在这里你可以找到一个非常good e
所以基本上我有一个 2D 游戏(基本上是过去的仿制品的链接),当你按 e 键时,库存会打开,关闭时会隐藏。问题是,每次我按 e 键时,它都会打开一个包含所有初始值的新库存,我希望它在初始值之后打开一个
我有一个 JFrame(1,主框架),它延续了 JPanel 和 JTable 以及数据。用户可以编辑数据。因此,一个新的 JFrame (2) 打开,用户可以输入新数据。如果他们在第二帧中单击“确定
我有一个新的netbeans“Java应用程序”项目,我试图从主JFrame添加第二个JFrame,用户可以从中加载文件。 所以我有主要的 JFrame main 方法 public static v
我的程序应该在单击按钮时启动第二个 JFrame 并打印一条语句,但它总是启动三个 JFrame 并打印三个语句。我只需要它来打印出一份声明并启动一个 Jframe。这是代码: import java
我目前已经构建了一个使用多个框架的应用程序。但如果我能在 1 帧中使用所有我用过的帧,那就太好了。如下图所示。 因此,如果您按左侧按钮“Speler Overzicht”,它将在右侧面板中向用户显示,
我目前有一个按钮,单击该按钮时,会执行一种方法,该方法创建一个带有加载多个图像的面板的 jframe。如果多次单击此按钮,图像将继续添加到加载到 jframe 上的现有图像上。我应该使用什么代码,以便
为什么无法将JFrame添加到JFrame中?它只是将一个组件添加到容器中。 Java 如何禁止我这样做?是的,我知道这样做没有意义,但我的问题的重点是了解 Swing 机制 - 它在幕后是如何工作的
我创建了一个生成 StartUpWindow 对象的类。其中一个 JButton 组件关闭 JFrame,然后调用一种新类型的框架进行实例化,AdminMainWindow。但是,当 AdminMai
我试图在不显示 JFrame 本身的情况下将 JFrame 渲染为图像(类似于 this 问题所问的内容)。我试过使用这段代码: private static BufferedImage getScr
我正在使用 XFCE 4 桌面的 Debian 8 上编译并运行以下代码。 import javax.swing.JFrame; import javax.swing.JComponent; impo
我有 14 个 JFrame 类和一个 JFrame 类,其中包含一个充当日历的 JTable。我希望当用户从 JTable 日历中选择日期时,更改 14 个 JFrame 类中任何一个文本字段或任何
我有 3 个扩展 JFrame 的对象让我们调用他们 FrameA FrameB FrameC . FrameA是我的主应用程序窗口。来自 FrameA的构造函数如果应用程序未注册我创建 FrameB
我试图简单地创建四个 jtextfields 和一个 jbutton。按下按钮后,我希望输入到 jtextfields 的文本作为参数(p、var、s、f)传递到另一个窗口,该窗口使用给定的参数显示数
这个问题已经有答案了: Close one JFrame without closing another? (2 个回答) 已关闭 6 年前。 我制作了一个程序,其中存在三个jframe。但问题是,当
我正在用 java 构建一个桌面学生管理应用程序。所以我需要关闭当前的 JFRAME,同时另一个 JFRAME 可见(关闭新的 jframe 后,旧的 jframe 应该可见)。你能帮我一下吗? 最佳
我正在开发的应用程序包含一个主 JFrame,用户最终可能会从中打开另一个补充框架。我正在尝试实现应用程序的这种行为,其中一旦主框架最小化,补充框架就会最小化(图标化)。 我正在考虑重写主框架的 se
我是一名优秀的程序员,十分优秀!