glac19
glac19

Reputation: 25

Using JFrame and Alignment

I asked another JFrame question earlier in the day and was able to figure out my problem, but now I've hit another snag. I need my UI to look like the one pictured, but I can't figure out how to get them to stack on top of each other using a LayoutManager. If use BorderLayout South on both, only the most recently added one will be visible. Any tips?

Heres what it should look like: enter image description here

And here is my code:

JFrame frame = new JFrame();
    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
    JPanel textPanel = new JPanel();

    textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));

    JTextField textField = new JTextField();
    textField.setMaximumSize((new Dimension(10000,25)));

    textPanel.add(new JLabel("Filename:"));
    textPanel.add(textField);
    textPanel.add(new JButton("Load"));

    frame.add(textPanel);

    buttonPanel.add(new JButton("Play"));
    buttonPanel.add(new JButton("Stop"));


    frame.add(buttonPanel, BorderLayout.SOUTH);

    frame.setSize(500, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true); 

Upvotes: 0

Views: 144

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Make use of another container to put the textPane and buttonPane in

Compound Panels

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            JPanel textPane = new JPanel(new GridBagLayout());
            JTextField textField = new JTextField(20);

            textPane.add(new JLabel("Filename:"));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            textPane.add(textField, gbc);
            textPane.add(new JButton("Load"));

            JPanel buttonPane = new JPanel();
            buttonPane.add(new JButton("Play"));
            buttonPane.add(new JButton("Stop"));

            JPanel southPane = new JPanel(new GridLayout(2, 0));
            southPane.add(textPane);
            southPane.add(buttonPane);

            add(new JLabel("I'm in the center"));
            add(southPane, BorderLayout.SOUTH);
        }

    }

}

Upvotes: 1

Related Questions