Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class Gui extends JFrame{
- // Variables
- private JList list;
- private static String[] colornames = {"black", "blue", "red", "white"};
- private static Color[] colors = {Color.BLACK, Color.BLUE, Color.RED, Color.WHITE};
- // Constructor
- public Gui(){
- super("JFrame's title");
- setLayout(new FlowLayout());
- list = new JList(colornames);
- list.setVisibleRowCount(4);
- list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Select only 1 at a time
- add(new JScrollPane(list));
- list.addListSelectionListener(
- new ListSelectionListener() {
- @Override
- public void valueChanged(ListSelectionEvent event) {
- // Access content pane ---> Set background
- getContentPane().setBackground(colors[list.getSelectedIndex()]);
- }
- }
- );
- }
- // Main Function
- public static void main(String[] args){
- Gui gui = new Gui();
- gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
- gui.setSize(600, 600);
- gui.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement