Java AWT FlowLayout layout components in one row

Description

Java AWT FlowLayout layout components in one row

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

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


public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("FlowLayout frame");
    /*from  w w  w  . jav a  2s .  com*/
    frame.getContentPane().setLayout(new FlowLayout());
    
    frame.getContentPane().add(makePanel(Color.red));
    frame.getContentPane().add(makePanel(Color.orange));
    frame.getContentPane().add(makePanel(Color.green));
    frame.getContentPane().add(makePanel(Color.blue));
    frame.getContentPane().add(makePanel(new Color(75, 0, 130)));
    frame.getContentPane().add(makePanel(new Color(138, 43, 226)));
    
    frame.pack();
    frame.setVisible(true);
  }
  
  private static JPanel makePanel(Color color) {
    JPanel panel = new JPanel();
    panel.setBackground(color);
    return panel;
  }
} 
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("FlowLayout Nesting");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Set the content pane's layout to FlowLayout 
        frame.getContentPane().setLayout(new FlowLayout());

        // JPanel is an empty container with a FlowLayout manager 
        JPanel panel = new JPanel();

        // Add thirty JButtons to the JPanel 
        for (int i = 1; i <= 30; i++) {
            panel.add(new JButton("Button " + i));
        }/*from w w  w. ja  v a  2 s  .  c om*/

        // Add JPanel to the content pane 
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }
}



PreviousNext

Related