user2883088
user2883088

Reputation: 95

Java GUI Layout Issue

I have the code working for this assignment, but can't figure out how to get it to look properly. I am a bit baffled. Need to use a Border Layout with all five regions added to the container. Just need to be pointed in the right direction...thanks. I can't post a pic...but it is something like this...

              Sum Numbers(Centered)
                   Text box 1
                   Text box 2
                   Text box 3
                   Result is: sum of numbers
                   Button 1(Sum Numbers)Button 2(Close)

Code:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

public class Calculator extends JFrame {

    // Declare GUI components
    private JTextField jtfFirst;
    private JTextField jtfSecond;
    private JTextField jtfThird;
    private JTextField jtfResult;
    private JLabel jlblTitle;
    private JLabel jlblFirst;
    private JLabel jlblSecond;
    private JLabel jlblThird;
    private JLabel jlblResult;
    private JPanel entryPanel;
    private JPanel buttonPanel;
    private JButton jbtAdd;
    private JButton jbtClear;

    // main method to instantiate and customize frame
    public static void main(String[] args)
    {
        Vector<String> myVector=new Vector<String>();
        Calculator frame = new Calculator();
        frame.setTitle( "Assignment 5");
        int windowWidth = 300;
        int windowHeight = 200;
        frame.setSize( 350, 150);
        frame.setLocation(400, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    // GUI constructor
    public Calculator()
    {
        // a bottom JPanel to place everything on.
        //JPanel totalGUI = new JPanel();
        //totalGUI.setLayout(null);

        JLabel textLabel = new JLabel("Add Numbers", JLabel.CENTER);
        textLabel.setFont(new Font("Ariel",Font.PLAIN,24));

        //textLabel.setPreferredSize(new Dimension(300, 100));

        jlblResult = new JLabel( "Total is: ", JLabel.LEFT );
        jtfFirst = new JTextField(9);
        jtfSecond = new JTextField(9);
        jtfThird = new JTextField(9);
        jtfResult = new JTextField(6);
        entryPanel = new JPanel();

        // make result text field uneditable
        jtfResult.setEditable( false );

        // Set layout manager of panel
        entryPanel.setLayout( new GridLayout(6,1,2,2));

        // add GUI components to panel

        entryPanel.add(textLabel );
        entryPanel.add( jtfFirst );
        entryPanel.add( jtfSecond );
        entryPanel.add( jtfThird );
        entryPanel.add( jlblResult);
        entryPanel.add( jtfResult );

        // add entryPanel to frame
        add(entryPanel, BorderLayout.CENTER);

        // Instantiate GUI components for bottom of frame
        jbtAdd = new JButton( "Show Total" );
        jbtClear = new JButton( "Clear" );
        buttonPanel = new JPanel();

        // add buttons to panel
        buttonPanel.add( jbtAdd );
        buttonPanel.add( jbtClear );

        // add buttonPanel to frame
        add( buttonPanel, BorderLayout.SOUTH);

        //Event Handler
        jbtAdd.addMouseListener(new List_ButtonADD());
        jbtClear.addMouseListener(new List_ButtonCLEAR());
    }

    private class List_ButtonADD implements MouseListener
    {
        public void mouseClicked(MouseEvent event)
        {
            double x=Double.parseDouble(jtfFirst.getText());
            double y=Double.parseDouble(jtfSecond.getText());
            double z=Double.parseDouble(jtfThird.getText());

            jtfResult.setText(String.valueOf(x+y+z));

         public void mousePressed(MouseEvent event)
         {
         }
         public void mouseReleased(MouseEvent event)
         {
         }
         public void mouseEntered(MouseEvent event)
         {
         }
         public void mouseExited(MouseEvent event)
         {
         }
    }

    private class List_ButtonCLEAR implements MouseListener
    {
        public void mouseClicked(MouseEvent event)
        {
            System.exit (0);
        }
        public void mousePressed(MouseEvent event)
        {
        }
        public void mouseReleased(MouseEvent event)
        {
        }
        public void mouseEntered(MouseEvent event)
        {
        }
        public void mouseExited(MouseEvent event)
        {
        }
    }
}

Image

Upvotes: 1

Views: 209

Answers (2)

alex2410
alex2410

Reputation: 10994

Here is an example for you. I have used GridBagLayout because it's very flexible.

public class ExampleFrame extends JFrame {

private JTextField jtfFirst;
private JTextField jtfSecond;
private JTextField jtfThird;
private JTextField jtfResult;

public ExampleFrame() {
    super();
    setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 5, 5);
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 0;
    add(new JLabel("Sum Numbers"),c);

    c.gridwidth = 1;
    c.anchor = GridBagConstraints.WEST;
    c.gridy = 1;
    add(new JLabel("Text1"),c);

    c.gridy =2;
    add(new JLabel("Text2"),c);

    c.gridy =3;
    add(new JLabel("Text3"),c);

    c.gridy =4;
    add(new JLabel("Result"),c);

    c.gridy =5;
    JButton b1 = new JButton("b1");
    b1.addActionListener(getActionB1Listener());
    add(b1,c);

    c.gridx = 1;
    c.gridy = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;
    add(jtfFirst = new JTextField(),c);

    c.gridy = 2;
    add(jtfSecond = new JTextField(),c);

    c.gridy = 3;
    add(jtfThird = new JTextField(),c);

    c.gridy = 4;
    add(jtfResult = new JTextField(),c);
    JButton b2 = new JButton("b2");
    b2.addActionListener(getActionB2Listener());

    c.fill = GridBagConstraints.NONE;
    c.weightx = 0;
    c.gridy = 5;
    add(b2,c);
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}


private ActionListener getActionB1Listener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("b1 clicked");

        }
    };
}

private ActionListener getActionB2Listener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("b2 clicked");

        }
    };
}


public static void main(String... s) {
    new ExampleFrame();
}
}

And never do that if you use MouseListener only for clicks :

 jbtAdd.addMouseListener(new List_ButtonADD());
 jbtClear.addMouseListener(new List_ButtonCLEAR());

MouseListener uses for another purposes. Instead of that use ActionListener it uses for button actions.

enter image description here

Upvotes: 2

An SO User
An SO User

Reputation: 24998

Ok here is how you go about doing it:

  1. get a JFrame and set its layout to BorderLayout.
  2. Create a JLabel with text set to "Add Numbers" and add to the JFrame it using BorderLayout.NORTH
  3. create a JPanel, set its layout to BorderLayout.
  4. Create the 3 JTextField, add them to the JPanel using BorderLayout.NORTH, BorderLayout.CENTER and BorderLayout.SOUTH
  5. Create another JPanel.
  6. Create a JLabel to display "Total is 60". Also create the two JButtons.
  7. Set the Layout of JPanel of step 5 to BorderLayout. Add the JLabel using BorderLayout.NORTH.
  8. Create another JPanel with FlowLayout, add the two buttons. Then add this JPanel to the JPanel in step 5 with BorderLayout.SOUTH.
  9. Add all the JPanels to your JFrame.

Upvotes: 2

Related Questions