MOdify this JAVA code to Use List to change the background color -\"Red\", \"Blu
ID: 3672525 • Letter: M
Question
MOdify this JAVA code to Use List to change the background color -"Red", "Blue", "Green", "Yellow", "Pink", "Black", "Cyan", "Magenta".
this is the code:
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
public class NewApplet extends Applet {
Label lCommand;
TextField tNumber;
Panel pDetails;
Button cancel;
Button ok;
@Override
public void init() {
// initialize components
lCommand = new Label("Enter a digit");
tNumber = new TextField(9);
ok = new Button("OK");
cancel = new Button("cancel");
ok.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
//Create the frame.
JFrame frame = new JFrame("Applet");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create components and put them in the frame.
//...create emptyLabel...
Label number = null;
try {
number = new Label(tNumber.getText());
} catch (Exception ex) {
number = new Label("0");
}
// create custom font
Font myFont = new Font("Serif", Font.BOLD, 80);
number.setFont(myFont);
frame.getContentPane().add(number, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
}
});
cancel.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
pDetails = new Panel();
// add the components to panel
pDetails.add(lCommand);
pDetails.add(tNumber);
pDetails.add(ok);
pDetails.add(cancel);
pDetails.setSize(100, 100);
pDetails.validate();
add(pDetails);
}
}
the output should look like this:
Input Enter a digit OK Cancel Change Background Color via List Green Yellow PinkExplanation / Answer
Add the below code to your code:
--------------------------------------------------------------
String colors[] = { "Red", "Blue", "Green", "Yellow", "Pink", "Black", "Cyan", "Magenta" };
JList<String> list = new JList<String>(colors); // creates a
// list box with
// colors as
// data for list
// box
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(-1);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(100, 50));
ListSelectionModel listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
if (e.getValueIsAdjusting() == false) {
String value = list.getSelectedValue();
System.out.println(value);
if (value.equals("Red")) {
frame.getContentPane().setBackground(Color.RED);
} else if (value.equals("Blue")) {
frame.getContentPane().setBackground(Color.BLUE);
} else if (value.equals("Green")) {
frame.getContentPane().setBackground(Color.GREEN);
} else if (value.equals("Yellow")) {
frame.getContentPane().setBackground(Color.YELLOW);
} else if (value.equals("Pink")) {
frame.getContentPane().setBackground(Color.PINK);
} else if (value.equals("Black")) {
frame.getContentPane().setBackground(Color.BLACK);
} else if (value.equals("Cyan")) {
frame.getContentPane().setBackground(Color.CYAN);
} else if (value.equals("Magenta")) {
frame.getContentPane().setBackground(Color.MAGENTA);
}
}
}
});
frame.getContentPane().add(listScroller, BorderLayout.NORTH);
--------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.