f. Develop swing application to present set of choices for a user to select different colours, on selection of colour the background of application changes its colour respectively.

CODE:

package swing_practicals;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

public class List1 extends JFrame implements ListSelectionListener {

     JList lst1;

Color clr[]={Color.RED,Color.GREEN,Color.BLUE,Color.BLACK,Color.CYAN,Color.PINK};

List1()

{

setTitle("ListBox Demo");

setSize(400,400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

                setLayout(new FlowLayout());

String str[]={"Red","Green","Blue","Black","Cyan","Pink"};

lst1 = new JList(str);

lst1.setVisibleRowCount(4);

lst1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

                JScrollPane sp = new JScrollPane(lst1);

                add(sp);

getContentPane().setBackground(Color.RED);

                lst1.addListSelectionListener(this);

}

public void valueChanged(ListSelectionEvent e)

{

getContentPane().setBackground(clr[lst1.getSelectedIndex()]);

}

 

    public static void main(String args[])

{

new List1();

}                                                                                                    

}

output:

Programs based on AWT and Swing.

Post a Comment

0 Comments