带有选项卡的JFrame不会显示 [英] JFrame with tabs is not being displayed

查看:96
本文介绍了带有选项卡的JFrame不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with my login screen. Somehow it is not being correctly...

所有对象(JLabel,JButton,JTextfield等)未正确显示. 它应该是这样的:

All the objects(JLabel,JButton,JTextfield etc.) is not being displayed correctly. This is how it should look:

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

It only appears once I resize the screen size by pulling at the window border.

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

The JFrame contains 2 JPanels as tabs. This is the code:

Login.java

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

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

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
    }
}

我希望有人能给我提示.关于我的代码的任何其他建议也将受到赞赏...仍然是初学者;)

I hope someone can give me a hint. Any other suggestions about my code is also appreciated ... still a beginner ;)

推荐答案

这是因为要在添加组件之前调用setVisible.在JFrame上添加组件后,调用setVisible.

It's because you are calling setVisible before you add components. Call setVisible after you add components on your JFrame.

基本上:

//Frame method
    private void frame() {

        //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);
        //window.setSize(400,250);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);

    }

此外,请调用pack而不是setSize. JFrame的大小将基于JFrame中组件的首选大小以及这些组件之间的间距.

Also, call pack instead of setSize. Size of your JFrame will be based on preferred sizes of components within of JFrame and gaps between those components.

这篇关于带有选项卡的JFrame不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆