rewrite the following code using anonymous class. submit.addActionListener(newBu
ID: 3583067 • Letter: R
Question
rewrite the following code using anonymous class.
submit.addActionListener(newButtonListener());
private class ButtonListenerimplements ActionListener {
public voidactionPerformed(ActionEvent e) {
if ( (java.isSelected() ||helpDesk.isSelected()
||coffee.isSelected()) &&
(goodCitizen.isSelected()) &&
(!salary.getSelectedItem().equals("above $100,000")))
{
JOptionPane.showMessageDialog(null,
"Thank you for your application submission. " +
"We'll contact you after we process your information.");
}
}
Explanation / Answer
Please find below the java code :
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*; // To get EmptyBorder
public class JobApplication extends JFrame //public class JobApplication
{
private static final int WIDTH = 350; //width declared
private static final int HEIGHT = 400; //height declared
private JCheckBox java; // If Sun Java certified
private JCheckBox helpDesk; // If holds help-desk experience
private JCheckBox coffee; // If a good coffee maker
private JRadioButton goodCitizen, criminal;
private JComboBox salary; //salary
private String[] salaryOptions =
{"$20,000-$59,000", "$60,000-$100,000", "above $100,000"}; //salary ranges
private JButton submit; // To submit the application
//***************************************
public JobApplication() //public class JobApplication
{
setSize(WIDTH, HEIGHT);
setTitle("Job Application Form");
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
} // Here ends the JobApplication constructor
//**************************************
// To Create components and add to window.
private void createContents() //private function createContents()
{
ButtonGroup radioGroup;
// To have windowPanel for south-panel separation and outer margin.
JPanel windowPanel = new JPanel(new BorderLayout(0, 10)); //BorderLayout setting
windowPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
// To have centerPanel holds all components except button
JPanel centerPanel = new JPanel(new GridLayout(11, 1)); //To set GridLayout
// To have a panel for button so it can be center aligned
JPanel southPanel = new JPanel(new FlowLayout()); //To set FlowLayout
java = new JCheckBox("a Sun Java certified");
helpDesk = new JCheckBox("a help-desk experience");
coffee = new JCheckBox("a good coffee maker");
criminal = new JRadioButton("a violent criminal"); //If a violent criminal
goodCitizen = new JRadioButton("a law abiding citizen"); //For law abiding citizens
radioGroup = new ButtonGroup();
radioGroup.add(goodCitizen); //ADD GOOD CITIZENS
radioGroup.add(criminal); //add criminals
salary = new JComboBox(salaryOptions); //salary options
submit = new JButton("Submit");
submit.addActionListener(new ButtonListener());
centerPanel.add(new JLabel("Skills (This will check all that apply):"));
centerPanel.add(java); //add java certified
centerPanel.add(helpDesk); //add helpdesk
centerPanel.add(coffee); //add coffee
centerPanel.add(new JLabel()); // This is filler
centerPanel.add(new JLabel("Community standing:"));
centerPanel.add(goodCitizen); //add good citizen
centerPanel.add(criminal); //add criminal
centerPanel.add(new JLabel()); // This is filler
centerPanel.add(new JLabel("Salary requirements:"));
centerPanel.add(salary); //add salary
windowPanel.add(centerPanel, BorderLayout.CENTER);
southPanel.add(submit);
windowPanel.add(southPanel, BorderLayout.SOUTH);
add(windowPanel);
} // Here ends the createContents
//***************************************
// To Read entered values and display an appropriate message.
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if ( //if statement
(java.isSelected() || helpDesk.isSelected() //condition
|| coffee.isSelected()) &&
(goodCitizen.isSelected()) &&
(!salary.getSelectedItem().equals("above $100,000"))) //getSelectedItem() function
{
JOptionPane.showMessageDialog(null,
"Thank you for your application submission. " +
"We'll contact you after we process your information.");
}
else
{
JOptionPane.showMessageDialog(null,
"Sorry, no jobs at this time.");
}
} // Here ends the actionPerformed
} // Here ends the class ButtonListener
public static void main(String[] args)
{
new JobApplication();
}
} // Here ends the class JobApplication
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.