3

I am working on this program, using swing. Every time I export the program, and run it, the GUI I atempt to set up doesn't appear. The JFrame does, but not the inner components. Thanks in advance ~Airis

Code:

import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Login {
    public static void login_Interface(){

        //Start GUI style//
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            e1.printStackTrace();
        }
        //      End     //

        JFrame login_Frame = new JFrame("Login - LetsMeet");
        login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        login_Frame.setSize(750, 650);
        login_Frame.setResizable(true);

        JPanel panel_Title = new JPanel();                            //PANEL Title
        panel_Title.setBounds(0, 0, 750, 150);
        panel_Title.setLayout(null);
        Image logo = null;
        try {
            logo = ImageIO.read(new File("Data/images/logo_letsmeet.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Graphics2D logo_out = ((BufferedImage) logo).createGraphics();
        panel_Title.paint(logo_out);

        JPanel login_Panel = new JPanel();                            //LOGIN Panel
        login_Panel.setBounds(0, 150, 350, 150);
        login_Panel.setLayout(null);
        JTextField username_login = new JTextField("Username");
        username_login.setBounds(100, 50, 100, 25);
        JPasswordField password_login = new JPasswordField();
        password_login.setBounds(200, 50, 100, 25);
        JButton login_go = new JButton("Login");
        login_go.setBounds(200, 50, 100, 25);
        login_Panel.add(password_login);
        login_Panel.add(username_login);


        JPanel panel_Divider = new JPanel();                          //PANEL Divider
        login_Panel.setBounds(350, 150, 50, 150);
        panel_Divider.setSize(50, 100);
        panel_Divider.setLayout(null);
        Image sep = null;
        try {
            sep = ImageIO.read(new File("Data/images/sep.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Graphics2D div = ((BufferedImage) sep).createGraphics();
        panel_Title.paint(div);

        JPanel register_Panel = new JPanel();                         //REGISTER Panel
        register_Panel.setBounds(400, 150, 350, 150);
        register_Panel.setLayout(null);

        login_Frame.add(panel_Title);
        login_Frame.add(login_Panel);
        login_Frame.add(panel_Divider);
        login_Frame.add(register_Panel);
        login_Frame.setVisible(true);
    }
}

Errors: None

1

3 Answers 3

8

Firstly...

panel_Title.paint(logo_out); this is not how graphics work in Swing...nor is it how images are painted in Swing

Secondly...

You should be making use of layout managers, they will greatly reduce potential issues as well as reduce the complexity of your code.

Thirdly

You application should be started within the context of the Event Dispatching Thread...

public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            // Construct your UI here...
        }
    });
}

Fourthly

As Andrew has also pointed out (+1)...your images "appear" to be internal resources, which will not be accessible via a File reference. You need to load these kind of (embedded) resources differently...

logo = ImageIO.read(Login.class.getResource("/Data/images/logo_letsmeet.png"));

You also ignore the potential for these resources to be null which is a very dangerous practice.

I would suggest you have a read through

1
  • Amen. You can't guess at this stuff and expect it to magically work. 1+. Dec 11, 2012 at 3:14
4

Besides all the suggestion made by @MadProgammer, you need to add your controls to the JFrame content pane, like this:

login_Frame.getContentPane().add(panel_Title);
login_Frame.getContentPane().add(login_Panel);
...

Then your controls should appear

Update:

Running your actual code, and adding a colored border to the containers (JPanels), I got the following:

panel_Title.setBorder(BorderFactory.createLineBorder(Color.BLUE));
login_Panel.setBorder(BorderFactory.createLineBorder(Color.RED));
panel_Divider.setBorder(BorderFactory.createLineBorder(Color.GREEN));
register_Panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW));

enter image description here

Basically your code has layout configuration problems. Again, follow the suggestion of @MadProgammer. You could use this border trick in future to debug your layouts

4
  • 3
    JFrame#add defers to getContentPane() automatically (as of Java 6). So far, the only method I've had issue with is JFrame#removeAll, which under Java 7 seems to remove the root pane... Dec 11, 2012 at 3:22
  • 1
    It's spelled getContentPane(). @MadProgrammer: I've had to help someone here who used BoxLayout and had issue because he was passing it into JFrame's setLayout(...) (no problem), but then was passing the JFrame into the BoxLayout's constructor (big problem!). Dec 11, 2012 at 3:32
  • @HovercraftFullOfEels Oh, that's an awesome tip to keep in mind! Dec 11, 2012 at 3:34
  • Thank you for your post, I will try this and thank you to @MadProgrammer I will start implementing some of your tips.
    – Airis
    Dec 11, 2012 at 4:21
1
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Login {
    public static void login_Interface(){

        //Start GUI style//
        try {
                 UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
             e1.printStackTrace();
        } catch (InstantiationException e1) {
             e1.printStackTrace();
        } catch (IllegalAccessException e1) {
             e1.printStackTrace();
         } catch (UnsupportedLookAndFeelException e1) {
              e1.printStackTrace();
         }
          //      End     //

          JFrame login_Frame = new JFrame("Login - LetsMeet");
          login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         login_Frame.setSize(750, 650);
          login_Frame.setResizable(true);

         JPanel panel_Title = new JPanel();                            //PANEL Title
         panel_Title.setBounds(0, 0, 750, 150);
         panel_Title.setLayout(null);
         Image logo = null;
         try {
              logo = ImageIO.read(new File("Data/images/logo_letsmeet.png"));
          } catch (IOException e) {
              e.printStackTrace();
         }
         Graphics2D logo_out = ((BufferedImage) logo).createGraphics();
         panel_Title.paint(logo_out);

           JPanel login_Panel = new JPanel();                            //LOGIN Panel
          login_Panel.setBounds(0, 150, 350, 150);
          login_Panel.setLayout(null);
          JTextField username_login = new JTextField("Username");
    username_login.setBounds(100, 50, 100, 25);
    JPasswordField password_login = new JPasswordField();
    password_login.setBounds(200, 50, 100, 25);
    JButton login_go = new JButton("Login");
    login_go.setBounds(200, 50, 100, 25);
    login_Panel.add(password_login);
    login_Panel.add(username_login);


    JPanel panel_Divider = new JPanel();                          //PANEL Divider
    login_Panel.setBounds(350, 150, 50, 150);
    panel_Divider.setSize(50, 100);
    panel_Divider.setLayout(null);
    Image sep = null;
    try {
        sep = ImageIO.read(new File("Data/images/sep.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Graphics2D div = ((BufferedImage) sep).createGraphics();
    panel_Title.paint(div);

    JPanel register_Panel = new JPanel();                         //REGISTER Panel
    register_Panel.setBounds(400, 150, 350, 150);
    register_Panel.setLayout(null);

    login_Frame.add(panel_Title);
    login_Frame.add(login_Panel);
    login_Frame.add(panel_Divider);
    login_Frame.add(register_Panel);
    login_Frame.setVisible(true);
}

}

You do not set any layout manager in this program . That's why u can't display any thing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.