How to arrange components in a FlowLayout to be left-justified in Java?


Use FlowLayout.LEFT to arrange components in a FlowLayout to be left-justified. The following is an example to arrange components in a FlowLayout to be left-justified −

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("Internet Language");
      frame.setLayout(new FlowLayout(FlowLayout.LEFT));
      JLabel label = new JLabel("Top Internet Language");
      label.setPreferredSize(new Dimension(240, 70));
      label.setOpaque(true);
      label.setBackground(Color.RED);
      label.setForeground(Color.WHITE);
      Font font = new Font("Serif", Font.BOLD, 14);
      label.setFont(font);
      label.setHorizontalAlignment(JLabel.CENTER);
      JTextArea text = new JTextArea();
      text.setText("English");
      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 


Updated on: 30-Jul-2019

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements