Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

swing - Java FlowLayout inside FlowLayout

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

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

1 Reply

0 votes
by (71.8m points)

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"));          
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...