Use A Layout And Arrange Objects

How to manipulate and use a layout and arrange objects the way I want?

A layout you may wish to consider is the BoxLayout (I personally always tend to lean toward BoxLayout because it is both easy and versatile when used in combinations): create a JPanel that has a page axis box layout, then add the components to this layout.

Here's a quick revision, you can of course alter the prettiness of the layout bay adding borders and/or chaning the layouts of the other panels.

Java code:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test extends JFrame implements ActionListener
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;

    private JTextField binaryText;
    private JTextField decimalText;

    private String decimal = "No Binary to Convert";
    private String binary = "No Decimal to Convert";

    public Test( )
  setSize(WIDTH, HEIGHT);
  JPanel main = new JPanel(  );
  main.setLayout( new BoxLayout(main, BoxLayout.PAGE_AXIS ));
        //addWindowListener(new WindowDestroyer( ));
        setTitle("Binary/Decimal Number Converter");
        Container contentPane = getContentPane( );
       // contentPane.setLayout(new BorderLayout( ));

        JPanel buttonPanel = new JPanel( );
        buttonPanel.setLayout(new FlowLayout( ));

        JButton toDecButton = new JButton("To Base 10");

        JButton toBinButton = new JButton("To Base 2");

        JButton clearButton = new JButton("Clear");

        JButton exitButton = new JButton("Exit");

       // contentPane.add(buttonPanel, BorderLayout.SOUTH);

        JPanel textPanel = new JPanel( );
  //contentPane.setLayout(new GridLayout(6, 1));
  JLabel binLabel = new JLabel("Binary number:");

        binaryText = new JTextField(20);
  JLabel decLabel = new JLabel("Decimal number:");
  decimalText = new JTextField(20);
  main.add( textPanel );
  main.add( decLabel );
  main.add( decimalText );
  main.add( buttonPanel );


    public void actionPerformed(ActionEvent e)
        String actionCommand = e.getActionCommand( );
        if (actionCommand.equals("To Base 10"))
            decimal = binaryText.getText( );
        else if (actionCommand.equals("To Base 2"))
            binary = decimalText.getText( );
        else if (actionCommand.equals("Clear")) {
        else if (actionCommand.equals("Exit"))
            binaryText.setText("Error in conversion");

    public static void main(String[] args)
        Test guiMemo = new Test( );

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.