Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How could I make the color box that appears when selected resize to the panel when enlarging? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Label;

How could I make the color box that appears when selected resize to the panel when enlarging?

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Label;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

@SuppressWarnings("serial")

public class threeRadioButtons extends JPanel implements ActionListener

{

JRadioButton redButton = new JRadioButton();

JRadioButton greenButton = new JRadioButton();

JRadioButton blueButton = new JRadioButton();

public threeRadioButtons()

{

add(redButton);

add(new Label("Red"));

add(greenButton);

add(new Label("Green"));

add(blueButton);

add(new Label("Blue"));

redButton.addActionListener(this);

greenButton.addActionListener(this);

blueButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();

Color color = getBackground();

if(source == redButton)

{

color = Color.red;

blueButton.setSelected(false);

greenButton.setSelected(false);

}

else if(source == greenButton)

{

color = Color.green;

blueButton.setSelected(false);

redButton.setSelected(false);

}

else if(source == blueButton)

{

color = Color.blue;

redButton.setSelected(false);

greenButton.setSelected(false);

}

JPanel p = new JPanel(new BorderLayout (3,3));

p.setSize(200, 100);

p.setLocation(50,50);

getRootPane().add(BorderLayout.SOUTH,p);

setVisible(true);

p.setBackground(color);

}

@SuppressWarnings("deprecation")

public static void main(String[] args)

{

JFrame frame = new JFrame("Radio");

frame.setSize(300,200);

frame.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent c)

{

System.exit(0);

}

});

Container content = (Container) frame.getContentPane();

content.add(new threeRadioButtons());

frame.show();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions