Java program to lay out components in a flow to be centered with FlowLayout?

Java 8Object Oriented ProgrammingProgramming

Use FlowLayout.CENTER to lay out components in a flow to be centered with FlowLayout. The following is an example to lay out components in a flow to be centered with FlowLayout −

Example

package my;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame("WorldCup2019");
      frame.setLayout(new FlowLayout(FlowLayout.CENTER));
      JLabel label = new JLabel("WorldCup Hosting Country ");
      label.setPreferredSize(new Dimension(220, 70));
      label.setOpaque(true);
      label.setBackground(Color.ORANGE);
      label.setForeground(Color.BLACK);
      Font font = new Font("Serif", Font.BOLD, 14);
      label.setFont(font);
      label.setHorizontalAlignment(JLabel.CENTER);
      JTextArea text = new JTextArea();
      text.setText("England");
      font = new Font("Serif", Font.BOLD, 13);
      text.setFont(font);
      frame.add(label);
      frame.add(text);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

Output

raja
Published on 31-May-2019 10:43:39
Advertisements