Simple program which demonstrates Swing/AWT Grid layout:
This post is intended to the beginners of swing/awt.
This below program creates a JFrame with 2 labels, 2 textfields and 2 buttons.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* @author Nagasharath Kopalle
*
* Simple program to demontrate Gridlayout
*
*/
public class GridLayoutDemo extends JFrame {
private JLabel labelName = null;
private JTextField textFieldName = null;
private JLabel labelPassword = null;
private JPasswordField passwordFieldPassword = null;
private JButton buttonSubmit = null;
private JButton buttonCancel = null;
/**
*
*/
public GridLayoutDemo() {
initComponents();
}
/**
* instantiates components and adds properties.
*/
void initComponents() {
this.setTitle("Grid Layout Demo");
this.setSize(300, 200);
GridLayout layout = new GridLayout(3, 2);
layout.setHgap(10);
layout.setVgap(5);
labelName = new JLabel("User Name: ");
textFieldName = new JTextField();
labelPassword = new JLabel("Password: ");
passwordFieldPassword = new JPasswordField();
buttonSubmit = new JButton("Submit");
buttonCancel = new JButton("Cancel");
this.setLayout(layout);
this.getContentPane().add(labelName);
this.getContentPane().add(textFieldName);
this.getContentPane().add(labelPassword);
this.getContentPane().add(passwordFieldPassword);
this.getContentPane().add(buttonSubmit);
this.getContentPane().add(buttonCancel);
}
public static void main(String[] args) {
GridLayoutDemo demo = new GridLayoutDemo();
demo.setVisible(true);
}
}
No comments:
Post a Comment