You are on page 1of 4

LAB TASK 2

QUESTION 1
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo extends Frame {
public static void main(String[] args) {
JFrame frm = new JFrame("BoxLayoutDemo"); //frame declaration
frm.setSize(300, 180);//set size frame
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pnl = new JPanel(); //panel declaration


JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button ");
JButton btn3 = new JButton("Button 3");
JButton btn4 = new JButton("Long-Named Button 4");
JButton btn5 = new JButton("5");
//button declaration

BoxLayout bl = new BoxLayout (pnl,BoxLayout.Y_AXIS); //boxlayout


declaration

pnl.setLayout(bl); //set layout to panel


btn1.setAlignmentX(CENTER_ALIGNMENT);
btn2.setAlignmentX(CENTER_ALIGNMENT);
btn3.setAlignmentX(CENTER_ALIGNMENT);
btn4.setAlignmentX(CENTER_ALIGNMENT);
btn5.setAlignmentX(CENTER_ALIGNMENT);
//set alignment to button

frm.add(pnl); //add panel to frame


pnl.add(btn1);
pnl.add(btn2);
pnl.add(btn3);
pnl.add(btn4);
pnl.add(btn5);
//add component to the panel

}
}
QUESTION 2
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class soalan2 {


public static void main(String[] args) {
JFrame frm = new JFrame("Layout4");//frame declaration
frm.setSize(400, 150); //set size to frame
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pnl = new JPanel (); //panel declaration

pnl.setLayout(new GridLayout(3, 2)); //set grid layout to the panel


frm.add(pnl, BorderLayout.NORTH); //add panel to the frame

JRadioButton radio1 = new JRadioButton("Show Component #1");


JRadioButton radio2 = new JRadioButton("Show Component #2");
JRadioButton radio3 = new JRadioButton("Show Component #3");
//radiobuttton declaration

JButton btn = new JButton ("Component 1"); //button declaration


frm.add(btn, BorderLayout.SOUTH); //add button to frame

pnl.add(radio1);
pnl.add(radio2);
pnl.add(radio3);
//add component to frame
}
}
QUESTION 3
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.JList;

public class soalan3 {


public static void main(String[] args) {
JFrame frm = new JFrame("Frame Abrar");//frame declaration
frm.setLayout(new BorderLayout());//set BorderLayout to frame
frm.setSize(400, 200);// set frame size
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set to close the
frame

JPanel pnl = new JPanel(new GridLayout(3,2)); //panel declaration

JLabel lab1 = new JLabel("First Name:");


JLabel lab2 = new JLabel("Last Name:");
JLabel lab3 = new JLabel("Address:");
//label declaration

JTextField txt = new JTextField (10);


JTextField txt2 = new JTextField(10);
JTextField txt3 = new JTextField(10);
//texfield declaration

JPanel pnl2 = new JPanel(); //panel declaration


pnl2.setLayout(new BorderLayout());//set Layout to panel
JLabel lab4 = new JLabel("Department:"); //label declaration

String[] choose = {"Finance", "Human Resources", "Marketing", "Payroll",


"Shipping"}; //string initialization

JList li = new JList (choose); //list declaration

JPanel pnl3 = new JPanel (); //panel declaration


frm.add(pnl, BorderLayout.WEST); //adding the pnl to frame
pnl.add(lab1);
pnl.add(txt);
pnl.add(lab2);
pnl.add(txt2);
pnl.add(lab3);
pnl.add(txt3);
// adding component to pnl

frm.add(pnl2, BorderLayout.EAST); //adding pnl2 to frame


pnl2.add(lab4, BorderLayout.NORTH);
pnl2.add(li, BorderLayout.SOUTH);
//adding component to panel

JButton btn1 = new JButton("Back");


JButton btn2 = new JButton("Cancel");
JButton btn3 = new JButton("Next");
//button declaration

frm.add(pnl3, BorderLayout.SOUTH); //adding pnl3 to frame


pnl3.add(btn1);
pnl3.add(btn2);
pnl3.add(btn3);
//adding component to panel

frm.pack();
//add method pack
}
}

You might also like