JAVA Modify this code to you create a menu and choose the color. The color selec
ID: 3672523 • Letter: J
Question
JAVA Modify this code to you create a menu and choose the color. The color selected by the user changes the digit to that color. In the first exercise the color was black. In this exercise the sdigit must load with darkGray color.
here 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 Applet Viewer: Ch12-PrExercises.class Applet Color Red Blue Green Yellow Black Pink Cyan Magenta Applet started.Explanation / Answer
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.