未显示带有标签的JFrame

问题描述 投票:0回答:1

我的登录屏幕有问题。不知何故,它不正确。

“登录错误”

所有对象(JLabel,JButton,JTextfield等)未正确显示。这是它的外观:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9QUlkwSS5wbmcifQ==” alt =“外观应该如何>

仅当我通过拉动窗口边框来调整屏幕大小时才出现。

JFrame包含2个JPanels作为选项卡。这是代码:

Login.java

package de.immozukunft.windows;

import javax.swing.*;

import de.immozukunft.tabs.LoginTab;
import de.immozukunft.tabs.MySQLConnectionTab;

import java.awt.BorderLayout;
import java.util.Locale;
import java.util.ResourceBundle;

public class Login {

    /**The Login class implements the LoginTab and the MySQLConnectionTab
     *  to verify the user access*/

    //String management
    static Locale locale = new Locale("de");
    static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);

    JFrame window = new JFrame();

    //Constructor
    public Login() {
        window.setTitle(r.getString("userlogin"));
        frame();
    }

    //Frame method
    private void frame() {
        window.setSize(400,250);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);

        //Tab-panel
        JTabbedPane tabbedPane = new JTabbedPane();

        //Tabs
        tabbedPane.addTab("Login", new LoginTab(window));
        tabbedPane.addTab("MySQL Verbindung", new MySQLConnectionTab()); //TODO Strings einfügen
        //Add tab-panel to frame
        window.add(tabbedPane, BorderLayout.CENTER);
    }
}

LoginTab.java

package de.immozukunft.tabs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import de.immozukunft.overwrittenClasses.JTextFieldImmo;
import de.immozukunft.persons.UserDetails;
import de.immozukunft.programs.MySQL;
import de.immozukunft.windows.ButtonPanel;

public class LoginTab extends JPanel {

    /** LoginTab does the userverfication by 
     * comparing the entered data with the MySQL-database
     * There is no encryption implemented*/

    private static final long serialVersionUID = 1L;

    //Multilingual String-Management
    static Locale locale = new Locale("de");
    static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);

    // static Connection con = null;
    static Statement stnt = null;
    static ResultSet rs = null;

    //Constructor
    public LoginTab(final JFrame window) {
        panelMethod(window);
    }

    // LOGIN ITEMS
    JLabel usernameLbl = new JLabel(r.getString("username"));
    JLabel passwordLbl = new JLabel(r.getString("password"));
    JTextFieldImmo usernameFld = new JTextFieldImmo(15);
    JPasswordField passwordFld = new JPasswordField(15);
    JButton loginBtn = new JButton(r.getString("login"));
    JButton registerUserBtn = new JButton(r.getString("newUser"));

    public void panelMethod(final JFrame window) {
        this.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        // Insets
        c.insets = new Insets(4, 5, 0, 0);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        this.add(usernameLbl, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        this.add(usernameFld, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        this.add(passwordLbl, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        this.add(passwordFld, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        this.add(loginBtn, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        this.add(registerUserBtn, c);

        // Actions Listener
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    MySQL.connect();
                    String username = usernameFld.getText().trim();
                    String password = String.valueOf(passwordFld.getPassword()).trim();

                    String sql = "SELECT username,password from per_user where username = '"
                            + username + "'and password = '" + password + "'";
                    stnt = MySQL.getCon().createStatement();
                    rs = stnt.executeQuery(sql);

                    int count = 0;

                    while (rs.next()) {
                        count = count + 1;
                    }

                    if (count == 1) {
                        //JOptionPane.showMessageDialog(null, "User Found, Access Granded!"); //TODO String
                        window.setVisible(false);
                        window.dispose();
                        new ButtonPanel();
                    } else if (count > 1) {
                        JOptionPane.showMessageDialog(null,
                                "Duplicate User, Access Denied!"); // TODO String einfügen
                    } else {
                        JOptionPane.showMessageDialog(null, r.getString("userNotFound"));
                    }

                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, r.getString("unableToConnectMySQL"));
                    e1.printStackTrace();
                }

            }
        });

        registerUserBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                new UserDetails();
            }
        });

        passwordFld.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                loginBtn.doClick();

            }
        });
    }
}

MySQLConnectionTab.java

package de.immozukunft.tabs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.*;

import de.immozukunft.overwrittenClasses.JTextFieldImmo;
import de.immozukunft.programs.MySQL;

public class MySQLConnectionTab extends JPanel {        //Subclass of Jframe, with ActionListener as interface

    /** MySQLConnectionTab is a JPanel which can be implemented
     * in other JTabbedPanes in order to change the MySQL connection settings 
     */

    private static final long serialVersionUID = 1L;

    private JButton connectBtn = new JButton(r.getString("connect"));                           //Connect-Button
    private JButton cancelBtn = new JButton(r.getString("cancel"));                             //Cancel-Button

    private JTextFieldImmo hostFld = new JTextFieldImmo(MySQL.getHost(), 20);                   // Host text field
    private JTextFieldImmo usernameFld = new JTextFieldImmo(MySQL.getUsername(), 20);           // Username text field
    private JTextFieldImmo databaseFld = new JTextFieldImmo(MySQL.getDatabase(), 20);           // Database text field
    private JTextFieldImmo portFld = new JTextFieldImmo(String.valueOf(MySQL.getPort()), 20);   // Port text field
    private JPasswordField passwordFld = new JPasswordField(MySQL.getPassword(), 20);           // Password text field

    private JLabel hostLbl = new JLabel(r.getString("host"));                           // Host text label
    private JLabel usernameLbl = new JLabel(r.getString("username"));                   // Username text label
    private JLabel databaseLbl = new JLabel(r.getString("database"));                   // Database text label
    private JLabel portLbl = new JLabel(r.getString("port"));                           // Port text label
    private JLabel passwordLbl = new JLabel(r.getString("password"));                   // Password text label

    static Locale locale = new Locale("de");
    static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);

    //Constructor
    public MySQLConnectionTab() {
        panelMethod();
    }

    public void panelMethod(){

        //Create GridBagConstraints
        GridBagConstraints c2 = new GridBagConstraints();

        //SetLayout to GridBagLayout
        this.setLayout(new GridBagLayout());

        //Insets
        c2.insets = new Insets(4, 5, 0, 0);

        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 0;
        this.add(hostLbl, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 1;
        c2.gridy = 0;
        this.add(hostFld, c2);

        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 1;
        this.add(usernameLbl, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 1;
        c2.gridy = 1;
        this.add(usernameFld, c2);

        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 2;
        this.add(databaseLbl, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 1;
        c2.gridy = 2;
        this.add(databaseFld, c2);

        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 3;
        this.add(portLbl, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 1;
        c2.gridy = 3;
        this.add(portFld, c2);

        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 4;
        this.add(passwordLbl, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 1;
        c2.gridy = 4;
        this.add(passwordFld, c2);

        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 1;
        c2.gridy = 5;
        this.add(connectBtn, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 5;
        this.add(cancelBtn, c2);

        connectBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                //All the field data is being set for the MySQL-connection
                MySQL.setHost(hostFld.getText());
                MySQL.setUsername(usernameFld.getText());
                MySQL.setDatabase(databaseFld.getText());
                MySQL.setPort(Integer.parseInt(portFld.getText()));
                MySQL.setPassword(String.valueOf(passwordFld.getPassword())); 

                JOptionPane.showMessageDialog(null, String.format("%s", MySQL.getStatus()));
            }
        });                 // Add ActionListener for connect Button

        cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                hostFld.setText(MySQL.getHost());
                usernameFld.setText(MySQL.getUsername());
                databaseFld.setText(MySQL.getDatabase());
                portFld.setText(String.valueOf(MySQL.getPort()));
                passwordFld.setText(MySQL.getPassword());


            }
        });                 // Add ActionListener for cancel Button
    }
}

我仍然是初学者。

我的登录屏幕有问题。不知何故,它是不正确的。所有对象(JLabel,JButton,JTextfield等)未正确显示。这是它的外观:仅...

java swing jframe jpanel
1个回答
6
投票

这是因为您在添加组件之前正在调用setVisible。在setVisible上添加组件后,请调用JFrame

© www.soinside.com 2019 - 2024. All rights reserved.