这以前工作得很好,但我把项目搁置了一段时间,当我决定重新开始时,面板没有出现在框架中。我尝试了许多不同的可能解决方案,并花了几天时间试图修复它,但没有任何效果。有谁看到可能导致框架显示为空的原因?
// imports
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class login extends JFrame {
// create variables
JFrame login = new JFrame();
private JTextField userTF;
private JPasswordField passwordField;
private JLabel userL;
private JLabel passL;
private JLabel updateOne;
private JLabel updateTwo;
private JLabel error;
private JLabel welcome;
private JButton submit;
String password = "";
String username = "";
JPanel panel1 = new JPanel(new GridBagLayout());
JPanel panel2 = new JPanel(new GridBagLayout());
JPanel panel3 = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// constructor
public login(){
// set and add all GUI components
welcome = new JLabel("PLA LOGIN");
c.gridx = 0;
c.gridy = 0;
panel1.add(welcome, c);
userL = new JLabel("Username: ");
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
panel2.add(userL, c);
userTF = new JTextField(15);
userTF.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.gray));
userTF.setOpaque(false);
c.gridx = 0;
c.gridy = 2;
panel2.add(userTF, c);
passL = new JLabel("Password: ");
c.gridx = 0;
c.gridy = 3;
panel2.add(passL, c);
passwordField = new JPasswordField(15);
passwordField.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.gray));
passwordField.setOpaque(false);
c.gridx = 0;
c.gridy = 4;
panel2.add(passwordField, c);
submit = new JButton("Sign In");
c.gridx = 0;
c.gridy = 5;
c.insets = new Insets(10, 0, 0, 0); // add top padding
c.ipadx = 93;
panel2.add(submit, c);
updateOne = new JLabel("MySQL JDBC Driver Registered!");
c.gridx = 0;
c.gridy = 4;
panel3.add(updateOne,c );
updateOne.setVisible(false);
updateTwo = new JLabel("You are now connected to your database.");
c.gridx = 1;
c.gridy = 4;
panel3.add(updateTwo);
updateTwo.setVisible(false);
login.add(panel1);
login.add(panel2);
login.add(panel3);
// call text handler constructor to complete correct actions depending on user action
TextHandler handler = new TextHandler();
userTF.addActionListener(handler);
passwordField.addActionListener(handler);
submit.addActionListener(handler);
}
// inner class TextHandler
private class TextHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
// check if user hit to submit with userTF, passwordField, or submit button
if(e.getSource() == userTF){
username = e.getActionCommand();
} else if(e.getSource() == passwordField){
password = e.getActionCommand();
} else if(e.getSource() == submit){
// try to access mysql driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException s) {
error = new JLabel("Where is your MySQL JDBC Driver?"); // if driver not found output label
s.printStackTrace();
c.gridx = 0;
c.gridy = 3;
add(error, c);
return;
} // end try
updateOne.setVisible(true); // make label visible
Connection connection = null;
// try to login to mysql database
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost/PLA", username, password); // fetch username and password
} catch (SQLException s) {
error = new JLabel("Connection Failed! Check output console"); // error if login failed
s.printStackTrace();
c.gridx = 0;
c.gridy = 3;
add(error, c);
return;
} // end try
// if login was successful output label
if (connection != null) {
updateTwo.setVisible(true);
} else {
error = new JLabel("Failed to make connection!"); // if login failed, output label
c.gridx = 0;
c.gridy = 3;
add(error, c);
} // end if
} // end submit else if
} // end action performed
} // end private class text handler } // end class login