In java language Question 1 Briefly explain what the following code snippets do
ID: 3687980 • Letter: I
Question
In java language
Question 1
Briefly explain what the following code snippets do if displayed to the user:
private JTextField input;
private JLabel output;
public MysteryClass () {
// other setup code not displayed
input = new JTextField("Enter something here ");
contentPane.add(input );
output = new JLabel("waiting");
contentPane.add(output);
input .addActionListener(new InputFieldListener());
}
public class InputFieldListenerimplements ActionListener {
public void actionPerformed(ActionEvent e) {
String inputText = input.getText();
boolean correct = inputText.equalsIgnoreCase("something") ? true : false;
output.setText(correct ? "you got it" : "not quite");
if(correct) {
input.setEnabled(false);
}
}
}
Question 2
Assume you have a GUI program where the user types text into one text box, clicks a button, and the text disappears from that text box and appears in a second text box. Write the code that would go inside of the handler/listener for the button. Use these references:
userInputTextField
outputTextField
submitButton
Question 3
Assume you have a GUI program with two text fields, a + button and a - button. The user should type a number into each text field. When they click one of the buttons, either the sum or difference is then calculated and displayed. The user can then no longer click either button.
Write the code that would inside of a single handler/listener for both buttons. Use the following objects:
JTextField inputNumber1TextField
JTextField inputNumber2TextField
JButton plusButton
JButton minusButton
JTextField resultTextField
Explanation / Answer
1)
The user is given a J Text field with the message of “Enter something here”
the input is added to the Content Pane
Then an output with a label of waiting is displayed
what ever the user enters here gets added to the Content Pane as well
The action listener is added to the input field listener
the function inputFieldListener has the function actionPerformed that does the following:
The string variable inputText receives the output of getText member function of the input class
The bollean variable correct stores the result of evaluating the case insensitive comparison of entered text with the text “something” – hence both SOMETHING and something will return true
if the user had enetered either SOMETHING or something, the out put will be “you got it”
otherwise it would say “not quite”
if correct is true, the setEnabled function of input class is set to false
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
javax.swing.JFrame frame = new javax.swing.JFrame("Text box content swapper");
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new ClientEditor());
frame.pack();
frame.setVisible(true);
}
});
public class textBoxContentSwapper extends javax.swing.JPanel {
private textBoxContentSwapper textBoxContentSwapperObj = textBoxContentSwapper.textBoxContentSwapperTest();
public textBoxContentSwapper() {
initComponents();
bindingGroup.addBindingListener(new LoggingBindingListener(validationMsgLabel));
}
javax.swing.JFrame frame = new javax.swing.JFrame(" + ");
javax.swing.JFrame frame = new javax.swing.JFrame(" - ");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.