Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:35:18+00:00 2026-06-04T14:35:18+00:00

I’ve checked the internet about FlowLayout , Group etc., all with unhelpful examples. I

  • 0

I’ve checked the internet about FlowLayout, Group etc., all with unhelpful examples. I just need a basic way to do a good layout for my Java application. I’ll show you my code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Test1 {

    //Step1 - Declaring variables
    private static JFrame myFrame;
    private static JPanel myPanel;
    private static JLabel titleLabel=null;
    private static JLabel logIn=null;
    private static JLabel username=null;
    private static JLabel password=null;
    private static JTextField usernameField=null;
    private static JPasswordField passwordField=null;
    private static Color myColor=new Color(0, 102, 204);
    private static Font myFont11=new Font("Tahoma", 1, 11);
    private static Font myFont12bold=new Font("Tahoma", Font.BOLD, 12);
    private static Font myFont11bold=new Font("Tahoma", Font.BOLD, 11);

    //Step2 - Creating Components
    public void createComponents() {


        //Title Label
        titleLabel=new JLabel("My Program");
        titleLabel.setForeground(Color.white);
        titleLabel.setFont(myFont12bold);
        //titleLabel.setVisible(false); //hide it or show it
        //--------------------------------------------------------


        logIn=new JLabel("Log in");
        logIn.setFont(myFont11bold);
        logIn.setForeground(Color.white);
        username=new JLabel("Username");
        username.setLabelFor(usernameField);
        username.setFont(myFont11);
        username.setForeground(Color.white);
        password=new JLabel("Password");
        password.setLabelFor(passwordField);
        password.setFont(myFont11);
        password.setForeground(Color.white);
        usernameField=new JTextField(10);
        usernameField.setBorder(new LineBorder(null, 0, false));
        passwordField=new JPasswordField(10);
        passwordField.setBorder(new LineBorder(null, 0, false));

        //Panel
        myPanel=new JPanel();
        myPanel.setBackground(myColor);
        myPanel.add(titleLabel);       
        myPanel.add(logIn);
        myPanel.add(mySeparator2);
        myPanel.add(username);
        myPanel.add(usernameField);
        myPanel.add(password);
        myPanel.add(passwordField);
        //----------------------------------------------------------

    //Step3 - Main Function
    public static void main(String[] arg) {

        //Frame
        myFrame=new JFrame();

        myFrame.setPreferredSize(new Dimension(400,300));//width:400px, height:300px
        myFrame.setLocationRelativeTo(null);//to show at center of screen
        myFrame.setTitle("My Program");
        Test1 prog=new Test1();
        prog.createComponents();
        myFrame.add(myPanel);
        myFrame.pack();//this alone will not give the frame a size
        myFrame.setVisible(true);
        //----------------------------------------------------------------------

    }

}

This is a basic gui which has some labels and some textfields, with the .pack() method they will be shown on the same line, I just need a small simple way to make a good layout

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:35:19+00:00Added an answer on June 4, 2026 at 2:35 pm

    Too many static variables inside your code is not an ideal solution for many a reasons. Try to use a different approach, until and unless you not thinking about making one Factory Class for your Project. Have a look at this modified version, is this good enough :

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.LineBorder;
    
    public class Test1 {
    
        //Step1 - Declaring variables
        private JFrame myFrame;
        // Added by me
        private JPanel contentPane;
        private JPanel myPanel;
        private JLabel username=null;
        private JLabel password=null;
        private JTextField usernameField=null;
        private JPasswordField passwordField=null;
        private Color myColor=new Color(200, 102, 204);
        private Font myFont11=new Font("Tahoma", 1, 11);
        private Font myFont12bold=new Font("Tahoma", Font.BOLD, 12);
        private Font myFont11bold=new Font("Tahoma", Font.BOLD, 11);
    
        //Step2 - Creating Components
        public void createComponents() {
    
            contentPane = new JPanel();
            contentPane.setOpaque(true);
            contentPane.setBackground(Color.WHITE);
            contentPane.setLayout(new GridBagLayout());
            contentPane.setBorder(BorderFactory.createTitledBorder("My Program"));
    
            username=new JLabel("Username");
            username.setLabelFor(usernameField);
            username.setFont(myFont11);
            username.setForeground(Color.white);
            password=new JLabel("Password");
            password.setLabelFor(passwordField);
            password.setFont(myFont11);
            password.setForeground(Color.white);
            usernameField=new JTextField(10);
            usernameField.setBorder(new LineBorder(null, 0, false));
            passwordField=new JPasswordField(10);
            passwordField.setBorder(new LineBorder(null, 0, false));
    
            //Panel
            myPanel=new JPanel();
            myPanel.setOpaque(true);
            myPanel.setBorder(BorderFactory.createTitledBorder("Login"));
            myPanel.setBackground(myColor);
            myPanel.setLayout(new GridLayout(2, 2, 2, 2));
            myPanel.add(username);
            myPanel.add(usernameField);
            myPanel.add(password);
            myPanel.add(passwordField);
            //----------------------------------------------------------
            contentPane.add(myPanel);
    
            myFrame=new JFrame();
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //myFrame.setPreferredSize(new Dimension(400,300));//width:400px, height:300px
            myFrame.setLocationRelativeTo(null);//to show at center of screen
            myFrame.setTitle("My Program");
            //myFrame.add(myPanel);
            myFrame.setContentPane(contentPane);
            myFrame.pack();//this alone will not give the frame a size
            myFrame.setVisible(true);
        }   
    
        //Step3 - Main Function
        public static void main(String[] arg) {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Test1().createComponents();
                }
            });
        }
    
    }
    

    Here is the output :

    LOGIN WINDOW

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.