这之前工作正常,但是我把项目设置了一段时间,当我决定再次拿起它时,面板没有显示在框架中。我已经尝试了许多不同的可能解决方案,花了几天时间试图解决它没有任何工作。有没有人看到可能导致框架显示空的原因?
// 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
答案 0 :(得分: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
实例中...