Skip to content

Instantly share code, notes, and snippets.

@AnthoniG
Created March 7, 2022 18:00
Show Gist options
  • Save AnthoniG/78194e30f903124333ccc7a6bcc34bd2 to your computer and use it in GitHub Desktop.
Save AnthoniG/78194e30f903124333ccc7a6bcc34bd2 to your computer and use it in GitHub Desktop.
Java Swing+MigLayout Tutorial - Simple Form
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class Demo02 extends JFrame {
public Demo02() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new MigLayout(""));
JLabel title = new JLabel("Registration Form", JLabel.CENTER);
title.setOpaque(true);
title.setBackground(Color.black);
title.setForeground(Color.white);
title.setPreferredSize(new Dimension(1, 27));
add(title, "span, wrap 15, growx");
add(new JLabel("Name"), "al right");
add(createTextField(), "wrap, pushx, growx");
add(new JLabel("Phone"), "al right");
add(createTextField(), "wrap, pushx, growx");
add(new JLabel("DOB"), "al right");
add(new JComboBox(new String[]{"1989"}), "split 3");
add(new JComboBox(new String[]{"01"}));
add(new JComboBox(new String[]{"01"}), "wrap");
add(new JLabel("Address"), "al right");
add(new JScrollPane(new JTextArea(3, 10)), "wrap, pushx, growx");
add(new JLabel("Resume"), "al right");
add(createTextField(), "pushx, growx, split 2");
add(new JButton("..."), "wrap 10");
add(new JLabel(""));
add(new JButton("Register"));
setPreferredSize(new Dimension(350, 300));
pack();
setVisible(true);
}
private JTextField createTextField() {
JTextField tf = new JTextField();
tf.setPreferredSize(new Dimension(1, 27));
return tf;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Demo02();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment