dh-Materialien
Java Projekte
// JavaProject LayoutPerformer
LayoutPerformer.java

// LayoutPerformer.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class MenuBar extends JMenuBar implements ActionListener {
    private static final long serialVersionUID = 0L;

    JMenu start;
    JMenuItem start_flow;
    JMenuItem start_grid;
    JMenuItem start_border;
    JMenuItem start_null;
    Frame lfr;

    public MenuBar(Frame lfr) {
        this.lfr = lfr;

        start = new JMenu("Start");
        add(start);

        start_flow = new JMenuItem("FlowLayout");
        start_flow.addActionListener(this);
        start.add(start_flow);

        start_grid = new JMenuItem("GridLayout");
        start_grid.addActionListener(this);
        start.add(start_grid);

        start_border = new JMenuItem("BorderLayout");
        start_border.addActionListener(this);
        start.add(start_border);

        start_null = new JMenuItem("Kein Layout");
        start_null.addActionListener(this);
        start.add(start_null);
    }

   
@Override    
    public void
actionPerformed(ActionEvent e) {
        if (!lfr.isRunning) {
            if (e.getSource() == start_flow) flowPanels();
            if (e.getSource() == start_grid) gridPanels();
            if (e.getSource() == start_border) borderPanels();
            if (e.getSource() == start_null) nullPanels();
        }
    }

    private void flowPanels() {
        removePanels();
        lfr.setLayout(new FlowLayout(FlowLayout.CENTER));
        lfr.run_typ = 1;
        lfr.runAnimation();
    }

    private void gridPanels() {
        removePanels();
        lfr.setLayout(new GridLayout(4, 2, 3, 3));
        lfr.run_typ = 2;
        lfr.runAnimation();
    }

    private void borderPanels() {
        removePanels();
        lfr.setLayout(new BorderLayout(3, 3));
        lfr.run_typ = 3;
        lfr.runAnimation();
    }

    private void nullPanels() {
        removePanels();
        lfr.setLayout(null);
        lfr.run_typ = 0;
        lfr.runAnimation();
    }

    private void removePanels() {
        lfr.getContentPane().removeAll();
        lfr.repaint();
    }
}


class Panel extends JPanel {
    private static final long serialVersionUID = 0L;

    JLabel label = new JLabel();
    Font font = new Font("Arial", Font.PLAIN, 24);

    Panel() {
        setSize (150, 100);
        setBackground(Color.GRAY);
        setBorder(BorderFactory.createEtchedBorder());

        label.setFont(font);
        label.setForeground(Color.WHITE);
        add(label);
    }
}


class Frame extends JFrame implements Runnable { // Threads
    private static final long serialVersionUID = 0L;
    Image icon;
    MenuBar menubar;
    Panel[] panels;

    Thread animation;
    byte run_typ;
    boolean isRunning = false;

    public Frame() {
        setTitle("Vergleich von Layouts");
        icon = Toolkit.getDefaultToolkit().getImage("dh.png");
        setIconImage(icon);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(349, 301);
        setLocationRelativeTo(null);

        menubar = new MenuBar(this);
        setJMenuBar(menubar);

        setVisible(true);
    }

    public void runAnimation() {
        animation = new Thread(this);
        animation.start();
        isRunning = true;
    }

    public void sleep() {
        try {
            Thread.sleep(1300);
        } catch (InterruptedException e) { }
    }

    @Override
    public void run() {
        String[] constraints = new String[7];
        Panel[] panels = new Panel[7];
        int i = 0;

        constraints[0] = BorderLayout.WEST;
        constraints[1] = BorderLayout.NORTH;
        constraints[2] = BorderLayout.EAST;
        constraints[3] = BorderLayout.CENTER;
        constraints[4] = BorderLayout.SOUTH;

        if (run_typ < 3) {
            while (i < 7) {
                panels[i] = new Panel();
                panels[i].label.setText(" "+i+" ");
                add(panels[i]);
                if (i < 6) sleep();
                i++;
            }
        }
        if (run_typ == 3) {
            while (i < 5) {
                panels[i] = new Panel();
                panels[i].label.setText(" "+i+" ");
                add(panels[i], constraints[i]);
                if (i < 4) sleep();
                i++;
            }
        }
        isRunning = false;
    }
}


public class LayoutPerformer {

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

Download LayoutPerformer