Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help understanding GUI screen layouts and labels

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Need help understanding GUI screen layouts and labels

    Ok, I have to make a program which converts binary to decimal and decimal to binary, making my own calculations for it. That part is no problem, but right now I'm trying to construct the basic layout of what the screen should look like, but I'm having problems with it.

    The general idea of what I want is:

    --------------------------------------------------------------
    |                                            ___________              
    |                  Binary number: |  (text field)  |             
    |                                                                                
    |                               Decimal number:                       
    |            _______________________________      
    |           |                      (text field)                      |      
    |                                                                                 
    |  [Button 1]    [Button 2]    [Button 3]    [Button 4] 
    |                                                                                 
    ---------------------------------------------------------------

    Now, with my current code, which I will show soon, this is basically what I am getting:


    --------------------------------------------------------------
    |       [Button 1]    [Button 2]    [Button 3]    [Button 4] 
    |                                                                                
    |   Binary number:                                                     
    |                                                                                
    |   Decimal number:                                                   
    |            _______________________________      
    |           |                      (text field)                      |      
    |                                                                                 
    |            _______________________________      
    |           |                      (text field)                      |      
    ---------------------------------------------------------------

    I've tried messing with some things, but I only end up really messing it up. Here is my code:

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    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 BinaryDecimalConverter 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 BinaryDecimalConverter( )
        {
    		setSize(WIDTH, HEIGHT);
            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");
            toDecButton.addActionListener(this);
            buttonPanel.add(toDecButton);
     
            JButton toBinButton = new JButton("To Base 2");
            toBinButton.addActionListener(this);
            buttonPanel.add(toBinButton);
     
            JButton clearButton = new JButton("Clear");
            clearButton.addActionListener(this);
            buttonPanel.add(clearButton);
     
            JButton exitButton = new JButton("Exit");
            exitButton.addActionListener(this);
            buttonPanel.add(exitButton);
     
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
     
            JPanel textPanel = new JPanel( );
     
    		contentPane.setLayout(new GridLayout(6, 1));
     
    		JLabel binLabel = new JLabel("Binary number:");
            contentPane.add(binLabel);
     
            binaryText = new JTextField(20);
            binaryText.setBackground(Color.WHITE);
            textPanel.add(binaryText);
            contentPane.add(textPanel);
     
    		JLabel decLabel = new JLabel("Decimal number:");
            contentPane.add(decLabel);
     
    		decimalText = new JTextField(20);
    		decimalText.setBackground(Color.WHITE);
    		textPanel.add(decimalText);
            contentPane.add(textPanel);
     
        }
     
        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")) {
                binaryText.setText("");
                decimalText.setText("");
    		}
            else if (actionCommand.equals("Exit"))
                System.exit(0);
            else
                binaryText.setText("Error in conversion");
         }
     
        public static void main(String[] args)
        {
            BinaryDecimalConverter guiMemo = new BinaryDecimalConverter( );
            guiMemo.setVisible(true);
        }
    }

    If anyone can give me a better idea of how to manipulate and use a layout and arrange objects the way I want, it would be much appreciated.

    Thanks!


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Need help understanding GUI screen layouts and labels

    Last edited by chronoz13; December 2nd, 2009 at 03:56 AM.

  3. The Following User Says Thank You to chronoz13 For This Useful Post:

    Bill_H (December 2nd, 2009)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help understanding GUI screen layouts and labels

    The problems you see have to do with you change in layout in the contentPane and order of addition to the contentPane. 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.

    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");
            toDecButton.addActionListener(this);
            buttonPanel.add(toDecButton);
     
            JButton toBinButton = new JButton("To Base 2");
            toBinButton.addActionListener(this);
            buttonPanel.add(toBinButton);
     
            JButton clearButton = new JButton("Clear");
            clearButton.addActionListener(this);
            buttonPanel.add(clearButton);
     
            JButton exitButton = new JButton("Exit");
            exitButton.addActionListener(this);
            buttonPanel.add(exitButton);
     
           // contentPane.add(buttonPanel, BorderLayout.SOUTH);
     
            JPanel textPanel = new JPanel( );
     
    		//contentPane.setLayout(new GridLayout(6, 1));
     
    		JLabel binLabel = new JLabel("Binary number:");
            //contentPane.add(binLabel);
     
            binaryText = new JTextField(20);
            binaryText.setBackground(Color.WHITE);
    		textPanel.add(binLabel);
            textPanel.add(binaryText);
            //contentPane.add(textPanel);
     
    		JLabel decLabel = new JLabel("Decimal number:");
            //contentPane.add(decLabel);
     
    		decimalText = new JTextField(20);
    		decimalText.setBackground(Color.WHITE);
    		//textPanel.add(decimalText);
     
    		main.add( textPanel );
    		main.add( decLabel );
    		main.add( decimalText );
    		main.add( buttonPanel );
    		getContentPane().add(main);
    		pack();
            //contentPane.add(textPanel);
     
        }
     
        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")) {
                binaryText.setText("");
                decimalText.setText("");
    		}
            else if (actionCommand.equals("Exit"))
                System.exit(0);
            else
                binaryText.setText("Error in conversion");
         }
     
        public static void main(String[] args)
        {
            Test guiMemo = new Test( );
            guiMemo.setVisible(true);
        }
    }

  5. The Following User Says Thank You to copeg For This Useful Post:

    Bill_H (December 2nd, 2009)

  6. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Need help understanding GUI screen layouts and labels

    Thanks for the help you two! I have the outlay I'm looking for, now I have another problem I've run into: my buttons don't appear to be working correctly.

    I've started writing my method to convert to decimal, but the setText method doesn't do anything to change what is shown in the textfield. Even if I do something like:

    binaryText.setText("random text");

    It doesn't change the status of the box at all. I also noticed that while my Clear button appears to work with:

    binaryText.setText("");
    decimalText.setText("");

    If I change the parameter to:

    binaryText.setText("Do something");

    It still clears the text box, but does not replace it with "Do something"

    Any ideas?



    EDIT: After some alterations, I have managed to fully complete the program. Thanks for the help!
    Last edited by Bill_H; December 3rd, 2009 at 02:06 AM.

Similar Threads

  1. Help - Return to menu screen
    By throzen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2009, 01:44 PM
  2. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM
  3. Content positioning on the screen
    By Drakenmul in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2009, 09:02 AM
  4. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM
  5. Java program for remote host screen capture
    By krishnaraoveera1294 in forum Java Networking
    Replies: 1
    Last Post: March 13th, 2009, 04:53 AM