2

I have searched this site, other sites, and my textbook and cannot come up with a solution so here goes:

I have a simple temp. conversion program which functions just fine however I would like to make my layout more appealing and more similar to what is asked for in the procedures.

Right now it looks like this:

my program output

And I want it to look like this:

Desired Output

Difference being the Input and Output fields are centered in the desired output.

Here is my program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Convert extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel convertFrom, convertTo, top, bottom;
    private JLabel label3, label4;
    private JTextField temperature1, temperature2;
    private ButtonGroup radioFrom, radioTo;
    private JRadioButton celsiusBoxTo, fahrenheitBoxTo, kelvinBoxTo, celsiusBoxFrom, fahrenheitBoxFrom, kelvinBoxFrom;


    public Convert(){


        fahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
        celsiusBoxFrom = new JRadioButton( "Celsius", false );
        kelvinBoxFrom = new JRadioButton( "Kelvin", false );

        radioFrom = new ButtonGroup();
        radioFrom.add( fahrenheitBoxFrom );
        radioFrom.add( celsiusBoxFrom );
        radioFrom.add( kelvinBoxFrom );

        label3 = new JLabel( "Input" );
        label4 = new JLabel( "Output" );

        fahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
        celsiusBoxTo = new JRadioButton( "Celsius", true );
        kelvinBoxTo = new JRadioButton( "Kelvin", false );

        radioTo = new ButtonGroup();
        radioTo.add( fahrenheitBoxTo );
        radioTo.add( celsiusBoxTo );
        radioTo.add( kelvinBoxTo );

        convertFrom = new JPanel();
        convertFrom.setLayout( new GridLayout( 4, 1 ) );
        convertFrom.add(new JLabel( "Input Scale" ));
        convertFrom.add( fahrenheitBoxFrom );
        convertFrom.add( celsiusBoxFrom );
        convertFrom.add( kelvinBoxFrom );

        convertTo = new JPanel();
        convertTo.setLayout( new GridLayout( 4, 1 ) );
        convertTo.add(new JLabel( "Output Scale" ));
        convertTo.add( fahrenheitBoxTo );
        convertTo.add( celsiusBoxTo );
        convertTo.add( kelvinBoxTo );

        temperature1 = new JTextField( 10 );
        top = new JPanel();
        top.setLayout(new GridLayout(1, 2));
        top.add(label3);
        top.add(temperature1);
        temperature1.addActionListener( new ActionListener(){

            public void actionPerformed( ActionEvent event ){
                int convertTemp, temp;
                temp = Integer.parseInt( ( ( JTextField ) event.getSource() ).getText() ); 

                if ( fahrenheitBoxFrom.isSelected() && celsiusBoxTo.isSelected() ){
                    convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( fahrenheitBoxFrom.isSelected() && kelvinBoxTo.isSelected() ){
                    convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) + 273 );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( celsiusBoxFrom.isSelected() && fahrenheitBoxTo.isSelected() ){
                    convertTemp = ( int ) ( 9.0f / 5.0f * temp + 32 );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( celsiusBoxFrom.isSelected() && kelvinBoxTo.isSelected()){
                    convertTemp = temp + 273;
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( kelvinBoxFrom.isSelected() && celsiusBoxTo.isSelected()){
                    convertTemp = temp - 273;
                    temperature2.setText( String.valueOf( convertTemp ) );
                }

                else if ( kelvinBoxFrom.isSelected() && fahrenheitBoxTo.isSelected()){
                    convertTemp = ( int ) ( 9.0f / 5.0f * ( temp - 273 ) + 32 );
                    temperature2.setText( String.valueOf( convertTemp ) );
                }
            }
            });

        temperature2 = new JTextField( 10 );
        temperature2.setEditable( false );

        bottom = new JPanel();
        bottom.setLayout(new GridLayout(1, 2));
        bottom.add(label4);
        bottom.add(temperature2);


        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add( top, BorderLayout.NORTH );
        container.add( convertFrom, BorderLayout.WEST );
        container.add( convertTo, BorderLayout.EAST );
        container.add( bottom, BorderLayout.SOUTH );
        setSize( 350,250);
        setVisible( true );

    } 

    public static void main ( String args[] ){
        Convert application = new Convert(); 
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Thinker
  • 31
  • 4

2 Answers2

1

BoxLayout can place components centered. BoxLayout respects the setAlignmentX property of a component (see also the answers to this question).
One caveat though: you'll have to manage a component's maximum size if it is configured to take the full available width of the window (like a text-field is). Here is how you can do it for the "bottom" panel:

javax.swing.Box bottomBox = Box.createHorizontalBox();
bottom = new JPanel();
bottomBox.add(bottom);
bottom.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
bottom.add(label4);
temperature2.setMaximumSize(temperature2.getPreferredSize());
bottom.add(temperature2);
...
container.add( bottomBox, BorderLayout.SOUTH );

You can do a similar thing for the "top" panel.

Community
  • 1
  • 1
vanOekel
  • 6,358
  • 1
  • 21
  • 56
0

You can use GridBagLayout for this task. it will easily place your components in a 4x6 grid and you can pretty much specify the desired spacing between components using GridBagConstraints. I would use this layout.

Andrei Mesh
  • 256
  • 2
  • 20