Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.Color;
- import javax.swing.JList;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.ListSelectionModel;
- import javax.swing.event.ListSelectionListener;
- import javax.swing.event.ListSelectionEvent;
- class ListGUI extends JFrame {
- private JList list;
- private String colorNames[ ] = {"black", "blue", "red", "green"};
- private Color colors[ ] = {Color.BLACK, Color.BLUE, Color.RED, Color.GREEN};
- public static void main(String args[ ]) {
- ListGUI gui = new ListGUI();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- gui.setSize(300, 200);
- gui.setVisible(true);
- }
- ListGUI() {
- super("background changer");
- setLayout(new FlowLayout());
- list = new JList(colorNames);
- list.setVisibleRowCount(colorNames.length);
- list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- add(new JScrollPane(list));
- getContentPane().setBackground(colors[0]);
- list.addListSelectionListener(
- new ListSelectionListener() {
- public void valueChanged(ListSelectionEvent event) {
- getContentPane().setBackground(colors[list.getSelectedIndex()]);
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement