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
391 views
in Technique[技术] by (71.8m points)

Java - JFrame break Components like FlowLayout, but order them vertically

since some hours I am trying to create a JFrame whit following behavior: I have a Title (with maybe an Image included) -This is centered at the top. -> Check The rest of this JFrame should be filled with JPanel.

The first JPanel should be placed in the center on top. The second JPanel should be placed under the first and so on. When the first JPanel runs out of the screen, a second row needs to be created. I dont care if they are reordered or not. Here is a drawing i prepared to show what I want: Example - I need one of that behaviors

I have tried out so many combination of different Layouts.. But still can't find the right solution.

Here is my code, which is working fine besides the fact, that my panels are ordered from left to right instead of from top to down.. :(

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class myFrameClass extends JFrame {

    myFrameClass() {
        super();
        setLayout(new BorderLayout());
        GraphicsDevice[] gs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        GraphicsConfiguration[] gc = gs[0].getConfigurations();
        setUndecorated(true);
        setBackground(Color.WHITE);
        setLocation(gc[0].getBounds().x, gc[0].getBounds().y);
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        add(getHeader(), BorderLayout.NORTH);
        add(getPanels());

        setVisible(true);
    }

    private JPanel getPanels() {
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout());
        int cnt = 6;

        for (int i = 0; i < cnt; i++) {
            p.add(getPanel());
        }   

        return p;
    }

    private JPanel getPanel() {
        JPanel p = new JPanel();
        p.setBackground(Color.RED);
        p.setPreferredSize(new Dimension(200, 200));
        return p;
    }

    private JPanel getHeader() {
        JPanel p = new JPanel();
        p.add(new JLabel("Titel"));
        return p;
    }

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

I also found Oracle VerticalFlowLayout but was not able to access it. Might it help me? In case - how to use it?

Otherwise.. How to use JFrame to archive that behavior? Thank you


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

1 Reply

0 votes
by (71.8m points)
等待大神答复

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

...