Search code examples
javaswingjbutton

How to get my JButtons to be on the left and not in the middle


I'm a little lost here, when I run the program the buttons are in the middle of the input and not directly on the bottom aligned with it. I'm not sure what I'm doing wrong. I'm also trying to find out how to get statistics for my input like min and max value, and average word size. I'm a little lost, thanks!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Arrays;
public class CopyTextPanel extends JPanel
{
private JTextField input;
private JLabel output, inlabel, outlabel;
private JButton compute, clear;
private JPanel panel, panel1, panel2;

public CopyTextPanel()
{
    setLayout (new BoxLayout(this, BoxLayout.Y_AXIS));
    inlabel = new JLabel("Input:  ");
    outlabel = new JLabel("Text Statistics Result: ");
    input = new JTextField (50);
    output = new JLabel();
    compute = new JButton("Compute Statistics");
    compute.addActionListener (new ButtonListener());
    clear = new JButton("Clear Text");
    clear.addActionListener (new ButtonListener());
    panel = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();

    output.setMaximumSize (new Dimension(500, 30));
    output.setMinimumSize (new Dimension(500, 30));
    panel.setMaximumSize (new Dimension(500, 30));
    panel.setMinimumSize (new Dimension(500, 30));
    panel.setBackground(Color.gray);
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(inlabel);
    panel.add(input);
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
    panel1.add(compute);
    panel1.add(clear);
    add (Box.createRigidArea (new Dimension(0, 10)));
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
    panel2.add(outlabel);
    panel2.add(output);
    setMaximumSize (new Dimension(600, 250));
    setMinimumSize (new Dimension(600, 250));
    setBackground(Color.white);
    add (panel);
    add (panel1);
    add (panel2);
}

private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {

        String inputText = input.getText();//sets what is typed by the user 
to a String object

        String[] splitted = inputText.trim().split("\\p{javaSpaceChar}
{1,}");//makes a String array, and trims all the whitespaces from the user 
input
        int numberofWords = splitted.length;//it then sets splitted length 
to an integer
        String numow = Integer.toString(numberofWords);// finally it makes 
the numberofwords int into a string 
        Arrays.sort(splitted);
        if (event.getSource()==compute)//if the user presses the compute 
button
        {
            output.setText (numow + " words; " );//the output is the string 
of integers of how many words were typed
        }
        else//if the user presses another button
            input.setText(" "); // clear text filed after copying
    }
}
}

Solution

  • So based on the desired result...

    Desired Result

    I would recommend considering using a series of panels, dedicated to generating the layout requirements for each row, then wrapping those together into a single container.

    For my money, GridBagLayout presents the most flexible option, while also presenting one of the more complicated at the same time.

    This example focuses solely on the layout requirements, you'll have to figure out how to apply the functionality to it later.

    Example output

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.HeadlessException;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.border.EmptyBorder;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Test");
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() throws HeadlessException {
                setBorder(new EmptyBorder(5, 5, 5, 5));
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(2, 2, 2, 2);
                JPanel fieldPane = new JPanel(new GridBagLayout());
    
                JTextField inputField = new JTextField("Computer Science 1");
                fieldPane.add(new JLabel("Input Text:"), gbc);
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.weightx = 1;
                fieldPane.add(inputField, gbc);
    
                JPanel buttonPane = new JPanel(new GridBagLayout());
                gbc = new GridBagConstraints();
                gbc.insets = new Insets(2, 2, 2, 2);
    
                buttonPane.add(new JButton("Computer Statistics"), gbc);
                gbc.anchor = GridBagConstraints.LINE_START;
                gbc.weightx = 1;
                buttonPane.add(new JButton("Clear Text"), gbc);
    
                JPanel resultsPane = new JPanel(new GridBagLayout());
                gbc = new GridBagConstraints();
                gbc.insets = new Insets(2, 2, 2, 2);
    
                resultsPane.add(new JLabel("Text Statistics Result:"));
                gbc.anchor = GridBagConstraints.LINE_START;
                gbc.weightx = 1;
                resultsPane.add(new JLabel("3 words"), gbc);
    
                gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
    
                add(fieldPane, gbc);
                add(buttonPane, gbc);
                add(resultsPane, gbc);
            }
    
        }
    
    }
    

    I strongly recommend having a look at Laying Out Components Within a Container for more details about how various layout managers work