Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is a code creating 4 simple buttons. I need to modify it so I can give col

ID: 3881284 • Letter: B

Question

Below is a code creating 4 simple buttons. I need to modify it so I can give color to the buttons and the background please help

import java.awt.*;
import javax.swing.*;

class ButtonFrame extends JFrame
{
JButton Button1 ;
JButton Button2 ;
JButton Button3 ;
JButton Button4 ;

// constructor for ButtonFrame
ButtonFrame(String title)
{
    super( title );                    
    setLayout( new FlowLayout() );     

    Button1 = new JButton("Button 1");
    Button2 = new JButton("Button 2");
    Button3 = new JButton("Button 3");
    Button4 = new JButton("Button 4");
   
    add( Button1 );
    add( Button2 );
    add( Button3 );
    add( Button4 );   // add the button to the JFrame
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );  
}
}

public class ButtonDemo
{
public static void main ( String[] args )
{
    ButtonFrame frm = new ButtonFrame("Basic GUI");

    frm.setSize( 400, 80 );    
    frm.setVisible( true );  
}
}

Explanation / Answer

import java.awt.*;
import javax.swing.*;

class ButtonFrame extends JFrame
{
JButton Button1 ;
JButton Button2 ;
JButton Button3 ;
JButton Button4 ;

// constructor for ButtonFrame
ButtonFrame(String title)
{
    super( title );                  
    setLayout( new FlowLayout() );   

    Button1 = new JButton("Button 1");
    Button1.setOpaque(true);
    Button1.setBackground(Color.BLACK);
    Button1.setForeground(Color.GRAY);

    Button2 = new JButton("Button 2");
    Button2.setOpaque(true);
    Button2.setBackground(Color.BLACK);
    Button2.setForeground(Color.GRAY);

    Button3 = new JButton("Button 3");
    Button3.setOpaque(true);
    Button3.setBackground(Color.BLACK);
    Button3.setForeground(Color.GRAY);

    Button4 = new JButton("Button 4");
    Button4.setOpaque(true);
    Button4.setBackground(Color.BLACK);
    Button4.setForeground(Color.GRAY);

    add( Button1 );
    add( Button2 );
    add( Button3 );
    add( Button4 );   // add the button to the JFrame
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}

public class ButtonDemo
{
public static void main ( String[] args )
{
    ButtonFrame frm = new ButtonFrame("Basic GUI");

    frm.setSize( 400, 80 );  
    frm.setVisible( true );
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote