Use of CardLayout to write a program to create a two-level card deck that allows the user to select an operating system.
1. Write Java program to display following output.
Use of CardLayout to write a program to create a two-level card deck that allows the user to select an operating system. |
OUTPUT :
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class exercise1
{
public exercise1()
{
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JFrame frame = new JFrame();
frame.setLayout(gbl);
frame.setTitle("GridBag Layout Example");
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
frame.add(new Button("Button two"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
frame.add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
frame.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridwidth = 2;
frame.add(new Button("Button Five"), gbc);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args)
{
exercise1 a = new exercise1();
}
}
2. Write Java Program to display following output.
Use of CardLayout to write a program to create a two-level card deck that allows the user to select an operating system. |
OUTPUT :
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class exercise2{
public exercise2() {
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JFrame frame = new JFrame();
frame.setLayout(grid);
frame.setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
JLabel L1 = new JLabel("Name");
JLabel L2 = new JLabel("Comments");
JTextField T1 = new JTextField();
JTextArea T2 = new JTextArea(10, 10);
JScrollPane SP = new JScrollPane(T2);
SP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(L1, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
frame.add(T1, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
frame.add(L2, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
frame.add(T2, gbc);
frame.getContentPane().add(SP);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
frame.add(new Button("Submit"), gbc);
frame.setSize(300, 300);
//frame.setPreferredSize(getSize());
frame.setVisible(true);
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
exercise2 ex2 = new exercise2();
}
}