1

This is my exercise description: When the user clicks on one of the buttons, the panel in the middle must change to a random colour. And the Label on top must have its text changed to a random element in an array of four strings. Use a button array for the four buttons. Whenever the user moves the curser over the label, the text colour must change to a random colour and font. When the mouse leaves the label,it must go back to default.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
//2.3.5 - 1
public class UserGui extends JFrame implements ActionListener, WindowListener, MouseListener {
    private JLabel label;
    private JButton button[] = new JButton[4];
    public UserGui() {
        super("My Frame");
        this.setSize(300,150);
        JMenuBar myMenuBar = new JMenuBar();
        ((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) );
        setLayout(new GridBagLayout());
        JPanel colorize = new JPanel();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;


        // from here
        //strings put in array for label change(by hovering over)
        String[] myStringArray = new String[]{"Funky","Classic","Legendary","Awesome"};
        Border border = BorderFactory.createLineBorder(Color.BLACK, 5);
        label = new JLabel("Java Wins!");
        c.ipady = 40;
        c.weightx = 0.0;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 0;

        setJMenuBar(myMenuBar);
        JMenu myMenu = new JMenu("File");
        JMenuItem quitItem = new JMenuItem("Quit");
        add(label);
        addWindowListener(this);
        myMenu.add(quitItem);
        myMenuBar.add(myMenu);      
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        quitItem.addActionListener(this);
        setVisible(true);
    }

    public void windowOpened(WindowEvent e) {
        JOptionPane.showMessageDialog(null, "Welcome!"); 
    }  
    /*
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals(""))
        {
            colorize.setText(field.getText());
        }
    }
    */
    public void actionPerformed(ActionEvent e) { 
        Object source = e.getSource(); 
        if (source.equals("Quit")) { 
            System.exit(1); 
        } 
    } 
    public void windowActivated(WindowEvent e) {} 
    public void windowClosed(WindowEvent e) {} 
    public void windowDeactivated(WindowEvent e) {} 
    public void windowDeiconified(WindowEvent e) {} 
    public void windowIconified(WindowEvent e) {}
    public void mouseClicked(MouseEvent e) {} 
    public void mouseReleased(MouseEvent e) {} 
    public void mousePressed(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void windowClosing(WindowEvent e) { 
        int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?"); 
        if (response == JOptionPane.YES_OPTION) { 
            System.exit(0); // Exits application 
        } 
    } 

    public static void main(String args[]) {
        JFrame frame = new UserGui();
        frame.setVisible(true);
    }
}

1 Answer 1

3

You can create a Jbutton array as follows, using a for loop to instantiate them and add them to a panel. You can use a quick switch statement to define your colours.

    JButton[] buttons = new JButton[4];
    for(int i = 0; i < buttons.length; i++){
        switch (i) {
            case 0:
                buttons[i] = new JButton("Red");
                break;
            case 1:
                buttons[i] = new JButton("Blue");
                break;
            case 2:
                buttons[i] = new JButton("Green");
                break;
            case 3:
                buttons[i] = new JButton("Black");
                break;
            default:
                break;
        }
        panel.add(buttons[i]);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.