Question: JAVA. I cannot get my program to work. Add a JList of eight items so the user can change the background color of the applet.

JAVA. I cannot get my program to work.

Add a JList of eight items so the user can change the background color of the applet.

import java.awt.*; import java.awt.event.*;

import javax.swing.*; import javax.swing.event.*;

public class DrawBG extends JApplet implements ListSelectionListener, ActionListener { //variable for JList private JList ListBG; //string array private String[] ColorBG = { "Black", "Blue", "Cyan", "Gray", "Green", "Orange", "Red", "Yellow"}; //color array private Color[] SelColor = {Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.orange, Color.red, Color.yellow}; //declare JScrollPane variable private JScrollPane SelectC; //declare current color private Color CurColor = Color.gray; //declare NumTF private JTextField NumTF; //declare label variable private JLabel Label; //ok butten private JButton OKBtn; //declare num var private int Num = 0; //init method public void init() { //container Container C = getContentPane(); setSize(500, 500); Label = new JLabel("Enter number"); Label.setLocation(200, 40); Label.setSize(100, 30); NumTF = new JTextField(1); NumTF.setLocation(300, 40); NumTF.setSize(100, 30); OKBtn = new JButton("OK"); OKBtn.setLocation(400, 40); OKBtn.setSize(50, 30); OKBtn.addActionListener(this); C.setLayout(null); C.add(Label); C.add(NumTF); C.add(OKBtn); ListBG = new JList(ColorBG); ListBG.setVisibleRowCount(3); ListBG.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ListBG.addListSelectionListener(this); SelectC = new JScrollPane(ListBG); SelectC.setSize(100, 60); SelectC.setLocation(200, 350); C.add(SelectC); } //paint method public void paint(Graphics g) { super.paint(g); g.setColor(Color.orange); g.setColor(CurColor); switch(Num) { case 1: g.fillRect(20, 20, 20, 100); break; case 2: g.fillRect(20, 40, 80, 20); g.fillRect(80, 40, 20, 40); g.fillRect(20, 80, 80, 20); g.fillRect(20, 80, 20, 50); g.fillRect(20, 120, 80, 20); break; case 3: g.fillRect(20, 40, 80, 20); g.fillRect(80, 40, 20, 40); g.fillRect(20, 80, 80, 20); g.fillRect(80, 80, 20, 50); g.fillRect(20, 120, 80, 20); break; case 4: g.fillRect(20, 20, 20, 60); g.fillRect(40, 60, 40, 20); g.fillRect(80, 20, 20, 100); break; case 5: g.fillRect(20, 40, 80, 20); g.fillRect(20, 40, 20, 40); g.fillRect(20, 80, 80, 20); g.fillRect(80, 80, 20, 50); g.fillRect(20, 120, 80, 20); break; case 6: g.fillRect(20, 20, 20, 100); g.fillRect(40, 20, 40, 20); g.fillRect(40, 60, 20, 20); g.fillRect(40, 100, 20, 20); g.fillRect(60, 60, 20, 60); break; case 7: g.fillRect(40, 20, 30, 20); g.fillRect(70, 20, 20, 80); break; case 8: g.fillRect(20, 20, 20, 100); g.fillRect(40, 20, 40, 20); g.fillRect(40, 60, 20, 20); g.fillRect(40, 100, 20, 20); g.fillRect(60, 40, 20, 80); break; case 9: g.fillRect(20, 20, 20, 60); g.fillRect(40, 20, 40, 20); g.fillRect(40, 60, 20, 20); g.fillRect(40, 100, 20, 20); g.fillRect(60, 40, 20, 80); break; case 0: g.fillRect(20, 20, 29, 100); g.fillRect(40, 20, 60, 20); g.fillRect(40, 100, 60, 20); g.fillRect(100, 20, 20, 100); break; } } //action event method public void ValChange(ListSelectionEvent e) { CurColor = SelColor[ListBG.getSelectedIndex()]; repaint(); } public void ActPerform(ActionEvent e) { if(e.getActionCommand().equals("OK")) Num = Integer.parseInt(NumTF.getText()); repaint(); }

@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub }

@Override public void valueChanged(ListSelectionEvent e) { // TODO Auto-generated method stub } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!