首页javalayoutJava Swing - 如何为JFrame设置FlowLayout

Java Swing - 如何为JFrame设置FlowLayout

我们想知道如何为JFrame设置FlowLayout。
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {
  JFrame f = new JFrame();
  Container c;
  JButton btn;
  JTextField tf;

  public Main() {
    f.setBounds(50, 50, 300, 300);

    c = f.getContentPane();
    c.setLayout(new FlowLayout());

    btn = new JButton("OK");
    tf = new JTextField(20);

    c.add(btn);
    c.add(tf);

    f.setVisible(true);
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
  }

  public static void main(String[] val) {
    new Main();
  }
}