Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Gui extends JFrame{
- // Variables
- private JComboBox box; // dropdown list
- private JLabel picture;
- private static String[] filename = {"b.png", "x.png"}; // Images' names
- private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))}; // Actual images
- // Constructor
- public Gui(){
- super("JFrame's title");
- setLayout(new FlowLayout());
- box = new JComboBox(filename); // Puts all array's elements in a dropdown list
- // Functionality with item listener
- box.addItemListener(
- // Anonymus inner class
- new ItemListener() {
- @Override
- public void itemStateChanged(ItemEvent event) {
- if(event.getStateChange() == ItemEvent.SELECTED)
- picture.setIcon(pics[box.getSelectedIndex()]);
- }
- }
- );
- add(box);
- picture = new JLabel(pics[0]); // Default value
- add(picture);
- }
- // 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