Hrach Ghapantsyan
Hrach Ghapantsyan

Reputation: 699

GUI not appearing

I have a Main class that has a public static void main(String[] args) {}. I also have a class called appGUI. I have been trying to get the GUI to load when i run the Main class, but nothing occurs, not even any errors... :(

Here is the Main class:

    public class Main {

    /**
     * @param args
     */

    public static void main(String[] args) throws Exception {

        appGUI gui = new appGUI();
    }

}

And here is the appGUI class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Wed Apr 03 19:24:35 BST 2013
 */



/**
 * @author Hrach Ghapantsyan
 */
public class appGUI extends JFrame {
    public appGUI() {
        initComponents();
    }

    private void loginButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
        loginPasswordField = new JPasswordField();
        loginUsernameField = new JTextField();
        usernameLabel = new JLabel();
        passwordLabel = new JLabel();
        loginButton = new JButton();
        titleLabel = new JLabel();

        //======== this ========
        setTitle("Experimental X | Administrator Login");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(loginPasswordField);
        loginPasswordField.setBounds(80, 65, 100, loginPasswordField.getPreferredSize().height);
        contentPane.add(loginUsernameField);
        loginUsernameField.setBounds(80, 35, 100, loginUsernameField.getPreferredSize().height);

        //---- usernameLabel ----
        usernameLabel.setText("Username:");
        contentPane.add(usernameLabel);
        usernameLabel.setBounds(20, 40, 55, usernameLabel.getPreferredSize().height);

        //---- passwordLabel ----
        passwordLabel.setText("Password:");
        contentPane.add(passwordLabel);
        passwordLabel.setBounds(20, 70, 55, passwordLabel.getPreferredSize().height);

        //---- loginButton ----
        loginButton.setText("Login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginButtonActionPerformed(e);
            }
        });
        contentPane.add(loginButton);
        loginButton.setBounds(80, 95, 100, loginButton.getPreferredSize().height);

        //---- titleLabel ----
        titleLabel.setText("Experimental X | Administrator Login");
        contentPane.add(titleLabel);
        titleLabel.setBounds(45, 10, 190, titleLabel.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(270, 170);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
    private JPasswordField loginPasswordField;
    private JTextField loginUsernameField;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JButton loginButton;
    private JLabel titleLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

I have tried running the main class on Eclipse and netbeans, but it runs and then stops after a few seconds. I do not get any errors. Do any of you here have any suggestions? Thanks.

Upvotes: 0

Views: 310

Answers (4)

Reimeus
Reimeus

Reputation: 159754

You haven't called JFrame#setVisible:

gui.setVisible(true);

Some notes:

  • Avoid using null layout. Always use a layout manager
  • Don't use the setXXXSize methods. Override getPreferredSize method to determine component sizes
  • Rather than extending a JFrame, you typically want to create one and use directly.
  • Consider creating the JFrame in the EDT by using initial threads

Upvotes: 1

hamid
hamid

Reputation: 2079

Just add:

this.setVisible(true);

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

public class appGUI extends JFrame {

    setVisible(true);

   // ----
}

place this code to make visible.

Upvotes: 0

Ken Harris
Ken Harris

Reputation: 113

Make sure you call setVisible(true) on the GUI.

Upvotes: 0

Related Questions