Ok, so I have the code mostly written, but I\'m not sure what I\'m missing. I do
ID: 3550440 • Letter: O
Question
Ok, so I have the code mostly written, but I'm not sure what I'm missing. I do not need a completely new code written, or one that doesn't work, or for someone to search this question on Chegg for the answer because the correct answer is not on here, I've already looked. This is the question:
Write an application that allows a user to select a favorite basketball team from a list box. Include at least five teams in the list, and display the chosen team in a text field after the user makes a selection. Save the file as JBasketball.java. This is what I have so far, but it is not quite right:
import javax.swing.*;
import java.avt.*;
public class JBasketball extends JFrame
{
private JButton b1 = new JButton("Miami Heat");
private JButton b2 = new JButton("Chicago Bulls");
private JButton b3 = new JButton("Boston Celtics");
private JButton b4 = new JButton("L.A. Lakers");
private JButton b5 = new JButton("Other");
private GridLayout layout = new GridLayout (3, 2, 5, 5);
public JBasketball ()
{
avtLayout (lyaout);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
antVisible(true);
avtSize(200, 200);
}
public static void main(String[] args)
{
JBasketball frame = new JBasketball[];
}
}
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class JBasketball extends JFrame implements ActionListener
{
private JButton b1 = new JButton("Miami Heat");
private JButton b2 = new JButton("Chicago Bulls");
private JButton b3 = new JButton("Boston Celtics");
private JButton b4 = new JButton("L.A. Lakers");
private JButton b5 = new JButton("Newyork Champs");
private JButton b6 = new JButton("Others");
private JTextArea statusArea = new JTextArea("Team here");
private GridLayout layout = new GridLayout (4, 2, 5, 5);
public JBasketball ()
{
super("BasketBall");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout (layout);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(statusArea);
statusArea.setEnabled(false);
statusArea.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
setVisible(true);
setSize(400, 400);
}
public static void main(String[] args)
{
JBasketball frame = new JBasketball();
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
JButton selected = (JButton) source;
if(selected.equals(b1))
b1.setBackground(Color.GREEN);
if(selected.equals(b2))
b2.setBackground(Color.GREEN);
if(selected.equals(b3))
b3.setBackground(Color.GREEN);
if(selected.equals(b4))
b4.setBackground(Color.GREEN);
if(selected.equals(b5))
b5.setBackground(Color.GREEN);
if(selected.equals(b6))
b6.setBackground(Color.GREEN);
String a = selected.getActionCommand();
statusArea.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 20));
statusArea.setText(a);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.