Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to design similar to a browsers title bar(top of the browser). Left side has tabs and right side has minimize, resize(minimize/maximize),exit button.

For this I tried like.

JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JPanel tabpanel= new JPanel();
tabpanel.setLayout(new  FlowLayout(FlowLayout.LEFT));
tabpanel.add(new JButton("Tab 1"));
tabpanel.add(new JButton("Tab 2"));
panel.add(tabpanel); 
panel.add(new JButton("Minimize")); 
panel.add(new JButton("Resize")); 
panel.add(new JButton("Quit")); 

Created Quit, Resize, Minimize buttons at the right as my needed but tabs created near Minimize button not LEFT of Frame. I think there should be method or anything to fill it remainng content or should I use another layout? Any help appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
122 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would highly recommend GridBagLayout, it's one of the most flexible and configurable layout managers available, but it does bring with it complexity

GridBagLayout

public class HeaderPane extends JPanel {

    public HeaderPane() {
        setLayout(new GridBagLayout());

        add(new JButton("Tab 1"));
        add(new JButton("Tab 2"));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.BASELINE_TRAILING;

        add(new JButton("Minimize"), gbc);
        add(new JButton("Maximise"));
        add(new JButton("Close"));          
    }

}

Arrgggh, the complexity burns, it burns ?? sarcasm

So, this solution is a single container, with a single layout manager. I'm not saying a more complex requirement might benefit from a compounding solution (I'd be tempted to put the min/max/close and tab buttons in there own containers), but as a starting point, it's relatively simple.

Runnable example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
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 HeaderPane(), BorderLayout.NORTH);
                frame.add(new JPanel() { 
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(600, 200);
                    }                   
                });
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class HeaderPane extends JPanel {

        public HeaderPane() {
            setLayout(new GridBagLayout());

            add(new JButton("Tab 1"));
            add(new JButton("Tab 2"));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.anchor = GridBagConstraints.BASELINE_TRAILING;

            add(new JButton("Minimize"), gbc);
            add(new JButton("Maximise"));
            add(new JButton("Close"));          
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...