0% found this document useful (0 votes)
4 views2 pages

ajp_4

The document contains two Java classes, Exp4_XIII1 and Exp4_XIII2, that demonstrate the use of GridBagLayout for creating user interfaces. Exp4_XIII1 creates a simple layout with buttons, while Exp4_XIII2 implements a form with text fields and a submit button that prints a message upon submission. Both classes extend the Frame class and set up their respective layouts and components in the constructor.

Uploaded by

treasurehunt901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

ajp_4

The document contains two Java classes, Exp4_XIII1 and Exp4_XIII2, that demonstrate the use of GridBagLayout for creating user interfaces. Exp4_XIII1 creates a simple layout with buttons, while Exp4_XIII2 implements a form with text fields and a submit button that prints a message upon submission. Both classes extend the Frame class and set up their respective layouts and components in the constructor.

Uploaded by

treasurehunt901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.awt.

*;
import java.awt.event.*;

class Exp4_XIII1 extends Frame {


Exp4_XIII1() {
setTitle("GridBagLayout");
setSize(300, 200);
setVisible(true);
setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();


gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
gbc.weighty = 1.0;

gbc.gridx = 0;
gbc.gridy = 0;
add(new Button("Button 1"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
add(new Button("Button 2"), gbc);

gbc.gridx = 0;
gbc.gridy = 1;
add(new Button("Button 3"), gbc);

gbc.gridx = 1;
gbc.gridy = 1;
add(new Button("Butoon 4"), gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
add(new Button("Button 5"), gbc);
}

public static void main(String[] args) {


new Exp4_XIII1();
}
}
import java.awt.*;
import java.awt.event.*;

class Exp4_XIII2 extends Frame {


TextField nameField;
TextArea commentsArea;
Button submitButton;

Exp4_XIII2() {
setTitle("Form Example with GridBagLayout");
setSize(400, 300);
setVisible(true);
setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();


gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.CENTER;
add(new Label("Name:"), gbc);

gbc.gridx = 1;
gbc.gridwidth = GridBagConstraints.RELATIVE;
nameField = new TextField(20);
add(nameField, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(new Label("Comments:"), gbc);

gbc.gridx = 1;
gbc.gridwidth = GridBagConstraints.RELATIVE;
commentsArea = new TextArea(5, 20);
add(commentsArea, gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.weighty = 0.0;
submitButton = new Button("Submit");
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Submitted");
}
});
add(submitButton, gbc);
}

public static void main(String[] args) {


new Exp4_XIII2();
}
}

You might also like