Java please answer A and B
a. Describe the purpose of action listener in push button jbtnCompare.(from the code below)
b. Explain why there are not action listeners added to text fields of this program.(from the code below)
code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class SwingFC implements ActionListener {
JTextField jtfFirst; // holds the first file name
JTextField jtfSecond; // holds the second file name
JButton jbtnComp; // button to compare the files
JLabel jlabFirst, jlabSecond; // displays prompts
JLabel jlabResult; // displays results and error messages
SwingFC() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Compare Files");
// Specify FlowLayout for the layout
manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(200, 190);
// Terminate the program when the user closes
the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the text fields for the file
names..
jtfFirst = new JTextField(14);
jtfSecond = new JTextField(14);
// Set the action commands for the text
fields.
jtfFirst.setActionCommand("fileA");
jtfSecond.setActionCommand("fileB");
// Create the Compare button.
JButton jbtnComp = new JButton("Compare");
// Add action listener for the Compare
button.
jbtnComp.addActionListener(this);
// Create the labels.
jlabFirst = new JLabel("First file: ");
jlabSecond = new JLabel("Second file: ");
jlabResult = new JLabel("");
// Add the components to the content
pane.
jfrm.add(jlabFirst);
jfrm.add(jtfFirst);
jfrm.add(jlabSecond);
jfrm.add(jtfSecond);
jfrm.add(jbtnComp);
jfrm.add(jlabResult);
// Display the frame.
jfrm.setVisible(true);
}
// Compare the files when the Compare button is pressed.
public void actionPerformed(ActionEvent ae) {
int i=0, j=0;
// First, confirm that both file names
have
// been entered.
if(jtfFirst.getText().equals("")) {
jlabResult.setText("First file name
missing.");
return;
}
if(jtfSecond.getText().equals("")) {
jlabResult.setText("Second file name
missing.");
return;
}
// Compare files. Use try-with-resources to
manage the files.
try (FileInputStream f1 = new
FileInputStream(jtfFirst.getText());
FileInputStream f2
= new FileInputStream(jtfSecond.getText()))
{
// Check the contents of each
file.
do {
i = f1.read();
j = f2.read();
if(i != j) break;
} while(i != -1 && j !=
-1);
if(i != j)
jlabResult.setText("Files are not the same.");
else
jlabResult.setText("Files compare equal.");
} catch(IOException exc) {
jlabResult.setText("File
Error");
}
}
public static void main(String args[]) {
// Create the frame on the event dispatching
thread.
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
new SwingFC();
}
});
}
}
A) Describe the purpose of action listener in push button jbtnCompare.(from the code below)
jbtnCompare Button implements an action listener to define what should be done when a user performs a certain operation. for example when a user clicks jbtnCompare Button. Two file name which are in the textfield are open and checked if their content matches then it shows the message Files compare equal and when not shows message Files are not the same."
B) Explain why there are not action listeners added to text fields of this program.(from the code below)
we do not add the action listener to text fields as because we need text only (file names) to check if the content is equal we do not need to occur any event while getting the text in text fields.
/* PLEASE UPVOTE (THANK YOU IN ADVANCE) */
Java please answer A and B a. Describe the purpose of action listener in push button...
tart from the following code, and add Action Listener to make it
functional to do the temperature conversion in the direction of the
arrow that is clicked.
. (Need about 10 lines)
Note:
import javax.swing.*;
import
java.awt.GridLayout;
import
java.awt.event.*;
import
java.text.DecimalFormat;
public class temperatureConverter extends JFrame {
public static void
main(String[] args) {
JFrame frame = new temperatureConverter();
frame.setTitle("Temp");
frame.setSize(200, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public
temperatureConverter() {
JLabel lblC = new...
Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...
Why when executed my frame does not contain what i have added to it? Here is my code import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JPanel; @SuppressWarnings("serial") public class AddressBook1 extends JFrame implements ActionListener { /** * */ JLabel name = new JLabel("Name: "); JLabel address = new JLabel("Address: "); JLabel phone = new JLabel("Phone: "); JLabel email = new JLabel("Email: ");...
Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file. Program...
Basic button tracking
Deliverables
Updated files for
app6.java
myJFrame6.java
myJPanel6.java
student.java
Contents
You can start with the files available here.
public class app6
{
public static void main(String args[])
{
myJFrame6 mjf = new myJFrame6();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJFrame6 extends JFrame
{
myJPanel6 p6;
public myJFrame6 ()
{
super ("My First Frame");
//------------------------------------------------------
// Create components: Jpanel, JLabel and JTextField
p6 = new myJPanel6();
//------------------------------------------------------...
Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...
JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...
Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box that displays the temporary password should be read-only. The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year. If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields. If the user does not enter data, clear the...
With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...
please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....