0

这以前工作得很好,但我把项目搁置了一段时间,当我决定重新开始时,面板没有出现在框架中。我尝试了许多不同的可能解决方案,并花了几天时间试图修复它,但没有任何效果。有谁看到可能导致框架显示为空的原因?

// 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
4

1 回答 1

2

我不确定这是否是你的意图,但这对我来说很奇怪......

首先,您声明一个从JFrame...扩展的类

public class login extends JFrame {

然后你声明一个同一个类的实例变量......

JFrame login = new JFrame();

您比将所有组件添加到此实例...

login.add(panel1);
login.add(panel2);
login.add(panel3);

现在,我看不出你是如何使用它的,但我想你正在做类似的事情......

login login = new login()
login.setVisible(true);

基本上,什么都没有显示...

反而。JFrame我不会从 扩展,而是从实例变量扩展JPanel并删除。login

创建一个实例,login然后将其添加到JFrame您创建的实例中......

我还建议您查看Java 编程语言的代码约定

于 2013-09-09T02:10:39.953 回答