0

I am pretty new in Java Swing development and I have the following problem

I have the following 3 classes:

1) A Main class that simply show a LoginFrame class:

package com.test.login4;

import javax.swing.JFrame;


public class Main {

    private static final LoginFrame loginFrame = new LoginFrame();

    //private static final GUI gui = new GUI();

    public static void main(String[] args) {
          System.out.println("Main ---> main()");   

          loginFrame.setVisible(true);


    }

}

2) Then I have the LoginFrame class that simply extends a classic Swing JFrame that show a login form where a user can insert a username and a password:

package com.test.login4;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class LoginFrame extends JFrame implements ActionListener {

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    private boolean loginResult = true;

    public LoginFrame() {

        System.out.println("Inside LoginFrame ---> LoginFrame()");

        this.setTitle("XCloud Login");

        this.setPreferredSize(INITAL_SIZE);
        this.setResizable(false);

        Container mainContainer = this.getContentPane(); // main Container into
                                                            // the main JFrame

        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            // externalPanel = new JPanelWithBackground("resources/logo.png");
            externalPanel = new JPanelWithBackground("src/com/test/resources/logo.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");
        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("loginAction");
        loginButton.addActionListener(this);

        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        // mainFrame.add(mainContainer);
        // loginFrame.setVisible(true);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}

3) This class use a JPanelWithBackground object to have a background immage

package com.test.login4;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class JPanelWithBackground extends JPanel {

      private Image backgroundImage;

      // Some code to initialize the background image.
      // Here, we use the constructor to load the image. This
      // can vary depending on the use case of the panel.
      public JPanelWithBackground(String fileName) throws IOException {
        backgroundImage = ImageIO.read(new File(fileName));
      }

      public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Draw the background image.
        //g.drawImage(backgroundImage, 0, 0, this);
        g.drawImage(backgroundImage, 0, 0, 550, 230, this);
      }
    }

My problem is that, when I execute, the Main class it appear to me the icon of my Java application (on the Ubuntu bar) but I can't show the LoginFrame window.

Why? What am I missing?

I tried to create and show a classic JFrame instead of my LoginFrame (in the Main class) and I have no problem.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) In this case, the log-in should be a modal `JDialog` or a `JOptionPane`. – Andrew Thompson Nov 14 '13 at 15:14
  • You shouldn't hardcode the size of your JPanelWithBackground. Use the size of the image that you read in. – Dodd10x Nov 14 '13 at 16:21

3 Answers3

2

There are a number of things that you should change.

Calling loginFrame.pack() before you call loginFrame.setVisible() should allow it to size itself properly and display.

There is no need to make loginFrame static. Make it a variable inside your main method - althought it should really be called from inside your program code that uses the login frame.

LoginFrame should extend JDialog instead of JFrame. Unless you are using multiple monitors of different sizes you should only have on JFrame in your program. Anything that should sit on top of the main program can be made a JDialog.

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
1

The method this.setPreferredSize(INITAL_SIZE); does not do what you expect. Call setSize method on LoginFrame constructor.

  setSize(INITAL_SIZE);
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • 1
    It is neither necessary nor optimal to be setting the size of a frame. – Andrew Thompson Nov 14 '13 at 15:23
  • @AndrewThompson, I know, but with `setSize` OP can view the login frame. – Masudul Nov 14 '13 at 15:26
  • 1
    `pack()` is what is needed there. If there are any custom components that do not otherwise suggest a size (e.g. a `BGImagePanel`), then that custom component should have a preferred size set (or overridden). – Andrew Thompson Nov 14 '13 at 16:20
  • If you want your code to continue working as expected when you make changes, please do not call setSize. You will run into problems later. – Dodd10x Nov 14 '13 at 16:26
0

and also setBounds method makes you have setSize and In addition have x,y for your Frame.

Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27