我在netbeans中创建了两个GUI(一个登录表单,另一个用于添加数据或其他任何内容)。
问题是,在关闭登录按钮的对话框后,我不知道如何显示其他表单。
在下面我发布两帧的源代码。 第一个用于登录,第二个用于另一个帧。
package hms;
import java.awt.Color;
import javax.swing.JOptionPane;
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
public Login() {
setTitle("Login");
getContentPane().setBackground(new Color(255,255,255));
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() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
jPanel1 = new javax.swing.JPanel();
UsernameLabel = new javax.swing.JLabel();
PasswordLabel = new javax.swing.JLabel();
UsernameTextField = new javax.swing.JTextField();
PasswordField = new javax.swing.JPasswordField();
LoginButton = new javax.swing.JButton();
CancelButton = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("HMS Login");
setBounds(new java.awt.Rectangle(0, 0, 721, 410));
setIconImages(null);
setMaximizedBounds(new java.awt.Rectangle(0, 0, 721, 410));
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setMaximumSize(new java.awt.Dimension(3276, 3276));
jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
jPanel1.setName(""); // NOI18N
UsernameLabel.setText("Username:");
PasswordLabel.setText("Password:");
UsernameTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
UsernameTextFieldActionPerformed(evt);
}
});
UsernameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
UsernameTextFieldKeyTyped(evt);
}
});
PasswordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PasswordFieldActionPerformed(evt);
}
});
PasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
PasswordFieldKeyTyped(evt);
}
});
LoginButton.setBackground(new java.awt.Color(102, 255, 255));
LoginButton.setText("Login");
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, LoginButton, org.jdesktop.beansbinding.ELProperty.create("${action.enabled}"), LoginButton, org.jdesktop.beansbinding.BeanProperty.create("selected"));
bindingGroup.addBinding(binding);
LoginButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
LoginButtonMouseClicked(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
LoginButtonMouseReleased(evt);
}
});
LoginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
LoginButtonActionPerformed(evt);
}
});
CancelButton.setBackground(new java.awt.Color(102, 255, 255));
CancelButton.setText("Cancel");
CancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CancelButtonActionPerformed(evt);
}
});
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hms/login icon.jpg"))); // NOI18N
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(71, 71, 71)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(UsernameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(PasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(UsernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
.addComponent(PasswordField))
.addGap(36, 36, 36)))
.addContainerGap(49, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(151, 151, 151)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(UsernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(UsernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(30, Short.MAX_VALUE))
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
bindingGroup.bind();
setSize(new java.awt.Dimension(729, 444));
setLocationRelativeTo(null);
}// </editor-fold>
private void UsernameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void PasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void LoginButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void UsernameTextFieldKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void PasswordFieldKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void LoginButtonMouseReleased(java.awt.event.MouseEvent evt) {
JOptionPane.showMessageDialog(null, "Successfully Login");
}
private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/**
* @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.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton CancelButton;
private javax.swing.JButton LoginButton;
private javax.swing.JPasswordField PasswordField;
private javax.swing.JLabel PasswordLabel;
private javax.swing.JLabel UsernameLabel;
private javax.swing.JTextField UsernameTextField;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}
第二个 -
package hms;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;
public class APPTAB extends javax.swing.JFrame {
/**
* Creates new form APPTAB
*/
public APPTAB() {
setTitle("Hospital Management System");
getContentPane().setBackground(new Color(255,255,255));
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() {
jScrollPane1 = new javax.swing.JScrollPane();
CredentialsTable = new javax.swing.JTable();
FirstNameLabel = new javax.swing.JLabel();
FirstNameTextField = new javax.swing.JTextField();
ADDButton = new javax.swing.JButton();
LastNameLabel = new javax.swing.JLabel();
LastNameTextField = new javax.swing.JTextField();
MessageLabel = new javax.swing.JLabel();
CategoryComboBox = new javax.swing.JComboBox<>();
CategoryLabel = new javax.swing.JLabel();
IDSSNLabel = new javax.swing.JLabel();
IDSSNTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("HOSPITAL MANAGEMENT SYSTEM");
setBackground(new java.awt.Color(255, 255, 255));
setBounds(new java.awt.Rectangle(0, 0, 590, 423));
setMaximizedBounds(new java.awt.Rectangle(0, 0, 590, 423));
CredentialsTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"First Name", "Last Name", "Category", "ID / SSN"
}
));
jScrollPane1.setViewportView(CredentialsTable);
FirstNameLabel.setText("First Name:");
ADDButton.setBackground(new java.awt.Color(153, 204, 255));
ADDButton.setText("Add");
ADDButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ADDButtonActionPerformed(evt);
}
});
LastNameLabel.setText("Last Name:");
MessageLabel.setForeground(new java.awt.Color(255, 0, 0));
CategoryComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Doctor", "Patient" }));
CategoryLabel.setText("Category:");
IDSSNLabel.setText("ID / SSN:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(149, 149, 149)
.addComponent(CategoryLabel)
.addGap(52, 52, 52)
.addComponent(CategoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(149, 149, 149)
.addComponent(IDSSNLabel)
.addGap(57, 57, 57)
.addComponent(IDSSNTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(270, 270, 270)
.addComponent(ADDButton))
.addGroup(layout.createSequentialGroup()
.addGap(97, 97, 97)
.addComponent(MessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(97, 97, 97)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(149, 149, 149)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(FirstNameLabel)
.addGap(46, 46, 46)
.addComponent(FirstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(LastNameLabel)
.addGap(47, 47, 47)
.addComponent(LastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(113, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(FirstNameLabel))
.addComponent(FirstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(LastNameLabel))
.addComponent(LastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(CategoryLabel))
.addComponent(CategoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(IDSSNLabel))
.addComponent(IDSSNTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21)
.addComponent(ADDButton)
.addGap(15, 15, 15)
.addComponent(MessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(51, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ADDButtonActionPerformed(java.awt.event.ActionEvent evt) {
MessageLabel.setText("");
DefaultTableModel model = (DefaultTableModel)CredentialsTable.getModel();
if(!FirstNameTextField.getText().trim().equals("")){
model.addRow(new Object[]{FirstNameTextField.getText(),CategoryComboBox.getSelectedItem().toString(),LastNameTextField.getText(),IDSSNTextField.getText()});
}
else{
MessageLabel.setText("All fields are mandatory!");
}
}
/**
* @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(APPTAB.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(APPTAB.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(APPTAB.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(APPTAB.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new APPTAB().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton ADDButton;
private javax.swing.JComboBox<String> CategoryComboBox;
private javax.swing.JLabel CategoryLabel;
private javax.swing.JTable CredentialsTable;
private javax.swing.JLabel FirstNameLabel;
private javax.swing.JTextField FirstNameTextField;
private javax.swing.JLabel IDSSNLabel;
private javax.swing.JTextField IDSSNTextField;
private javax.swing.JLabel LastNameLabel;
private javax.swing.JTextField LastNameTextField;
private javax.swing.JLabel MessageLabel;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}